|
| 1 | +package apply |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/plan" |
| 12 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 13 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/state/sqlite" |
| 14 | +) |
| 15 | + |
| 16 | +func minimalGraph() *spec.ProjectGraph { |
| 17 | + return &spec.ProjectGraph{ |
| 18 | + Meta: spec.Metadata{Name: "acme"}, |
| 19 | + Spec: spec.ProjectSpec{}, |
| 20 | + Agents: map[string]*spec.AgentResource{}, |
| 21 | + Tools: map[string]*spec.ToolResource{}, |
| 22 | + Workflows: map[string]*spec.WorkflowResource{}, |
| 23 | + Policies: map[string]*spec.PolicyResource{}, |
| 24 | + Environments: map[string]*spec.EnvironmentResource{}, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func graphWithAgent() *spec.ProjectGraph { |
| 29 | + g := minimalGraph() |
| 30 | + g.Agents["rev"] = &spec.AgentResource{ |
| 31 | + APIVersion: spec.APIVersionV0, |
| 32 | + Kind: spec.KindAgent, |
| 33 | + Metadata: spec.Metadata{Name: "rev"}, |
| 34 | + Spec: spec.AgentSpec{Model: "m", Policy: "default"}, |
| 35 | + } |
| 36 | + return g |
| 37 | +} |
| 38 | + |
| 39 | +func TestApplyPlan_thenListShowsResources(t *testing.T) { |
| 40 | + ctx := context.Background() |
| 41 | + st, err := sqlite.Open(ctx, filepath.Join(t.TempDir(), "apply.db")) |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + t.Cleanup(func() { _ = st.Close() }) |
| 46 | + |
| 47 | + g := minimalGraph() |
| 48 | + pl := plan.NewPlanner(st) |
| 49 | + p, err := pl.ComputePlan(ctx, "dev", g) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + at := time.Date(2026, 4, 11, 12, 0, 0, 0, time.UTC) |
| 54 | + ap := NewApplier(st) |
| 55 | + if err := ap.ApplyPlan(ctx, "dev", g, p, at); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + list, err := st.ListAppliedResourcesByEnv(ctx, "dev") |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + if len(list) != 1 { |
| 64 | + t.Fatalf("resources: %+v", list) |
| 65 | + } |
| 66 | + if list[0].Kind != spec.KindProject || list[0].Name != "acme" { |
| 67 | + t.Fatalf("got %+v", list[0]) |
| 68 | + } |
| 69 | + got, err := st.GetAppliedResource(ctx, "dev", spec.ResourceID{Kind: spec.KindProject, Name: "acme"}) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + if got.SpecHash == "" || got.NormalizedSpecJSON == "" { |
| 74 | + t.Fatalf("missing spec material: %+v", got) |
| 75 | + } |
| 76 | + |
| 77 | + proj, err := st.GetAppliedProject(ctx, "dev", "acme") |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + if proj.Version == "" { |
| 82 | + t.Fatalf("applied_projects.version empty: %+v", proj) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestApplyPlan_deleteRemovesRow(t *testing.T) { |
| 87 | + ctx := context.Background() |
| 88 | + st, err := sqlite.Open(ctx, filepath.Join(t.TempDir(), "apply-del.db")) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + t.Cleanup(func() { _ = st.Close() }) |
| 93 | + |
| 94 | + pl := plan.NewPlanner(st) |
| 95 | + ap := NewApplier(st) |
| 96 | + t0 := time.Date(2026, 4, 11, 10, 0, 0, 0, time.UTC) |
| 97 | + t1 := t0.Add(time.Hour) |
| 98 | + |
| 99 | + gFull := graphWithAgent() |
| 100 | + p1, err := pl.ComputePlan(ctx, "dev", gFull) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + if err := ap.ApplyPlan(ctx, "dev", gFull, p1, t0); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + gOnly := minimalGraph() |
| 109 | + p2, err := pl.ComputePlan(ctx, "dev", gOnly) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + if err := ap.ApplyPlan(ctx, "dev", gOnly, p2, t1); err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + _, err = st.GetAppliedResource(ctx, "dev", spec.ResourceID{Kind: spec.KindAgent, Name: "rev"}) |
| 118 | + if !errors.Is(err, sql.ErrNoRows) { |
| 119 | + t.Fatalf("agent row should be gone: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + list, err := st.ListAppliedResourcesByEnv(ctx, "dev") |
| 123 | + if err != nil { |
| 124 | + t.Fatal(err) |
| 125 | + } |
| 126 | + if len(list) != 1 || list[0].Kind != spec.KindProject { |
| 127 | + t.Fatalf("want project only, got %+v", list) |
| 128 | + } |
| 129 | +} |
0 commit comments