-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathcat_agent_opfs.test.ts
More file actions
190 lines (158 loc) · 7.83 KB
/
Copy pathcat_agent_opfs.test.ts
File metadata and controls
190 lines (158 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, expect, it, vi } from "vitest";
import type { OPFSApiRequest } from "@App/app/service/agent/core/types";
// 直接导入以触发装饰器注册
import CATAgentOPFSApi from "./cat_agent_opfs";
import { GMContextApiGet } from "./gm_context";
describe.concurrent("CATAgentOPFSApi", () => {
it.concurrent("装饰器注册了 write/read/list/delete/readAttachment 五个方法到 CAT.agent.opfs grant", () => {
void CATAgentOPFSApi;
const apis = GMContextApiGet("CAT.agent.opfs");
expect(apis).toBeDefined();
const fnKeys = apis!.map((a) => a.fnKey);
expect(fnKeys).toContain("CAT.agent.opfs.write");
expect(fnKeys).toContain("CAT.agent.opfs.read");
expect(fnKeys).toContain("CAT.agent.opfs.list");
expect(fnKeys).toContain("CAT.agent.opfs.delete");
expect(fnKeys).toContain("CAT.agent.opfs.readAttachment");
});
it.concurrent("write 方法调用 sendMessage 并传递正确的请求", async () => {
const mockSendMessage = vi.fn().mockResolvedValue({ path: "hello.txt", size: 5 });
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const writeApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.write")!;
const result = await writeApi.api.call(ctx, "hello.txt", "Hello");
expect(mockSendMessage).toHaveBeenCalledWith("CAT_agentOPFS", [
{ action: "write", path: "hello.txt", content: "Hello", scriptUuid: "test-uuid" } as OPFSApiRequest,
]);
expect(result).toEqual({ path: "hello.txt", size: 5 });
});
it.concurrent("read 方法传递 path 参数", async () => {
const mockSendMessage = vi.fn().mockResolvedValue({ path: "f.txt", content: "data", size: 4 });
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const readApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.read")!;
const result = await readApi.api.call(ctx, "f.txt");
expect(mockSendMessage).toHaveBeenCalledWith("CAT_agentOPFS", [
{ action: "read", path: "f.txt", scriptUuid: "test-uuid" } as OPFSApiRequest,
]);
expect((result as any).content).toBe("data");
});
it.concurrent("list 方法可选 path 参数", async () => {
const mockSendMessage = vi.fn().mockResolvedValue([{ name: "a.txt", type: "file", size: 1 }]);
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const listApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.list")!;
// 不带 path
await listApi.api.call(ctx);
expect(mockSendMessage).toHaveBeenCalledWith("CAT_agentOPFS", [
{ action: "list", path: undefined, scriptUuid: "test-uuid" } as OPFSApiRequest,
]);
// 带 path
mockSendMessage.mockClear();
await listApi.api.call(ctx, "sub");
expect(mockSendMessage).toHaveBeenCalledWith("CAT_agentOPFS", [
{ action: "list", path: "sub", scriptUuid: "test-uuid" } as OPFSApiRequest,
]);
});
it.concurrent("delete 方法传递 path 参数", async () => {
const mockSendMessage = vi.fn().mockResolvedValue({ success: true });
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const deleteApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.delete")!;
const result = await deleteApi.api.call(ctx, "old.txt");
expect(mockSendMessage).toHaveBeenCalledWith("CAT_agentOPFS", [
{ action: "delete", path: "old.txt", scriptUuid: "test-uuid" } as OPFSApiRequest,
]);
expect(result).toEqual({ success: true });
});
it.concurrent("scriptRes 为空时使用空字符串作为 scriptUuid", async () => {
const mockSendMessage = vi.fn().mockResolvedValue([]);
const ctx = { sendMessage: mockSendMessage, scriptRes: undefined };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const listApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.list")!;
await listApi.api.call(ctx);
expect(mockSendMessage).toHaveBeenCalledWith("CAT_agentOPFS", [
{ action: "list", path: undefined, scriptUuid: "" } as OPFSApiRequest,
]);
});
// ---- postMessage 通道:SW 直接返回 Blob ----
it.concurrent("readAttachment postMessage 通道直接返回 Blob", async () => {
const testBlob = new Blob(["image data"], { type: "image/png" });
const mockSendMessage = vi.fn().mockResolvedValue({
id: "att-1",
data: testBlob,
size: 10,
mimeType: "image/png",
});
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const readAttachmentApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.readAttachment")!;
const result = await readAttachmentApi.api.call(ctx, "att-1");
expect(mockSendMessage).toHaveBeenCalledTimes(1);
expect((result as any).data).toBe(testBlob);
});
it.concurrent("read blob postMessage 通道直接返回 Blob", async () => {
const testBlob = new Blob(["file data"], { type: "image/png" });
const mockSendMessage = vi.fn().mockResolvedValue({
path: "img.png",
data: testBlob,
size: 9,
mimeType: "image/png",
});
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const readApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.read")!;
const result = await readApi.api.call(ctx, "img.png", "blob");
expect(mockSendMessage).toHaveBeenCalledTimes(1);
expect((result as any).data).toBe(testBlob);
});
// ---- chrome.runtime 通道:SW 返回 blobUrl,客户端 CAT_fetchBlob 还原 ----
it.concurrent("readAttachment chrome.runtime 通道通过 CAT_fetchBlob 还原 Blob", async () => {
const testBlob = new Blob(["image data"], { type: "image/png" });
const mockSendMessage = vi.fn().mockImplementation((api: string) => {
if (api === "CAT_agentOPFS") {
return Promise.resolve({
id: "att-1",
blobUrl: "blob:chrome-extension://test/123",
size: 10,
mimeType: "image/png",
});
}
if (api === "CAT_fetchBlob") {
return Promise.resolve(testBlob);
}
return Promise.resolve(undefined);
});
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const readAttachmentApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.readAttachment")!;
const result = await readAttachmentApi.api.call(ctx, "att-1");
expect(mockSendMessage).toHaveBeenCalledWith("CAT_fetchBlob", ["blob:chrome-extension://test/123"]);
expect((result as any).data).toBe(testBlob);
expect((result as any).blobUrl).toBeUndefined();
});
it.concurrent("read blob chrome.runtime 通道通过 CAT_fetchBlob 还原 Blob", async () => {
const testBlob = new Blob(["file data"], { type: "image/png" });
const mockSendMessage = vi.fn().mockImplementation((api: string) => {
if (api === "CAT_agentOPFS") {
return Promise.resolve({
path: "img.png",
blobUrl: "blob:chrome-extension://test/456",
size: 9,
mimeType: "image/png",
});
}
if (api === "CAT_fetchBlob") {
return Promise.resolve(testBlob);
}
return Promise.resolve(undefined);
});
const ctx = { sendMessage: mockSendMessage, scriptRes: { uuid: "test-uuid" } };
const apis = GMContextApiGet("CAT.agent.opfs")!;
const readApi = apis.find((a) => a.fnKey === "CAT.agent.opfs.read")!;
const result = await readApi.api.call(ctx, "img.png", "blob");
expect(mockSendMessage).toHaveBeenCalledWith("CAT_fetchBlob", ["blob:chrome-extension://test/456"]);
expect((result as any).data).toBe(testBlob);
expect((result as any).blobUrl).toBeUndefined();
});
});