forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_fs_adapter.test.ts
More file actions
279 lines (249 loc) · 10.9 KB
/
Copy pathsession_fs_adapter.test.ts
File metadata and controls
279 lines (249 loc) · 10.9 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { MemoryProvider } from "@platformatic/vfs";
import { describe, expect, it } from "vitest";
import { createSessionFsAdapter, type SessionFsProvider } from "../src/index.js";
describe("SessionFsAdapter", () => {
it("should map all sessionFs handler operations", async () => {
const memoryProvider = new MemoryProvider();
const sessionId = "handler-session";
const sp = (path: string) => `/${sessionId}${path.startsWith("/") ? path : "/" + path}`;
const provider: SessionFsProvider = {
async readFile(path) {
return (await memoryProvider.readFile(sp(path), "utf8")) as string;
},
async writeFile(path, content) {
await memoryProvider.writeFile(sp(path), content);
},
async appendFile(path, content) {
await memoryProvider.appendFile(sp(path), content);
},
async exists(path) {
return memoryProvider.exists(sp(path));
},
async stat(path) {
const st = await memoryProvider.stat(sp(path));
return {
isFile: st.isFile(),
isDirectory: st.isDirectory(),
size: st.size,
mtime: new Date(st.mtimeMs).toISOString(),
birthtime: new Date(st.birthtimeMs).toISOString(),
};
},
async mkdir(path, recursive, mode) {
await memoryProvider.mkdir(sp(path), { recursive, mode });
},
async readdir(path) {
return (await memoryProvider.readdir(sp(path))) as string[];
},
async readdirWithTypes(path) {
const names = (await memoryProvider.readdir(sp(path))) as string[];
return Promise.all(
names.map(async (name) => {
const st = await memoryProvider.stat(sp(`${path}/${name}`));
return {
name,
type: st.isDirectory() ? ("directory" as const) : ("file" as const),
};
})
);
},
async rm(path) {
await memoryProvider.unlink(sp(path));
},
async rename(src, dest) {
await memoryProvider.rename(sp(src), sp(dest));
},
sqlite: {
async query(queryType, query, params) {
return {
columns: ["sessionId", "query", "queryType", "answer"],
rows: [{ sessionId, query, queryType, answer: params?.answer }],
rowsAffected: 0,
};
},
async transaction(statements) {
return statements.map((statement) => ({
columns: ["sessionId", "query", "queryType", "answer"],
rows: [
{
sessionId,
query: statement.query,
queryType: statement.queryType,
answer: statement.params?.answer,
},
],
rowsAffected: 0,
}));
},
async exists() {
return true;
},
},
};
const handler = createSessionFsAdapter(provider);
const mkdirError = await handler.mkdir({
sessionId,
path: "/workspace/nested",
recursive: true,
});
expect(mkdirError).toBeUndefined();
const writeError = await handler.writeFile({
sessionId,
path: "/workspace/nested/file.txt",
content: "hello",
});
expect(writeError).toBeUndefined();
const appendError = await handler.appendFile({
sessionId,
path: "/workspace/nested/file.txt",
content: " world",
});
expect(appendError).toBeUndefined();
const exists = await handler.exists({ sessionId, path: "/workspace/nested/file.txt" });
expect(exists.exists).toBe(true);
const stat = await handler.stat({ sessionId, path: "/workspace/nested/file.txt" });
expect(stat.isFile).toBe(true);
expect(stat.isDirectory).toBe(false);
expect(stat.size).toBe("hello world".length);
expect(stat.error).toBeUndefined();
const content = await handler.readFile({
sessionId,
path: "/workspace/nested/file.txt",
});
expect(content.content).toBe("hello world");
expect(content.error).toBeUndefined();
const entries = await handler.readdir({ sessionId, path: "/workspace/nested" });
expect(entries.entries).toContain("file.txt");
expect(entries.error).toBeUndefined();
const typedEntries = await handler.readdirWithTypes({
sessionId,
path: "/workspace/nested",
});
expect(
typedEntries.entries.some((entry) => entry.name === "file.txt" && entry.type === "file")
).toBe(true);
expect(typedEntries.error).toBeUndefined();
const renameError = await handler.rename({
sessionId,
src: "/workspace/nested/file.txt",
dest: "/workspace/nested/renamed.txt",
});
expect(renameError).toBeUndefined();
const oldPath = await handler.exists({
sessionId,
path: "/workspace/nested/file.txt",
});
expect(oldPath.exists).toBe(false);
const renamedPath = await handler.readFile({
sessionId,
path: "/workspace/nested/renamed.txt",
});
expect(renamedPath.content).toBe("hello world");
const rmError = await handler.rm({
sessionId,
path: "/workspace/nested/renamed.txt",
});
expect(rmError).toBeUndefined();
const removed = await handler.exists({
sessionId,
path: "/workspace/nested/renamed.txt",
});
expect(removed.exists).toBe(false);
const missing = await handler.stat({
sessionId,
path: "/workspace/nested/missing.txt",
});
expect(missing.error?.code).toBe("ENOENT");
const sqliteResult = await handler.sqliteQuery({
sessionId,
query: "select :answer as answer",
queryType: "query",
params: { answer: 42 },
});
expect(sqliteResult.columns).toContain("answer");
expect(sqliteResult.rows[0]).toMatchObject({
sessionId,
query: "select :answer as answer",
queryType: "query",
answer: 42,
});
expect(sqliteResult.rowsAffected).toBe(0);
expect(sqliteResult.error).toBeUndefined();
const sqliteExists = await handler.sqliteExists({ sessionId });
expect(sqliteExists.exists).toBe(true);
});
it("converts provider exceptions to rpc errors", async () => {
function makeError(message: string, code?: string): Error {
const err = new Error(message) as Error & { code?: string };
if (code) {
err.code = code;
}
return err;
}
function makeThrowingProvider(error: Error): SessionFsProvider {
return {
readFile: () => Promise.reject(error),
writeFile: () => Promise.reject(error),
appendFile: () => Promise.reject(error),
exists: () => Promise.reject(error),
stat: () => Promise.reject(error),
mkdir: () => Promise.reject(error),
readdir: () => Promise.reject(error),
readdirWithTypes: () => Promise.reject(error),
rm: () => Promise.reject(error),
rename: () => Promise.reject(error),
sqlite: {
query: () => Promise.reject(error),
transaction: () => Promise.reject(error),
exists: () => Promise.reject(error),
},
};
}
const enoent = makeError("missing file", "ENOENT");
const handler = createSessionFsAdapter(makeThrowingProvider(enoent));
const sessionId = "throw-session";
function assertEnoent(error: { code: string; message: string } | undefined) {
expect(error).toBeDefined();
expect(error!.code).toBe("ENOENT");
expect(error!.message.toLowerCase()).toContain("missing");
}
assertEnoent((await handler.readFile({ sessionId, path: "missing.txt" })).error);
assertEnoent(
await handler.writeFile({ sessionId, path: "missing.txt", content: "content" })
);
assertEnoent(
await handler.appendFile({ sessionId, path: "missing.txt", content: "content" })
);
const exists = await handler.exists({ sessionId, path: "missing.txt" });
expect(exists.exists).toBe(false);
assertEnoent((await handler.stat({ sessionId, path: "missing.txt" })).error);
assertEnoent(await handler.mkdir({ sessionId, path: "missing-dir" }));
assertEnoent((await handler.readdir({ sessionId, path: "missing-dir" })).error);
assertEnoent((await handler.readdirWithTypes({ sessionId, path: "missing-dir" })).error);
assertEnoent(await handler.rm({ sessionId, path: "missing.txt" }));
assertEnoent(await handler.rename({ sessionId, src: "missing.txt", dest: "dest.txt" }));
// sqlite methods let errors propagate (no try/catch wrapping)
await expect(
handler.sqliteQuery({ sessionId, query: "select 1", queryType: "query" })
).rejects.toThrow("missing file");
await expect(handler.sqliteExists({ sessionId })).rejects.toThrow("missing file");
// sqliteTransaction reports a classified result-level error instead
const transaction = await handler.sqliteTransaction({
sessionId,
statements: [{ query: "select 1", queryType: "query" }],
});
expect(transaction.results).toEqual([]);
expect(transaction.error).toEqual({ errorClass: "fatal", message: "missing file" });
const unknownProvider = createSessionFsAdapter(makeThrowingProvider(makeError("bad path")));
const unknownError = await unknownProvider.writeFile({
sessionId,
path: "bad.txt",
content: "content",
});
expect(unknownError).toBeDefined();
expect(unknownError!.code).toBe("UNKNOWN");
});
});