| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/agent" |
| 11 | "reasonix/internal/event" |
| 12 | "reasonix/internal/jobs" |
| 13 | "reasonix/internal/session" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | func canonicalIdentityController(t *testing.T, service *session.Service, id, legacy string, manager *jobs.Manager) *Controller { |
| 18 | t.Helper() |
| 19 | runtime, err := service.Create(t.Context(), session.CreateOptions{SessionID: id}) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | exec := agent.New(nil, tool.NewRegistry(), agent.NewSession("system"), agent.Options{}, event.Discard) |
| 24 | return newOwnedTestController(t, Options{Executor: exec, Sink: event.Discard, Jobs: manager, |
| 25 | SessionService: service, SessionRuntime: runtime, ExclusiveSession: true, SessionPath: legacy}) |
| 26 | } |
| 27 | |
| 28 | func TestCanonicalInboxWithLegacyImportPathUsesTemporaryStorage(t *testing.T) { |
| 29 | t.Chdir(t.TempDir()) |
| 30 | service, err := session.NewService("local", session.NewFilesystemPersistence(t.TempDir())) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | legacy := filepath.Join(t.TempDir(), "old.jsonl") |
| 35 | c := canonicalIdentityController(t, service, "imported.session", legacy, nil) |
| 36 | st, err := c.ensureInbox() |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if st.SessionPath() != "session-id:imported.session" || !filepath.IsAbs(st.Dir()) { |
| 41 | t.Fatalf("mixed locator and storage path: locator=%q directory=%q", st.SessionPath(), st.Dir()) |
| 42 | } |
| 43 | if _, err := os.Stat("session-id:imported.session.inbox"); !os.IsNotExist(err) { |
| 44 | t.Fatalf("canonical locator became a file path: %v", err) |
| 45 | } |
| 46 | if _, err := os.Stat(filepath.Join(filepath.Dir(legacy), "old.inbox")); !os.IsNotExist(err) { |
| 47 | t.Fatalf("canonical inbox wrote beside the import source: %v", err) |
| 48 | } |
| 49 | receipt, err := c.EnqueueInbox(InboxRequest{ExpectedSessionPath: "session-id:imported.session", Submit: "later"}) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | meta, _, err := c.ReadInboxItem(receipt.ItemID) |
| 54 | if err != nil || meta.SessionID != "imported.session" { |
| 55 | t.Fatalf("queue item lost its exact canonical ID: %+v %v", meta, err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestCanonicalRuntimeJobsAndPermissionSnapshotsUseExactSessionIdentity(t *testing.T) { |
| 60 | service, err := session.NewService("local", session.NewFilesystemPersistence(t.TempDir())) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | manager := jobs.NewManager(event.Discard) |
| 65 | t.Cleanup(manager.Close) |
| 66 | a := canonicalIdentityController(t, service, "a", "", manager) |
| 67 | b := canonicalIdentityController(t, service, "b", "", manager) |
| 68 | empty := canonicalIdentityController(t, service, "empty", "", manager) |
| 69 | start := func(id string) *jobs.Job { |
| 70 | return manager.StartForSession(id, "bash", id, func(ctx context.Context, _ io.Writer) (string, error) { |
| 71 | <-ctx.Done() |
| 72 | return "", ctx.Err() |
| 73 | }) |
| 74 | } |
| 75 | jobA, jobB := start("a"), start("b") |
| 76 | for _, tc := range []struct { |
| 77 | c *Controller |
| 78 | id string |
| 79 | count int |
| 80 | }{{a, "a", 1}, {b, "b", 1}, {empty, "empty", 0}} { |
| 81 | if got := tc.c.RuntimeStateSnapshot().BackgroundJobs; got != tc.count { |
| 82 | t.Errorf("session %s counted %d jobs, want %d", tc.id, got, tc.count) |
| 83 | } |
| 84 | if got := tc.c.PermissionSnapshot().SessionID; got != tc.id { |
| 85 | t.Errorf("session %s permission identity = %q", tc.id, got) |
| 86 | } |
| 87 | } |
| 88 | if a.CancelJob(jobB.ID) || !a.CancelJob(jobA.ID) || !b.CancelJob(jobB.ID) { |
| 89 | t.Fatal("job cancellation crossed its session boundary") |
| 90 | } |
| 91 | } |
| 92 |