@milaboratories/pl-mcp-server 54.0.0 → 55.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -25
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +15 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +15 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +58 -8
- package/dist/project-holds.cjs.map +1 -1
- package/dist/project-holds.js +58 -8
- package/dist/project-holds.js.map +1 -1
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +40 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +40 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +48 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +21 -20
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +48 -18
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -73
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -75
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +3 -0
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +3 -0
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +272 -4
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +270 -6
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +323 -42
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +317 -46
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +183 -70
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +177 -70
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +451 -113
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +446 -118
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +56 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +51 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +98 -19
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +95 -20
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +66 -19
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +66 -20
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +15 -12
- package/src/project-holds.test.ts +179 -0
- package/src/project-holds.ts +114 -12
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +54 -1
- package/src/server-log.ts +49 -27
- package/src/server.logging.test.ts +38 -46
- package/src/server.test.ts +151 -17
- package/src/server.ts +62 -34
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -123
- package/src/tools/block-action-context.ts +3 -0
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +623 -8
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +659 -66
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +336 -105
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +807 -157
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +67 -0
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +130 -21
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +135 -25
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +77 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ImportFileHandle,
|
|
3
|
+
LocalImportFileHandle,
|
|
4
|
+
StorageEntry,
|
|
5
|
+
StorageHandle,
|
|
6
|
+
} from "@milaboratories/pl-model-common";
|
|
7
|
+
import { asProjectId, getFilePathFromHandle } from "@milaboratories/pl-model-common";
|
|
8
|
+
import * as fs from "node:fs/promises";
|
|
9
|
+
import * as os from "node:os";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
|
|
12
|
+
import {
|
|
13
|
+
clampRange,
|
|
14
|
+
decodeContent,
|
|
15
|
+
type FileBlobDriver,
|
|
16
|
+
fileHandlesResult,
|
|
17
|
+
type FileLsDriver,
|
|
18
|
+
importHandlesIn,
|
|
19
|
+
invalidNewImportHandles,
|
|
20
|
+
listFilesResult,
|
|
21
|
+
listLocalFolder,
|
|
22
|
+
matchNames,
|
|
23
|
+
pageEntries,
|
|
24
|
+
PATTERN_TIME_BUDGET_MS,
|
|
25
|
+
PatternError,
|
|
26
|
+
parseBlobHandle,
|
|
27
|
+
type ProjectHold,
|
|
28
|
+
READ_CONTENT_CHARS,
|
|
29
|
+
READ_MAX_BYTES,
|
|
30
|
+
readFileResult,
|
|
31
|
+
} from "./files";
|
|
32
|
+
import {
|
|
33
|
+
awaitImportsDone,
|
|
34
|
+
collectImports,
|
|
35
|
+
importsOf,
|
|
36
|
+
MAX_IMPORT_ENTRIES,
|
|
37
|
+
pendingUploadsOfProject,
|
|
38
|
+
unfinishedImports,
|
|
39
|
+
} from "./imports";
|
|
40
|
+
|
|
41
|
+
/** The JSON of a tool result, or its error text prefixed with `ERROR: `. */
|
|
42
|
+
function out(result: { content: { text: string }[]; isError?: boolean }): any {
|
|
43
|
+
const text = result.content[0].text;
|
|
44
|
+
return result.isError ? `ERROR: ${text}` : JSON.parse(text);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function uploadHandle(localPath: string): LocalImportFileHandle {
|
|
48
|
+
return `upload://upload/${encodeURIComponent(JSON.stringify({ localPath }))}` as LocalImportFileHandle;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function indexHandle(p: string): ImportFileHandle {
|
|
52
|
+
return `index://index/${encodeURIComponent(JSON.stringify({ storageId: "lib", path: p }))}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const LOCAL: StorageEntry = {
|
|
56
|
+
id: "local",
|
|
57
|
+
name: "Local",
|
|
58
|
+
handle: `local://local/${encodeURIComponent("/")}`,
|
|
59
|
+
initialFullPath: "/home",
|
|
60
|
+
};
|
|
61
|
+
const REMOTE: StorageEntry = {
|
|
62
|
+
id: "library",
|
|
63
|
+
name: "Library",
|
|
64
|
+
handle: "remote://library/x/1",
|
|
65
|
+
initialFullPath: "",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type RemoteEntry = Awaited<
|
|
69
|
+
ReturnType<FileLsDriver["listRemoteFilesWithFileStats"]>
|
|
70
|
+
>["entries"][number];
|
|
71
|
+
|
|
72
|
+
/** An ls driver over real local files and a fixed remote folder tree. */
|
|
73
|
+
function fakeLs(remote: Record<string, RemoteEntry[]> = {}, files: Record<string, Buffer> = {}) {
|
|
74
|
+
const listed: string[] = [];
|
|
75
|
+
const ls: FileLsDriver = {
|
|
76
|
+
getStorageList: async () => [LOCAL, REMOTE],
|
|
77
|
+
getLocalFileHandle: async (p) => uploadHandle(p),
|
|
78
|
+
getLocalFileSize: async (h) => {
|
|
79
|
+
const file = files[h];
|
|
80
|
+
if (file === undefined) throw new Error("File has changed since the handle was created.");
|
|
81
|
+
return file.length;
|
|
82
|
+
},
|
|
83
|
+
getLocalFileContent: async (h, range) => {
|
|
84
|
+
// A handle made for a path names a file on disk.
|
|
85
|
+
const file = files[h] ?? (await fs.readFile(getFilePathFromHandle(h)));
|
|
86
|
+
const from = range?.offset ?? 0;
|
|
87
|
+
return new Uint8Array(file.subarray(from, from + (range?.length ?? file.length)));
|
|
88
|
+
},
|
|
89
|
+
listRemoteFilesWithFileStats: async (_storage: StorageHandle, dir: string) => {
|
|
90
|
+
listed.push(dir);
|
|
91
|
+
return { entries: remote[dir] ?? [] };
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
return { ls, listed };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const noBlob: FileBlobDriver = {
|
|
98
|
+
getLocalPath: () => {
|
|
99
|
+
throw new Error("unused");
|
|
100
|
+
},
|
|
101
|
+
getContent: async () => {
|
|
102
|
+
throw new Error("unused");
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const signal = new AbortController().signal;
|
|
107
|
+
|
|
108
|
+
let dir: string;
|
|
109
|
+
beforeAll(async () => {
|
|
110
|
+
dir = await fs.mkdtemp(path.join(os.tmpdir(), "mcp-files-"));
|
|
111
|
+
for (const name of ["S1_R1.fastq.gz", "S1_R2.fastq.gz", "S2_R1.fastq.gz", "notes.txt"])
|
|
112
|
+
await fs.writeFile(path.join(dir, name), name);
|
|
113
|
+
await fs.mkdir(path.join(dir, "sub"));
|
|
114
|
+
await fs.writeFile(path.join(dir, "big.txt"), "a".repeat(100_000));
|
|
115
|
+
await fs.writeFile(path.join(dir, "bin.dat"), Buffer.from([1, 2, 0, 3]));
|
|
116
|
+
});
|
|
117
|
+
afterAll(async () => {
|
|
118
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("pageEntries", () => {
|
|
122
|
+
const entries = ["c", "a", "b", "d", "e"].map((name) => ({
|
|
123
|
+
name,
|
|
124
|
+
type: name === "d" ? ("dir" as const) : ("file" as const),
|
|
125
|
+
}));
|
|
126
|
+
|
|
127
|
+
test("sorts by name and pages", async () => {
|
|
128
|
+
const first = await pageEntries(entries, { offset: 0, limit: 2 });
|
|
129
|
+
expect(first.page.map((e) => e.name)).toEqual(["a", "b"]);
|
|
130
|
+
expect(first).toMatchObject({ total: 5, hasMore: true, nextOffset: 2 });
|
|
131
|
+
const last = await pageEntries(entries, { offset: 4, limit: 2 });
|
|
132
|
+
expect(last.page.map((e) => e.name)).toEqual(["e"]);
|
|
133
|
+
expect(last.hasMore).toBe(false);
|
|
134
|
+
expect(last.nextOffset).toBeUndefined();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("filters by pattern and type before paging", async () => {
|
|
138
|
+
const page = await pageEntries(entries, {
|
|
139
|
+
pattern: "^[a-c]$",
|
|
140
|
+
type: "file",
|
|
141
|
+
offset: 0,
|
|
142
|
+
limit: 10,
|
|
143
|
+
});
|
|
144
|
+
expect(page.page.map((e) => e.name)).toEqual(["a", "b", "c"]);
|
|
145
|
+
expect((await pageEntries(entries, { type: "dir", offset: 0, limit: 10 })).total).toBe(1);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("an invalid pattern is reported", async () => {
|
|
149
|
+
await expect(matchNames("(", ["a"])).rejects.toThrow(
|
|
150
|
+
/^`pattern` is not a valid regular expression: SyntaxError/,
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("a catastrophic pattern is stopped by the time budget", async () => {
|
|
155
|
+
const names = ["a".repeat(30) + "!", "aaa"];
|
|
156
|
+
const started = Date.now();
|
|
157
|
+
const err = await matchNames("^(a+)+$", names).catch((e: unknown) => e);
|
|
158
|
+
const elapsed = Date.now() - started;
|
|
159
|
+
expect(err).toBeInstanceOf(PatternError);
|
|
160
|
+
expect((err as PatternError).message).toMatch(/took longer than 500 ms to match 2 names/);
|
|
161
|
+
expect(elapsed).toBeGreaterThanOrEqual(PATTERN_TIME_BUDGET_MS);
|
|
162
|
+
expect(elapsed).toBeLessThan(PATTERN_TIME_BUDGET_MS + 1000);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe("list_files", () => {
|
|
167
|
+
test("lists a local folder with a regexp, sizes on the page only", async () => {
|
|
168
|
+
const page = await listLocalFolder(dir, {
|
|
169
|
+
pattern: "_R1\\.fastq\\.gz$",
|
|
170
|
+
offset: 0,
|
|
171
|
+
limit: 1,
|
|
172
|
+
});
|
|
173
|
+
expect(page.total).toBe(2);
|
|
174
|
+
expect(page.entries).toEqual([
|
|
175
|
+
{ name: "S1_R1.fastq.gz", type: "file", path: path.join(dir, "S1_R1.fastq.gz"), size: 14 },
|
|
176
|
+
]);
|
|
177
|
+
expect(page).toMatchObject({ hasMore: true, nextOffset: 1 });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("folders carry no size", async () => {
|
|
181
|
+
const page = await listLocalFolder(dir, { type: "dir", offset: 0, limit: 10 });
|
|
182
|
+
expect(page.entries).toEqual([{ name: "sub", type: "dir", path: path.join(dir, "sub") }]);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("no arguments: the storages", async () => {
|
|
186
|
+
const { ls } = fakeLs();
|
|
187
|
+
expect(out(await listFilesResult(ls, {}))).toEqual({
|
|
188
|
+
storages: [
|
|
189
|
+
{ id: "local", name: "Local", kind: "local", initialPath: "/home" },
|
|
190
|
+
{ id: "library", name: "Library", kind: "remote", initialPath: "" },
|
|
191
|
+
],
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("a local storage path; `path` alone is this computer", async () => {
|
|
196
|
+
const { ls } = fakeLs();
|
|
197
|
+
const byStorage = out(await listFilesResult(ls, { storage: "local", path: dir, limit: 3 }));
|
|
198
|
+
expect(byStorage).toMatchObject({ storage: "local", path: dir, total: 7, hasMore: true });
|
|
199
|
+
expect(byStorage.entries).toHaveLength(3);
|
|
200
|
+
const byPath = out(await listFilesResult(ls, { path: dir, pattern: "^notes" }));
|
|
201
|
+
expect(byPath.entries.map((e: { name: string }) => e.name)).toEqual(["notes.txt"]);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("the page size is capped at 500", async () => {
|
|
205
|
+
const remote = {
|
|
206
|
+
big: Array.from({ length: 700 }, (_, i): RemoteEntry => ({
|
|
207
|
+
type: "file",
|
|
208
|
+
name: `f${String(i).padStart(3, "0")}`,
|
|
209
|
+
fullPath: `big/f${i}`,
|
|
210
|
+
handle: indexHandle(`big/f${i}`),
|
|
211
|
+
size: i,
|
|
212
|
+
})),
|
|
213
|
+
};
|
|
214
|
+
const { ls } = fakeLs(remote);
|
|
215
|
+
const page = out(await listFilesResult(ls, { storage: "library", path: "big", limit: 10_000 }));
|
|
216
|
+
expect(page.entries).toHaveLength(500);
|
|
217
|
+
expect(page).toMatchObject({ total: 700, hasMore: true, nextOffset: 500 });
|
|
218
|
+
expect(page.entries[1]).toEqual({ name: "f001", type: "file", path: "big/f1", size: 1 });
|
|
219
|
+
expect(page.entries[0].handle).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("unknown storage and invalid pattern are errors", async () => {
|
|
223
|
+
const { ls } = fakeLs();
|
|
224
|
+
expect(out(await listFilesResult(ls, { storage: "nope", path: "" }))).toMatch(
|
|
225
|
+
/^ERROR: No storage "nope"/,
|
|
226
|
+
);
|
|
227
|
+
expect(out(await listFilesResult(ls, { path: dir, pattern: "(" }))).toMatch(
|
|
228
|
+
/^ERROR: `pattern` is not a valid regular expression/,
|
|
229
|
+
);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("a pattern that takes too long is an error result, not a blocked server", async () => {
|
|
233
|
+
const name = "a".repeat(30) + "!";
|
|
234
|
+
const { ls } = fakeLs({
|
|
235
|
+
slow: [{ type: "file", name, fullPath: `slow/${name}`, handle: indexHandle(name), size: 1 }],
|
|
236
|
+
});
|
|
237
|
+
const slow = out(
|
|
238
|
+
await listFilesResult(ls, { storage: "library", path: "slow", pattern: "^(a+)+$" }),
|
|
239
|
+
);
|
|
240
|
+
expect(slow).toMatch(/^ERROR: `pattern` took longer than 500 ms to match 1 name\./);
|
|
241
|
+
expect(slow).toContain("Simplify it");
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe("get_file_handles", () => {
|
|
246
|
+
test("local files get handles; folders, relative and missing paths get errors", async () => {
|
|
247
|
+
const { ls } = fakeLs();
|
|
248
|
+
const file = path.join(dir, "notes.txt");
|
|
249
|
+
const result = out(
|
|
250
|
+
await fileHandlesResult(ls, {
|
|
251
|
+
paths: [file, path.join(dir, "sub"), "relative.txt", path.join(dir, "missing")],
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
254
|
+
expect(result.handles).toEqual([{ path: file, handle: uploadHandle(file), size: 9 }]);
|
|
255
|
+
expect(result.errors.map((e: { error: string }) => e.error)).toEqual([
|
|
256
|
+
"Not a file.",
|
|
257
|
+
"Not an absolute path.",
|
|
258
|
+
expect.stringContaining("ENOENT"),
|
|
259
|
+
]);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test("remote files: one listing per folder, matched by path", async () => {
|
|
263
|
+
const a = indexHandle("runs/a.fastq");
|
|
264
|
+
const { ls, listed } = fakeLs({
|
|
265
|
+
runs: [
|
|
266
|
+
{ type: "file", name: "a.fastq", fullPath: "runs/a.fastq", handle: a, size: 5 },
|
|
267
|
+
{ type: "dir", name: "sub", fullPath: "runs/sub", size: 0 },
|
|
268
|
+
],
|
|
269
|
+
});
|
|
270
|
+
const result = out(
|
|
271
|
+
await fileHandlesResult(ls, {
|
|
272
|
+
storage: "library",
|
|
273
|
+
paths: ["runs/a.fastq", "/runs/a.fastq", "runs/sub", "runs/none"],
|
|
274
|
+
}),
|
|
275
|
+
);
|
|
276
|
+
expect(listed).toEqual(["runs"]);
|
|
277
|
+
expect(result.handles).toEqual([
|
|
278
|
+
{ path: "runs/a.fastq", handle: a, size: 5 },
|
|
279
|
+
{ path: "/runs/a.fastq", handle: a, size: 5 },
|
|
280
|
+
]);
|
|
281
|
+
expect(result.errors).toEqual([
|
|
282
|
+
{ path: "runs/sub", error: "Not a file." },
|
|
283
|
+
{ path: "runs/none", error: "No such file in the storage." },
|
|
284
|
+
]);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test("at most 200 paths per call", async () => {
|
|
288
|
+
const { ls } = fakeLs();
|
|
289
|
+
const many = Array.from({ length: 201 }, (_, i) => `/x/${i}`);
|
|
290
|
+
expect(out(await fileHandlesResult(ls, { paths: many }))).toMatch(/at most 200 per call/);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
describe("read_file", () => {
|
|
295
|
+
test("reads 16384 bytes by default, with the next offset", async () => {
|
|
296
|
+
const { ls } = fakeLs();
|
|
297
|
+
const r = out(await readFileResult(ls, noBlob, { path: path.join(dir, "big.txt") }, signal));
|
|
298
|
+
expect(r).toMatchObject({
|
|
299
|
+
source: "local",
|
|
300
|
+
size: 100_000,
|
|
301
|
+
offset: 0,
|
|
302
|
+
bytesRead: 16_384,
|
|
303
|
+
eof: false,
|
|
304
|
+
nextOffset: 16_384,
|
|
305
|
+
encoding: "utf-8",
|
|
306
|
+
});
|
|
307
|
+
expect(r.text).toHaveLength(16_384);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("a range past the end is cut to it: eof, no error", async () => {
|
|
311
|
+
const { ls } = fakeLs();
|
|
312
|
+
const file = path.join(dir, "notes.txt");
|
|
313
|
+
expect(
|
|
314
|
+
out(await readFileResult(ls, noBlob, { path: file, offset: 5, length: 100 }, signal)),
|
|
315
|
+
).toEqual({
|
|
316
|
+
source: "local",
|
|
317
|
+
size: 9,
|
|
318
|
+
offset: 5,
|
|
319
|
+
bytesRead: 4,
|
|
320
|
+
eof: true,
|
|
321
|
+
encoding: "utf-8",
|
|
322
|
+
text: ".txt",
|
|
323
|
+
});
|
|
324
|
+
const past = out(await readFileResult(ls, noBlob, { path: file, offset: 50 }, signal));
|
|
325
|
+
expect(past).toMatchObject({ offset: 9, bytesRead: 0, eof: true, text: "" });
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
test("the length is capped, and the text cut to fit the result", async () => {
|
|
329
|
+
const { ls } = fakeLs();
|
|
330
|
+
const r = out(
|
|
331
|
+
await readFileResult(
|
|
332
|
+
ls,
|
|
333
|
+
noBlob,
|
|
334
|
+
{ path: path.join(dir, "big.txt"), length: 1_000_000 },
|
|
335
|
+
signal,
|
|
336
|
+
),
|
|
337
|
+
);
|
|
338
|
+
expect(r.bytesRead).toBeLessThanOrEqual(READ_MAX_BYTES);
|
|
339
|
+
expect(r.bytesRead).toBe(READ_CONTENT_CHARS - 2);
|
|
340
|
+
expect(r.nextOffset).toBe(r.bytesRead);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("a transform reads up to 4 MiB and returns its value only", async () => {
|
|
344
|
+
const { ls } = fakeLs();
|
|
345
|
+
const r = out(
|
|
346
|
+
await readFileResult(
|
|
347
|
+
ls,
|
|
348
|
+
noBlob,
|
|
349
|
+
{ path: path.join(dir, "big.txt"), length: 1_000_000, transform: "text.length" },
|
|
350
|
+
signal,
|
|
351
|
+
),
|
|
352
|
+
);
|
|
353
|
+
expect(r).toMatchObject({ bytesRead: 100_000, eof: true, result: 100_000 });
|
|
354
|
+
expect(r.text).toBeUndefined();
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("binary content: byte count, base64 on request", async () => {
|
|
358
|
+
const { ls } = fakeLs();
|
|
359
|
+
const file = path.join(dir, "bin.dat");
|
|
360
|
+
const plain = out(await readFileResult(ls, noBlob, { path: file }, signal));
|
|
361
|
+
expect(plain).toMatchObject({ encoding: "binary", bytesRead: 4 });
|
|
362
|
+
expect(plain.base64).toBeUndefined();
|
|
363
|
+
const b64 = out(await readFileResult(ls, noBlob, { path: file, base64: true }, signal));
|
|
364
|
+
expect(Buffer.from(b64.base64, "base64")).toEqual(Buffer.from([1, 2, 0, 3]));
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test("leading bytes of a file are never dropped", async () => {
|
|
368
|
+
const h = uploadHandle("/data/u.bin");
|
|
369
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from([0x80, 0x41]) });
|
|
370
|
+
const text = out(await readFileResult(ls, noBlob, { handle: h }, signal));
|
|
371
|
+
expect(text).toEqual({
|
|
372
|
+
source: "upload",
|
|
373
|
+
size: 2,
|
|
374
|
+
offset: 0,
|
|
375
|
+
bytesRead: 2,
|
|
376
|
+
eof: true,
|
|
377
|
+
encoding: "binary",
|
|
378
|
+
});
|
|
379
|
+
const b64 = out(await readFileResult(ls, noBlob, { handle: h, base64: true }, signal));
|
|
380
|
+
expect(b64).toMatchObject({ offset: 0, bytesRead: 2, eof: true, encoding: "binary" });
|
|
381
|
+
expect(Buffer.from(b64.base64, "base64")).toEqual(Buffer.from([0x80, 0x41]));
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
test("a base64 read starts exactly at its offset, inside a character or not", async () => {
|
|
385
|
+
const h = uploadHandle("/data/u.txt");
|
|
386
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from("aé z") }); // 61 c3 a9 20 7a
|
|
387
|
+
const r = out(await readFileResult(ls, noBlob, { handle: h, offset: 2, base64: true }, signal));
|
|
388
|
+
expect(r).toMatchObject({ offset: 2, bytesRead: 3, eof: true, encoding: "binary" });
|
|
389
|
+
expect(Buffer.from(r.base64, "base64")).toEqual(Buffer.from([0xa9, 0x20, 0x7a]));
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
test("a base64 read takes the length it asks for, down to 1 byte", async () => {
|
|
393
|
+
const h = uploadHandle("/data/u.bin");
|
|
394
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from([0x80, 0x81, 0x82, 0x83, 0x84, 0x85]) });
|
|
395
|
+
const r = out(
|
|
396
|
+
await readFileResult(ls, noBlob, { handle: h, offset: 2, length: 1, base64: true }, signal),
|
|
397
|
+
);
|
|
398
|
+
expect(r).toMatchObject({ offset: 2, bytesRead: 1, nextOffset: 3, encoding: "binary" });
|
|
399
|
+
expect(Buffer.from(r.base64, "base64")).toEqual(Buffer.from([0x82]));
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test("text reads of the shortest length walk the file to its end in whole characters", async () => {
|
|
403
|
+
const h = uploadHandle("/data/u.txt");
|
|
404
|
+
const content = Buffer.from("aé z😀!");
|
|
405
|
+
const { ls } = fakeLs({}, { [h]: content });
|
|
406
|
+
const seen: { offset: number; text: string }[] = [];
|
|
407
|
+
let offset: number | undefined = 0;
|
|
408
|
+
while (offset !== undefined) {
|
|
409
|
+
const r = out(await readFileResult(ls, noBlob, { handle: h, offset, length: 1 }, signal));
|
|
410
|
+
expect(r.encoding).toBe("utf-8");
|
|
411
|
+
expect(r.nextOffset === undefined || r.nextOffset > offset).toBe(true);
|
|
412
|
+
seen.push({ offset: r.offset, text: r.text });
|
|
413
|
+
offset = r.nextOffset;
|
|
414
|
+
}
|
|
415
|
+
expect(seen.map((s) => s.text).join("")).toBe("aé z😀!");
|
|
416
|
+
// A length of 1 reads 4 bytes, the longest character; one cut at the end is left for the next.
|
|
417
|
+
expect(seen.map((s) => s.offset)).toEqual([0, 4, 5, 9]);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("a read that starts inside a character reports where it really started", async () => {
|
|
421
|
+
const h = uploadHandle("/data/u.txt");
|
|
422
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from("aé z") });
|
|
423
|
+
const r = out(await readFileResult(ls, noBlob, { handle: h, offset: 2, length: 1 }, signal));
|
|
424
|
+
expect(r).toMatchObject({ offset: 3, bytesRead: 2, text: " z", eof: true });
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
test("an upload:// handle is read through the ls driver", async () => {
|
|
428
|
+
const h = uploadHandle("/data/a.csv");
|
|
429
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from("a,b\n1,2\n") });
|
|
430
|
+
const r = out(await readFileResult(ls, noBlob, { handle: h, offset: 4 }, signal));
|
|
431
|
+
expect(r).toMatchObject({ source: "upload", size: 8, offset: 4, text: "1,2\n", eof: true });
|
|
432
|
+
const changed = uploadHandle("/data/changed.csv");
|
|
433
|
+
expect(out(await readFileResult(ls, noBlob, { handle: changed }, signal))).toMatch(
|
|
434
|
+
/^ERROR: Cannot read the handle: File has changed/,
|
|
435
|
+
);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
test("a local read that never ends returns once the signal aborts", async () => {
|
|
439
|
+
const h = uploadHandle("/mnt/hung/a.csv");
|
|
440
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from("a,b\n") });
|
|
441
|
+
let asked = false;
|
|
442
|
+
ls.getLocalFileContent = () => {
|
|
443
|
+
asked = true;
|
|
444
|
+
return new Promise<Uint8Array>(() => {});
|
|
445
|
+
};
|
|
446
|
+
const controller = new AbortController();
|
|
447
|
+
const pending = readFileResult(ls, noBlob, { handle: h }, controller.signal);
|
|
448
|
+
await vi.waitFor(() => expect(asked).toBe(true));
|
|
449
|
+
controller.abort();
|
|
450
|
+
const r = out(await pending);
|
|
451
|
+
expect(r).toMatch(/^ERROR: Read failed: /);
|
|
452
|
+
expect(r).toContain("took too long or was cancelled");
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
test("a local read is not started once the signal has aborted", async () => {
|
|
456
|
+
const h = uploadHandle("/mnt/slow/a.csv");
|
|
457
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from("a,b\n") });
|
|
458
|
+
const controller = new AbortController();
|
|
459
|
+
ls.getLocalFileSize = async () => {
|
|
460
|
+
controller.abort();
|
|
461
|
+
return 4;
|
|
462
|
+
};
|
|
463
|
+
let asked = false;
|
|
464
|
+
ls.getLocalFileContent = async () => {
|
|
465
|
+
asked = true;
|
|
466
|
+
return new Uint8Array(0);
|
|
467
|
+
};
|
|
468
|
+
const r = out(await readFileResult(ls, noBlob, { handle: h }, controller.signal));
|
|
469
|
+
expect(r).toMatch(/^ERROR: Read failed: /);
|
|
470
|
+
expect(r).toContain("took too long or was cancelled");
|
|
471
|
+
expect(asked).toBe(false);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test("a cancel while the handle resolves leaves no unhandled rejection", async () => {
|
|
475
|
+
const h = uploadHandle("/mnt/gone/b.csv");
|
|
476
|
+
const { ls } = fakeLs({}, { [h]: Buffer.from("a,b\n") });
|
|
477
|
+
const controller = new AbortController();
|
|
478
|
+
ls.getLocalFileSize = async () => {
|
|
479
|
+
controller.abort();
|
|
480
|
+
return 4;
|
|
481
|
+
};
|
|
482
|
+
ls.getLocalFileContent = async () => {
|
|
483
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
484
|
+
throw new Error("disk gone");
|
|
485
|
+
};
|
|
486
|
+
const unhandled = vi.fn();
|
|
487
|
+
process.on("unhandledRejection", unhandled);
|
|
488
|
+
try {
|
|
489
|
+
const r = out(await readFileResult(ls, noBlob, { handle: h }, controller.signal));
|
|
490
|
+
expect(r).toMatch(/^ERROR: Read failed: /);
|
|
491
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
492
|
+
expect(unhandled).not.toHaveBeenCalled();
|
|
493
|
+
} finally {
|
|
494
|
+
process.off("unhandledRejection", unhandled);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
test("an index:// handle is refused with the way to its content", async () => {
|
|
499
|
+
const { ls } = fakeLs();
|
|
500
|
+
const r = out(await readFileResult(ls, noBlob, { handle: indexHandle("x") }, signal));
|
|
501
|
+
expect(r).toMatch(/^ERROR: An index:\/\/ handle/);
|
|
502
|
+
expect(r).toContain("pre-run");
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
test("a remote blob is read in a range clamped to the size in its handle", async () => {
|
|
506
|
+
const asked: { from: number; to: number }[] = [];
|
|
507
|
+
const blob: FileBlobDriver = {
|
|
508
|
+
getLocalPath: () => "",
|
|
509
|
+
getContent: async (_h, { range }) => {
|
|
510
|
+
if (range === undefined) throw new Error("no range");
|
|
511
|
+
asked.push(range);
|
|
512
|
+
return new Uint8Array(range.to - range.from).fill(65);
|
|
513
|
+
},
|
|
514
|
+
};
|
|
515
|
+
const { ls } = fakeLs();
|
|
516
|
+
const handle = "blob+remote://download/PlBlob/1/42/sig/10#signature";
|
|
517
|
+
const r = out(await readFileResult(ls, blob, { handle, offset: 6, length: 100 }, signal));
|
|
518
|
+
expect(asked).toEqual([{ from: 6, to: 10 }]);
|
|
519
|
+
expect(r).toMatchObject({ source: "blob", size: 10, bytesRead: 4, eof: true, text: "AAAA" });
|
|
520
|
+
// Past the end: the driver is not asked at all (a remote blob answers 416 there).
|
|
521
|
+
out(await readFileResult(ls, blob, { handle, offset: 10 }, signal));
|
|
522
|
+
expect(asked).toHaveLength(1);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
test("a local blob's size comes from its file", async () => {
|
|
526
|
+
const file = path.join(dir, "notes.txt");
|
|
527
|
+
const blob: FileBlobDriver = {
|
|
528
|
+
getLocalPath: () => file,
|
|
529
|
+
getContent: async (_h, { range }) =>
|
|
530
|
+
new Uint8Array(Buffer.from("notes.txt").subarray(range?.from, range?.to)),
|
|
531
|
+
};
|
|
532
|
+
const { ls } = fakeLs();
|
|
533
|
+
const r = out(
|
|
534
|
+
await readFileResult(ls, blob, { handle: "blob+local://download/x#sig" }, signal),
|
|
535
|
+
);
|
|
536
|
+
expect(r).toMatchObject({ source: "blob", size: 9, text: "notes.txt" });
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
test("a blob is read holding the project that gave it; a path or an upload is not", async () => {
|
|
540
|
+
const holds: string[] = [];
|
|
541
|
+
let holding = false;
|
|
542
|
+
const hold: ProjectHold = async (projectId, read) => {
|
|
543
|
+
holds.push(projectId);
|
|
544
|
+
holding = true;
|
|
545
|
+
try {
|
|
546
|
+
return await read();
|
|
547
|
+
} finally {
|
|
548
|
+
holding = false;
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
const blob: FileBlobDriver = {
|
|
552
|
+
getLocalPath: () => "",
|
|
553
|
+
getContent: async (_h, { range }) => {
|
|
554
|
+
// The whole read, every range of it, happens inside the hold.
|
|
555
|
+
if (!holding) throw new Error("read outside the hold");
|
|
556
|
+
return new Uint8Array((range?.to ?? 0) - (range?.from ?? 0)).fill(66);
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
const { ls } = fakeLs();
|
|
560
|
+
const handle = "blob+remote://download/PlBlob/1/42/sig/10#signature";
|
|
561
|
+
|
|
562
|
+
const projectId = asProjectId("p1");
|
|
563
|
+
const r = out(await readFileResult(ls, blob, { handle, projectId, length: 4 }, signal, hold));
|
|
564
|
+
expect(r).toMatchObject({ source: "blob", bytesRead: 4, text: "BBBB" });
|
|
565
|
+
out(await readFileResult(ls, noBlob, { path: path.join(dir, "notes.txt") }, signal, hold));
|
|
566
|
+
expect(holds).toEqual([projectId]);
|
|
567
|
+
expect(out(await readFileResult(ls, blob, { handle }, signal, hold))).toMatch(
|
|
568
|
+
/^ERROR: A blob handle is read with `projectId`/,
|
|
569
|
+
);
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
test("other strings and both or neither of path and handle are errors", async () => {
|
|
573
|
+
const { ls } = fakeLs();
|
|
574
|
+
expect(out(await readFileResult(ls, noBlob, { handle: "nope" }, signal))).toMatch(
|
|
575
|
+
/^ERROR: Not a handle/,
|
|
576
|
+
);
|
|
577
|
+
expect(out(await readFileResult(ls, noBlob, {}, signal))).toMatch(/^ERROR: Give either/);
|
|
578
|
+
});
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
describe("decoding", () => {
|
|
582
|
+
test("a character cut by the range end is left for the next read, unless the file ends there", () => {
|
|
583
|
+
const bytes = Buffer.from("aé z").subarray(0, 2); // 61 c3
|
|
584
|
+
expect(decodeContent(bytes)).toEqual({
|
|
585
|
+
encoding: "utf-8",
|
|
586
|
+
text: "a",
|
|
587
|
+
skippedBytes: 0,
|
|
588
|
+
usedBytes: 1,
|
|
589
|
+
});
|
|
590
|
+
expect(decodeContent(bytes, { atEnd: true })).toEqual({ encoding: "binary", usedBytes: 2 });
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
test("a range that starts inside a character starts after it", () => {
|
|
594
|
+
const bytes = Buffer.from("é z"); // c3 a9 20 7a
|
|
595
|
+
expect(decodeContent(bytes.subarray(1), { skipLead: true, atEnd: true })).toEqual({
|
|
596
|
+
encoding: "utf-8",
|
|
597
|
+
text: " z",
|
|
598
|
+
skippedBytes: 1,
|
|
599
|
+
usedBytes: 2,
|
|
600
|
+
});
|
|
601
|
+
const emoji = Buffer.from("😀x"); // f0 9f 98 80 78
|
|
602
|
+
expect(decodeContent(emoji.subarray(1), { skipLead: true })).toMatchObject({
|
|
603
|
+
text: "x",
|
|
604
|
+
skippedBytes: 3,
|
|
605
|
+
});
|
|
606
|
+
// At the start of the file nothing is skipped: not UTF-8, so binary.
|
|
607
|
+
expect(decodeContent(Buffer.from([0x80, 0x41]))).toEqual({ encoding: "binary", usedBytes: 2 });
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
test("NUL bytes or invalid UTF-8 make it binary", () => {
|
|
611
|
+
expect(decodeContent(Buffer.from([0x61, 0])).encoding).toBe("binary");
|
|
612
|
+
expect(decodeContent(Buffer.from([0xff, 0xfe, 0x61]))).toEqual({
|
|
613
|
+
encoding: "binary",
|
|
614
|
+
usedBytes: 3,
|
|
615
|
+
});
|
|
616
|
+
// An invalid sequence inside the range: binary, all of it.
|
|
617
|
+
expect(decodeContent(Buffer.from([0x61, 0xc3, 0x41]))).toEqual({
|
|
618
|
+
encoding: "binary",
|
|
619
|
+
usedBytes: 3,
|
|
620
|
+
});
|
|
621
|
+
// ed a0 80 is a surrogate, not a character.
|
|
622
|
+
expect(decodeContent(Buffer.from([0x61, 0xed, 0xa0, 0x80])).encoding).toBe("binary");
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
test("ranges and blob handles", () => {
|
|
626
|
+
expect(clampRange(5, 10, 8)).toEqual({ from: 5, to: 8 });
|
|
627
|
+
expect(clampRange(20, 10, 8)).toEqual({ from: 8, to: 8 });
|
|
628
|
+
expect(parseBlobHandle("blob+remote://download/T/1/2/s/123#x")).toMatchObject({
|
|
629
|
+
kind: "remote",
|
|
630
|
+
size: 123,
|
|
631
|
+
});
|
|
632
|
+
expect(parseBlobHandle("blob+local://download/p#x")?.kind).toBe("local");
|
|
633
|
+
expect(parseBlobHandle("upload://upload/x")).toBeUndefined();
|
|
634
|
+
});
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
describe("import handles in written data", () => {
|
|
638
|
+
const good = uploadHandle("/data/good.fastq");
|
|
639
|
+
const changed = uploadHandle("/data/changed.fastq");
|
|
640
|
+
const ls = { getLocalFileSize: fakeLs({}, { [good]: Buffer.from("x") }).ls.getLocalFileSize };
|
|
641
|
+
|
|
642
|
+
test("are found with their JSON paths", () => {
|
|
643
|
+
expect(
|
|
644
|
+
importHandlesIn({ a: [good], "b c": { d: indexHandle("x") }, e: "upload:/no" }).map(
|
|
645
|
+
(h) => h.path,
|
|
646
|
+
),
|
|
647
|
+
).toEqual(["a[0]", '["b c"].d']);
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
test("only new handles are checked: a stored handle with a changed file passes", async () => {
|
|
651
|
+
const current = { files: [changed] };
|
|
652
|
+
expect(await invalidNewImportHandles(ls, current, { files: [changed, good] })).toEqual([]);
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
test("a new handle whose file changed, or a broken index handle, is reported by path", async () => {
|
|
656
|
+
const invalid = await invalidNewImportHandles(
|
|
657
|
+
ls,
|
|
658
|
+
{ files: [] },
|
|
659
|
+
{ files: [good, changed], lib: "index://index/%7Bnot-json" },
|
|
660
|
+
);
|
|
661
|
+
expect(invalid).toEqual([
|
|
662
|
+
{ path: "files[1]", error: "File has changed since the handle was created." },
|
|
663
|
+
{ path: "lib", error: "The index:// handle does not parse." },
|
|
664
|
+
]);
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
describe("import progress in outputs", () => {
|
|
669
|
+
const progress = (done: boolean, extra: Record<string, unknown> = {}) => ({
|
|
670
|
+
done,
|
|
671
|
+
isUpload: true,
|
|
672
|
+
...extra,
|
|
673
|
+
});
|
|
674
|
+
const h1 = uploadHandle("/data/one.fastq");
|
|
675
|
+
const h2 = uploadHandle("/data/two.fastq");
|
|
676
|
+
|
|
677
|
+
test("found as the value, in a record keyed by handle, and in [handle, progress] pairs", () => {
|
|
678
|
+
const entries = collectImports({
|
|
679
|
+
single: { ok: true, value: progress(true) },
|
|
680
|
+
byHandle: {
|
|
681
|
+
ok: true,
|
|
682
|
+
value: {
|
|
683
|
+
[h1]: progress(false, { status: { progress: 0.5, bytesProcessed: 50, bytesTotal: 100 } }),
|
|
684
|
+
[h2]: progress(false),
|
|
685
|
+
},
|
|
686
|
+
},
|
|
687
|
+
pairs: { ok: true, value: [[h1, progress(false, { lastError: "timeout" })]] },
|
|
688
|
+
nested: { ok: true, value: { samples: { s1: { progress: progress(true) } } } },
|
|
689
|
+
failed: { ok: false, errors: [{ message: "boom" }] },
|
|
690
|
+
});
|
|
691
|
+
expect(entries.map((e) => [e.output, e.path, e.file, e.state])).toEqual([
|
|
692
|
+
["single", "(value)", undefined, "done"],
|
|
693
|
+
["byHandle", `[${JSON.stringify(h1)}]`, "one.fastq", "inProgress"],
|
|
694
|
+
["byHandle", `[${JSON.stringify(h2)}]`, "two.fastq", "notStarted"],
|
|
695
|
+
["pairs", "[0][1]", "one.fastq", "failed"],
|
|
696
|
+
["nested", '["samples"]["s1"]["progress"]', undefined, "done"],
|
|
697
|
+
]);
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
test("summary: counts, bytes, foreign uploads, pending entries failed first", () => {
|
|
701
|
+
const summary = importsOf({
|
|
702
|
+
a: {
|
|
703
|
+
ok: true,
|
|
704
|
+
value: [
|
|
705
|
+
progress(true, { status: { progress: 1, bytesProcessed: 10, bytesTotal: 10 } }),
|
|
706
|
+
progress(false, { status: { progress: 0.1, bytesProcessed: 1, bytesTotal: 10 } }),
|
|
707
|
+
progress(false, { isUploadSignMatch: false }),
|
|
708
|
+
progress(false, { lastError: "x" }),
|
|
709
|
+
],
|
|
710
|
+
},
|
|
711
|
+
});
|
|
712
|
+
expect(summary).toMatchObject({
|
|
713
|
+
total: 4,
|
|
714
|
+
done: 1,
|
|
715
|
+
inProgress: 1,
|
|
716
|
+
notStarted: 0,
|
|
717
|
+
failed: 1,
|
|
718
|
+
foreign: 1,
|
|
719
|
+
bytesProcessed: 11,
|
|
720
|
+
bytesTotal: 20,
|
|
721
|
+
});
|
|
722
|
+
expect(summary?.pending?.map((e) => e.state)).toEqual(["failed", "inProgress", "foreign"]);
|
|
723
|
+
expect(unfinishedImports(summary)).toBe(2);
|
|
724
|
+
expect(importsOf({ a: { ok: true, value: { x: 1 } } })).toBeUndefined();
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
test("at most 20 pending entries", () => {
|
|
728
|
+
const summary = importsOf({
|
|
729
|
+
a: { ok: true, value: Array.from({ length: 25 }, () => progress(false)) },
|
|
730
|
+
});
|
|
731
|
+
expect(summary?.pending).toHaveLength(MAX_IMPORT_ENTRIES);
|
|
732
|
+
expect(summary?.morePending).toBe(5);
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
test("a project's pending uploads count every block, uploads in progress or not started only", async () => {
|
|
736
|
+
const states: Record<string, { outputs?: unknown }> = {
|
|
737
|
+
// A block that never ran: its pre-run started the upload.
|
|
738
|
+
prerun: {
|
|
739
|
+
outputs: {
|
|
740
|
+
h: {
|
|
741
|
+
ok: true,
|
|
742
|
+
value: progress(false, { status: { progress: 0.1, bytesProcessed: 10 } }),
|
|
743
|
+
},
|
|
744
|
+
},
|
|
745
|
+
},
|
|
746
|
+
running: { outputs: { h: { ok: true, value: progress(false) } } },
|
|
747
|
+
other: {
|
|
748
|
+
outputs: {
|
|
749
|
+
done: { ok: true, value: progress(true, { status: { bytesProcessed: 5 } }) },
|
|
750
|
+
failed: { ok: true, value: progress(false, { lastError: "denied" }) },
|
|
751
|
+
foreign: { ok: true, value: progress(false, { isUploadSignMatch: false }) },
|
|
752
|
+
library: { ok: true, value: { done: false, isUpload: false } },
|
|
753
|
+
},
|
|
754
|
+
},
|
|
755
|
+
};
|
|
756
|
+
const read: string[] = [];
|
|
757
|
+
const pending = await pendingUploadsOfProject({
|
|
758
|
+
overview: {
|
|
759
|
+
getValue: async () => ({ blocks: [{ id: "prerun" }, { id: "running" }, { id: "other" }] }),
|
|
760
|
+
},
|
|
761
|
+
getBlockState: (id) => ({
|
|
762
|
+
getValue: async () => {
|
|
763
|
+
read.push(id);
|
|
764
|
+
return states[id];
|
|
765
|
+
},
|
|
766
|
+
}),
|
|
767
|
+
});
|
|
768
|
+
expect(pending).toEqual({ count: 2, bytesProcessed: 15 });
|
|
769
|
+
expect(read).toEqual(["prerun", "running", "other"]);
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
test("awaitImportsDone waits for the state to show them done", async () => {
|
|
773
|
+
const values = [
|
|
774
|
+
{ outputs: { h: { ok: true, value: progress(false) } } },
|
|
775
|
+
{ outputs: { h: { ok: true, value: progress(true) } } },
|
|
776
|
+
];
|
|
777
|
+
let i = 0;
|
|
778
|
+
const wait = await awaitImportsDone(
|
|
779
|
+
{
|
|
780
|
+
getFullValue: async () => ({ value: values[Math.min(i, 1)], uTag: String(i) }),
|
|
781
|
+
awaitChange: async () => {
|
|
782
|
+
i++;
|
|
783
|
+
},
|
|
784
|
+
},
|
|
785
|
+
{ timeoutMs: 5_000 },
|
|
786
|
+
);
|
|
787
|
+
expect(wait.done).toBe(true);
|
|
788
|
+
expect(wait.imports).toMatchObject({ total: 1, done: 1 });
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
test("awaitImportsDone runs out of time with what is left", async () => {
|
|
792
|
+
const wait = await awaitImportsDone(
|
|
793
|
+
{
|
|
794
|
+
getFullValue: async () => ({
|
|
795
|
+
value: { outputs: { h: { ok: true, value: progress(false) } } },
|
|
796
|
+
uTag: "1",
|
|
797
|
+
}),
|
|
798
|
+
awaitChange: (s?: AbortSignal) =>
|
|
799
|
+
new Promise<void>((_, reject) =>
|
|
800
|
+
s?.addEventListener("abort", () => reject(new Error("aborted"))),
|
|
801
|
+
),
|
|
802
|
+
},
|
|
803
|
+
{ timeoutMs: 50 },
|
|
804
|
+
);
|
|
805
|
+
expect(wait).toMatchObject({ done: false, aborted: false });
|
|
806
|
+
expect(wait.imports?.notStarted).toBe(1);
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
test("awaitImportsDone looks again every 5 s without a change, and ends on the request's abort", async () => {
|
|
810
|
+
vi.useFakeTimers();
|
|
811
|
+
try {
|
|
812
|
+
const looks: number[] = [];
|
|
813
|
+
const controller = new AbortController();
|
|
814
|
+
const pending = awaitImportsDone(
|
|
815
|
+
{
|
|
816
|
+
getFullValue: async () => ({
|
|
817
|
+
value: { outputs: { h: { ok: true, value: progress(false) } } },
|
|
818
|
+
uTag: "1",
|
|
819
|
+
}),
|
|
820
|
+
// A state that never changes: only a wake-up or the abort ends a wait.
|
|
821
|
+
awaitChange: (s?: AbortSignal) =>
|
|
822
|
+
new Promise<void>((_, reject) =>
|
|
823
|
+
s?.addEventListener("abort", () => reject(new Error("aborted"))),
|
|
824
|
+
),
|
|
825
|
+
},
|
|
826
|
+
{
|
|
827
|
+
timeoutMs: 60_000,
|
|
828
|
+
signal: controller.signal,
|
|
829
|
+
onProgress: (elapsedMs) => looks.push(elapsedMs),
|
|
830
|
+
},
|
|
831
|
+
);
|
|
832
|
+
await vi.advanceTimersByTimeAsync(12_000);
|
|
833
|
+
expect(looks).toEqual([0, 5_000, 10_000]);
|
|
834
|
+
controller.abort();
|
|
835
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
836
|
+
const wait = await pending;
|
|
837
|
+
expect(wait).toMatchObject({ done: false, aborted: true, waitedMs: 12_000 });
|
|
838
|
+
} finally {
|
|
839
|
+
vi.useRealTimers();
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
});
|