| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | "reasonix/internal/event" |
| 10 | "reasonix/internal/provider" |
| 11 | "reasonix/internal/sessioninbox" |
| 12 | "reasonix/internal/tool" |
| 13 | ) |
| 14 | |
| 15 | func TestCorruptQueuedImageBlocksExecution(t *testing.T) { |
| 16 | root := t.TempDir() |
| 17 | p := &reviewImageProvider{requests: make(chan provider.Request, 4)} |
| 18 | ag := agent.New(p, tool.NewRegistry(), agent.NewSession("sys"), agent.Options{}, event.Discard) |
| 19 | c := newOwnedTestController(t, Options{WorkspaceRoot: root, SessionPath: filepath.Join(root, "session.jsonl"), Runner: ag, Executor: ag}) |
| 20 | draft, err := c.StageImage(t.Context(), "queued.png", "image/png", "data:image/png;base64,"+tinyPNG) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | receipt, err := c.EnqueueInboxContext(t.Context(), InboxRequest{Submit: "inspect", Attachments: []SubmissionAttachment{{ClientAttachmentID: "queued", DraftID: draft.ID}}}) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | digest := draft.Ref.Content.Digest |
| 29 | path := filepath.Join(c.attachmentService().Store().Root(), "objects", digest[:2], digest[2:4], digest) |
| 30 | if err := os.WriteFile(path, []byte("corrupt"), 0600); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if err := c.RunInboxTurn(t.Context(), receipt.ItemID); err == nil { |
| 34 | t.Fatal("corrupt queued image executed") |
| 35 | } |
| 36 | st, err := c.ensureInbox() |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | meta, _, err := st.ReadItem(receipt.ItemID) |
| 41 | if err != nil || meta.State != sessioninbox.StateBlocked { |
| 42 | t.Fatalf("queue state=%v err=%v", meta.State, err) |
| 43 | } |
| 44 | if len(p.requests) != 0 { |
| 45 | t.Fatal("corrupt queued image reached provider") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestAttachmentFingerprintIgnoresReboundCredentials(t *testing.T) { |
| 50 | a := SubmissionRequest{Input: "inspect @draft:old", Display: "", DraftIDs: []string{"old"}, Attachments: []SubmissionAttachment{{ClientAttachmentID: "photo", DraftID: "old"}}} |
| 51 | b := cloneSubmissionRequest(a) |
| 52 | b.Input, b.Display, b.DraftIDs, b.Attachments[0].DraftID = "inspect @draft:new", "", []string{"new"}, "new" |
| 53 | if canonicalSubmissionFingerprint(a) != canonicalSubmissionFingerprint(b) { |
| 54 | t.Fatal("transport credential changed request identity") |
| 55 | } |
| 56 | if a.Attachments[0].DraftID != "old" { |
| 57 | t.Fatal("fingerprinting mutated caller request") |
| 58 | } |
| 59 | b.Attachments[0].ClientAttachmentID = "different" |
| 60 | if canonicalSubmissionFingerprint(a) == canonicalSubmissionFingerprint(b) { |
| 61 | t.Fatal("logical attachment identity was ignored") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestQueueReceiptSurvivesCredentialRenewalAndRelease(t *testing.T) { |
| 66 | root := t.TempDir() |
| 67 | c := newOwnedTestController(t, Options{WorkspaceRoot: root, SessionPath: filepath.Join(root, "session.jsonl")}) |
| 68 | draft, err := c.StageImage(t.Context(), "queue.png", "image/png", "data:image/png;base64,"+tinyPNG) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | req := InboxRequest{Submit: "inspect", Display: "", Idempotency: "queue-stable", Attachments: []SubmissionAttachment{{ClientAttachmentID: "photo", DraftID: draft.ID}}} |
| 73 | first, err := c.EnqueueInboxContext(t.Context(), req) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | c.ReleaseDraftImage(draft.ID) |
| 78 | req.Attachments[0].DraftID = "replacement-credential" |
| 79 | req.Display = "" |
| 80 | again, err := c.EnqueueInboxContext(t.Context(), req) |
| 81 | if err != nil || first.ItemID != again.ItemID { |
| 82 | t.Fatalf("retry=%+v err=%v, want %s", again, err, first.ItemID) |
| 83 | } |
| 84 | req.Submit = "different user intent" |
| 85 | if _, err := c.EnqueueInboxContext(t.Context(), req); err == nil { |
| 86 | t.Fatal("different queue intent reused receipt") |
| 87 | } |
| 88 | } |
| 89 |