|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { artifactsHandlers, collectArtifactsFromMessages } from "./artifacts.js"; |
| 3 | + |
| 4 | +const hoisted = vi.hoisted(() => ({ |
| 5 | + loadSessionEntry: vi.fn(), |
| 6 | + readSessionMessages: vi.fn(), |
| 7 | + resolveSessionKeyForRun: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("../session-utils.js", async () => { |
| 11 | + const actual = await vi.importActual<typeof import("../session-utils.js")>("../session-utils.js"); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + loadSessionEntry: hoisted.loadSessionEntry, |
| 15 | + readSessionMessages: hoisted.readSessionMessages, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +vi.mock("../server-session-key.js", async () => { |
| 20 | + const actual = await vi.importActual<typeof import("../server-session-key.js")>( |
| 21 | + "../server-session-key.js", |
| 22 | + ); |
| 23 | + return { |
| 24 | + ...actual, |
| 25 | + resolveSessionKeyForRun: hoisted.resolveSessionKeyForRun, |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +function createResponder() { |
| 30 | + const calls: Array<{ ok: boolean; payload?: unknown; error?: unknown }> = []; |
| 31 | + return { |
| 32 | + calls, |
| 33 | + respond: (ok: boolean, payload?: unknown, error?: unknown) => { |
| 34 | + calls.push({ ok, payload, error }); |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +describe("artifacts RPC handlers", () => { |
| 40 | + beforeEach(() => { |
| 41 | + vi.clearAllMocks(); |
| 42 | + hoisted.loadSessionEntry.mockReturnValue({ |
| 43 | + storePath: "/tmp/sessions.json", |
| 44 | + entry: { sessionId: "sess-main", sessionFile: "/tmp/sess-main.jsonl" }, |
| 45 | + }); |
| 46 | + hoisted.readSessionMessages.mockReturnValue([ |
| 47 | + { |
| 48 | + role: "assistant", |
| 49 | + content: [ |
| 50 | + { type: "text", text: "see attached" }, |
| 51 | + { |
| 52 | + type: "image", |
| 53 | + data: "aGVsbG8=", |
| 54 | + mimeType: "image/png", |
| 55 | + alt: "result.png", |
| 56 | + }, |
| 57 | + ], |
| 58 | + __openclaw: { seq: 2 }, |
| 59 | + }, |
| 60 | + ]); |
| 61 | + }); |
| 62 | + |
| 63 | + it("lists stable transcript artifact summaries by sessionKey", () => { |
| 64 | + const { calls, respond } = createResponder(); |
| 65 | + |
| 66 | + artifactsHandlers["artifacts.list"]?.({ |
| 67 | + req: { id: 1, method: "artifacts.list", params: {} }, |
| 68 | + params: { sessionKey: "agent:main:main" }, |
| 69 | + client: null, |
| 70 | + isWebchatConnect: () => false, |
| 71 | + respond, |
| 72 | + context: {} as never, |
| 73 | + }); |
| 74 | + |
| 75 | + expect(calls).toHaveLength(1); |
| 76 | + expect(calls[0]?.ok).toBe(true); |
| 77 | + const payload = calls[0]?.payload as { artifacts?: Array<Record<string, unknown>> }; |
| 78 | + expect(payload.artifacts).toHaveLength(1); |
| 79 | + expect(payload.artifacts?.[0]).toMatchObject({ |
| 80 | + type: "image", |
| 81 | + title: "result.png", |
| 82 | + mimeType: "image/png", |
| 83 | + sizeBytes: 5, |
| 84 | + sessionKey: "agent:main:main", |
| 85 | + messageSeq: 2, |
| 86 | + source: "session-transcript", |
| 87 | + download: { mode: "bytes" }, |
| 88 | + }); |
| 89 | + expect(payload.artifacts?.[0]?.id).toMatch(/^artifact_/); |
| 90 | + expect(payload.artifacts?.[0]).not.toHaveProperty("data"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("gets and downloads an inline artifact", () => { |
| 94 | + const listed = collectArtifactsFromMessages({ |
| 95 | + sessionKey: "agent:main:main", |
| 96 | + messages: hoisted.readSessionMessages(), |
| 97 | + }); |
| 98 | + const artifactId = listed[0]?.id; |
| 99 | + expect(artifactId).toBeTruthy(); |
| 100 | + |
| 101 | + const get = createResponder(); |
| 102 | + artifactsHandlers["artifacts.get"]?.({ |
| 103 | + req: { id: 1, method: "artifacts.get", params: {} }, |
| 104 | + params: { sessionKey: "agent:main:main", artifactId }, |
| 105 | + client: null, |
| 106 | + isWebchatConnect: () => false, |
| 107 | + respond: get.respond, |
| 108 | + context: {} as never, |
| 109 | + }); |
| 110 | + expect(get.calls[0]?.ok).toBe(true); |
| 111 | + expect(get.calls[0]?.payload).toMatchObject({ |
| 112 | + artifact: { id: artifactId, download: { mode: "bytes" } }, |
| 113 | + }); |
| 114 | + |
| 115 | + const download = createResponder(); |
| 116 | + artifactsHandlers["artifacts.download"]?.({ |
| 117 | + req: { id: 1, method: "artifacts.download", params: {} }, |
| 118 | + params: { sessionKey: "agent:main:main", artifactId }, |
| 119 | + client: null, |
| 120 | + isWebchatConnect: () => false, |
| 121 | + respond: download.respond, |
| 122 | + context: {} as never, |
| 123 | + }); |
| 124 | + expect(download.calls[0]?.ok).toBe(true); |
| 125 | + expect(download.calls[0]?.payload).toMatchObject({ |
| 126 | + encoding: "base64", |
| 127 | + data: "aGVsbG8=", |
| 128 | + artifact: { id: artifactId }, |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it("resolves runId queries through the gateway run-to-session lookup", () => { |
| 133 | + hoisted.resolveSessionKeyForRun.mockReturnValue("agent:main:main"); |
| 134 | + const { calls, respond } = createResponder(); |
| 135 | + |
| 136 | + artifactsHandlers["artifacts.list"]?.({ |
| 137 | + req: { id: 1, method: "artifacts.list", params: {} }, |
| 138 | + params: { runId: "run-1" }, |
| 139 | + client: null, |
| 140 | + isWebchatConnect: () => false, |
| 141 | + respond, |
| 142 | + context: {} as never, |
| 143 | + }); |
| 144 | + |
| 145 | + expect(calls[0]?.ok).toBe(true); |
| 146 | + expect(hoisted.resolveSessionKeyForRun).toHaveBeenCalledWith("run-1"); |
| 147 | + const payload = calls[0]?.payload as { artifacts?: Array<Record<string, unknown>> }; |
| 148 | + expect(payload.artifacts?.[0]).toMatchObject({ runId: "run-1" }); |
| 149 | + }); |
| 150 | + |
| 151 | + it("returns typed errors for missing query scope and missing artifacts", () => { |
| 152 | + const missingScope = createResponder(); |
| 153 | + artifactsHandlers["artifacts.list"]?.({ |
| 154 | + req: { id: 1, method: "artifacts.list", params: {} }, |
| 155 | + params: {}, |
| 156 | + client: null, |
| 157 | + isWebchatConnect: () => false, |
| 158 | + respond: missingScope.respond, |
| 159 | + context: {} as never, |
| 160 | + }); |
| 161 | + expect(missingScope.calls[0]?.ok).toBe(false); |
| 162 | + expect(missingScope.calls[0]?.error).toMatchObject({ |
| 163 | + details: { type: "artifact_query_unsupported" }, |
| 164 | + }); |
| 165 | + |
| 166 | + const notFound = createResponder(); |
| 167 | + artifactsHandlers["artifacts.get"]?.({ |
| 168 | + req: { id: 1, method: "artifacts.get", params: {} }, |
| 169 | + params: { sessionKey: "agent:main:main", artifactId: "artifact_missing" }, |
| 170 | + client: null, |
| 171 | + isWebchatConnect: () => false, |
| 172 | + respond: notFound.respond, |
| 173 | + context: {} as never, |
| 174 | + }); |
| 175 | + expect(notFound.calls[0]?.ok).toBe(false); |
| 176 | + expect(notFound.calls[0]?.error).toMatchObject({ |
| 177 | + details: { type: "artifact_not_found", artifactId: "artifact_missing" }, |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments