@lumeweb/sia-testing 0.0.0-stage → 0.0.2
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/LICENSE +9 -0
- package/README.md +202 -2
- package/dist/esm/client.d.ts +8 -0
- package/dist/esm/client.js +6 -0
- package/dist/esm/http.d.ts +3 -0
- package/dist/esm/http.js +3 -0
- package/dist/esm/mock.d.ts +8 -0
- package/dist/esm/mock.js +7 -0
- package/dist/esm/model/index.js +509 -0
- package/dist/esm/model/state.js +188 -0
- package/dist/esm/msw.d.ts +2 -0
- package/dist/esm/msw.js +3 -0
- package/dist/esm/node.d.ts +4 -0
- package/dist/esm/node.js +4 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/_md.js +169 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/_u64.js +12 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/sha2.js +199 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/utils.js +220 -0
- package/dist/esm/sdk/abort.js +23 -0
- package/dist/esm/sdk/control.js +129 -0
- package/dist/esm/sdk/index.js +454 -0
- package/dist/esm/sdk/link.js +55 -0
- package/dist/esm/sdk/object.js +24 -0
- package/dist/esm/sdk/payload-store.d.ts +9 -0
- package/dist/esm/sdk/payload-store.js +67 -0
- package/dist/esm/sdk/state.js +74 -0
- package/dist/esm/testing/application-client.d.ts +8 -0
- package/dist/esm/testing/application-client.js +295 -0
- package/dist/esm/testing/application-metadata.js +17 -0
- package/dist/esm/testing/application-transport.js +363 -0
- package/dist/esm/testing/auth-client.d.ts +20 -0
- package/dist/esm/testing/auth-client.js +63 -0
- package/dist/esm/testing/authentication.js +48 -0
- package/dist/esm/testing/byte-view.js +28 -0
- package/dist/esm/testing/chunked-files.d.ts +12 -0
- package/dist/esm/testing/chunked-files.js +50 -0
- package/dist/esm/testing/client.d.ts +8 -0
- package/dist/esm/testing/client.js +105 -0
- package/dist/esm/testing/controls.d.ts +44 -0
- package/dist/esm/testing/controls.js +255 -0
- package/dist/esm/testing/diagnostics.js +55 -0
- package/dist/esm/testing/files.d.ts +9 -0
- package/dist/esm/testing/files.js +86 -0
- package/dist/esm/testing/http-body.js +33 -0
- package/dist/esm/testing/http.d.ts +14 -0
- package/dist/esm/testing/http.js +181 -0
- package/dist/esm/testing/live/agent.d.ts +1 -0
- package/dist/esm/testing/live/agent.js +211 -0
- package/dist/esm/testing/live/approval.js +42 -0
- package/dist/esm/testing/live/cleanup.js +74 -0
- package/dist/esm/testing/live/journal.js +180 -0
- package/dist/esm/testing/live/lease.js +56 -0
- package/dist/esm/testing/live/private-files.js +50 -0
- package/dist/esm/testing/live/process.js +208 -0
- package/dist/esm/testing/live/profile.js +214 -0
- package/dist/esm/testing/live/recovery.js +164 -0
- package/dist/esm/testing/live/resource-limits.js +9 -0
- package/dist/esm/testing/live/run.js +390 -0
- package/dist/esm/testing/live/session.js +93 -0
- package/dist/esm/testing/live/types.d.ts +8 -0
- package/dist/esm/testing/mock.d.ts +8 -0
- package/dist/esm/testing/mock.js +1364 -0
- package/dist/esm/testing/msw.d.ts +21 -0
- package/dist/esm/testing/msw.js +61 -0
- package/dist/esm/testing/mutations.d.ts +59 -0
- package/dist/esm/testing/mutations.js +58 -0
- package/dist/esm/testing/node-payload-store.js +353 -0
- package/dist/esm/testing/node.d.ts +40 -0
- package/dist/esm/testing/node.js +148 -0
- package/dist/esm/testing/owner-client.d.ts +9 -0
- package/dist/esm/testing/owner-client.js +101 -0
- package/dist/esm/testing/owner-shares.js +80 -0
- package/dist/esm/testing/packed.d.ts +46 -0
- package/dist/esm/testing/packed.js +326 -0
- package/dist/esm/testing/reader-lifetime.js +40 -0
- package/dist/esm/testing/reply-loss.js +14 -0
- package/dist/esm/testing/runtime.d.ts +7 -0
- package/dist/esm/testing/runtime.js +13 -0
- package/dist/esm/testing/sdk-upload.d.ts +45 -0
- package/dist/esm/testing/sdk-upload.js +198 -0
- package/dist/esm/testing/streaming-source.js +72 -0
- package/dist/esm/testing/transport.d.ts +22 -0
- package/dist/esm/testing/transport.js +238 -0
- package/dist/esm/testing/types.d.ts +276 -0
- package/dist/esm/vitest.d.ts +23 -0
- package/dist/esm/vitest.js +34 -0
- package/docs/live-auth.md +154 -0
- package/docs/live-runs.md +188 -0
- package/docs/mock-contracts.md +647 -0
- package/package.json +91 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/testing/streaming-source.ts
|
|
2
|
+
async function collectPayload(open, spool, signal, checkpoint = () => void 0, cleanupError = () => void 0, descriptor) {
|
|
3
|
+
let reader;
|
|
4
|
+
let closed;
|
|
5
|
+
let cancellation;
|
|
6
|
+
const cancel = (reason) => {
|
|
7
|
+
if (!reader) return;
|
|
8
|
+
cancellation ??= reader.cancel(reason);
|
|
9
|
+
cancellation.catch(() => void 0);
|
|
10
|
+
};
|
|
11
|
+
const abort = () => cancel(signal.reason);
|
|
12
|
+
const errors = [];
|
|
13
|
+
let complete = false;
|
|
14
|
+
let token;
|
|
15
|
+
try {
|
|
16
|
+
signal.throwIfAborted();
|
|
17
|
+
reader = open().getReader();
|
|
18
|
+
closed = reader.closed.then(() => ({ failed: false }), () => ({ failed: true }));
|
|
19
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
20
|
+
if (signal.aborted) abort();
|
|
21
|
+
const initial = checkpoint(0);
|
|
22
|
+
if (initial) await initial;
|
|
23
|
+
let length = 0;
|
|
24
|
+
for (;;) {
|
|
25
|
+
signal.throwIfAborted();
|
|
26
|
+
const next = await reader.read();
|
|
27
|
+
signal.throwIfAborted();
|
|
28
|
+
if (next.done) break;
|
|
29
|
+
const accepted = intrinsicLength(next.value);
|
|
30
|
+
await spool.append(next.value);
|
|
31
|
+
length += accepted;
|
|
32
|
+
const pending = checkpoint(length);
|
|
33
|
+
if (pending) await pending;
|
|
34
|
+
}
|
|
35
|
+
token = await spool.finish();
|
|
36
|
+
if (descriptor && (token.length !== descriptor.sizeBytes || token.sha256 !== descriptor.sha256)) throw Object.assign(/* @__PURE__ */ new Error("Fixture content does not match its descriptor"), { code: "invalid-input" });
|
|
37
|
+
signal.throwIfAborted();
|
|
38
|
+
complete = true;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
errors.push(error);
|
|
41
|
+
}
|
|
42
|
+
signal.removeEventListener("abort", abort);
|
|
43
|
+
if (!complete) cancel(errors[0]);
|
|
44
|
+
try {
|
|
45
|
+
await cancellation;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (!(await closed)?.failed) {
|
|
48
|
+
errors.push(error);
|
|
49
|
+
cleanupError(error);
|
|
50
|
+
}
|
|
51
|
+
} finally {
|
|
52
|
+
reader?.releaseLock();
|
|
53
|
+
}
|
|
54
|
+
if (!complete) {
|
|
55
|
+
try {
|
|
56
|
+
await spool.discard();
|
|
57
|
+
} catch (error) {
|
|
58
|
+
errors.push(error);
|
|
59
|
+
cleanupError(error);
|
|
60
|
+
}
|
|
61
|
+
if (errors.length === 1) throw errors[0];
|
|
62
|
+
throw new AggregateError(errors, "Payload source and cleanup failed");
|
|
63
|
+
}
|
|
64
|
+
return token;
|
|
65
|
+
}
|
|
66
|
+
function intrinsicLength(value) {
|
|
67
|
+
const prototype = Object.getPrototypeOf(Uint8Array.prototype);
|
|
68
|
+
return Object.getOwnPropertyDescriptor(prototype, "byteLength").get.call(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { collectPayload };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ActorRef, BrowserAccess, MockShareGrant } from "./types.js";
|
|
2
|
+
import { MockCredential } from "./runtime.js";
|
|
3
|
+
|
|
4
|
+
//#region src/testing/transport.d.ts
|
|
5
|
+
interface MockConnection {
|
|
6
|
+
readonly client: string;
|
|
7
|
+
readonly credential?: MockCredential;
|
|
8
|
+
readonly kind: MockCredential["kind"];
|
|
9
|
+
readonly url: string;
|
|
10
|
+
}
|
|
11
|
+
interface MockEndpoint {
|
|
12
|
+
connection(input: {
|
|
13
|
+
readonly access: BrowserAccess;
|
|
14
|
+
readonly as: ActorRef;
|
|
15
|
+
readonly shareGrants?: readonly MockShareGrant[];
|
|
16
|
+
}): MockConnection & {
|
|
17
|
+
readonly credential: MockCredential;
|
|
18
|
+
};
|
|
19
|
+
readonly url: string;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { MockConnection, MockEndpoint };
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { diagnosticField, invalidInput, isEndpointShutdown } from "./diagnostics.js";
|
|
2
|
+
import { isLostReply } from "./reply-loss.js";
|
|
3
|
+
import { mockRuntime } from "./runtime.js";
|
|
4
|
+
import { createApplicationTransport } from "./application-transport.js";
|
|
5
|
+
|
|
6
|
+
//#region src/testing/transport.ts
|
|
7
|
+
/** HTTP and MSW readers use one dispatcher and authenticate the application's current credential. */
|
|
8
|
+
function createMockEndpoint(mock, base, requestBody) {
|
|
9
|
+
const root = new URL("./sia-test/", base.endsWith("/") ? base : base + "/");
|
|
10
|
+
const clients = /* @__PURE__ */ new Map();
|
|
11
|
+
const runtime = mockRuntime(mock);
|
|
12
|
+
runtime.assertTransport();
|
|
13
|
+
const application = createApplicationTransport(mock, requestBody);
|
|
14
|
+
let closed = false;
|
|
15
|
+
const endpoint = {
|
|
16
|
+
connection({ access, as, shareGrants = [] }) {
|
|
17
|
+
if (closed) throw Object.assign(/* @__PURE__ */ new Error("Mock endpoint is closed"), { code: "closed" });
|
|
18
|
+
const credential = runtime.credential(access);
|
|
19
|
+
const createApplication = runtime.installShareGrants(access, as, shareGrants);
|
|
20
|
+
mock.reader({
|
|
21
|
+
access,
|
|
22
|
+
as
|
|
23
|
+
}).close();
|
|
24
|
+
const client = crypto.randomUUID();
|
|
25
|
+
const abort = new AbortController();
|
|
26
|
+
const pending = /* @__PURE__ */ new Set();
|
|
27
|
+
const cleanupErrors = /* @__PURE__ */ new Set();
|
|
28
|
+
const release = runtime.onRunCleanup(access, async () => {
|
|
29
|
+
clients.delete(client);
|
|
30
|
+
abort.abort();
|
|
31
|
+
const results = await Promise.allSettled([...pending]);
|
|
32
|
+
for (const result of results) if (result.status === "rejected" && result.reason instanceof AggregateError) cleanupErrors.add(result.reason);
|
|
33
|
+
if (cleanupErrors.size) throw new AggregateError([...cleanupErrors], "Application run cleanup failed");
|
|
34
|
+
});
|
|
35
|
+
clients.set(client, {
|
|
36
|
+
abort,
|
|
37
|
+
actor: as,
|
|
38
|
+
cleanupErrors,
|
|
39
|
+
createApplication,
|
|
40
|
+
credential: credential.keyHex,
|
|
41
|
+
kind: credential.kind,
|
|
42
|
+
pending,
|
|
43
|
+
release,
|
|
44
|
+
shares: /* @__PURE__ */ new Map(),
|
|
45
|
+
tokens: /* @__PURE__ */ new WeakMap()
|
|
46
|
+
});
|
|
47
|
+
return Object.freeze({
|
|
48
|
+
client,
|
|
49
|
+
credential: Object.freeze(credential),
|
|
50
|
+
kind: credential.kind,
|
|
51
|
+
url: root.href
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
url: root.href
|
|
55
|
+
};
|
|
56
|
+
async function dispatch(request) {
|
|
57
|
+
let detach;
|
|
58
|
+
const url = new URL(request.url);
|
|
59
|
+
if (url.origin !== root.origin || !url.pathname.startsWith(root.pathname)) return Response.json({ code: "not-found" }, { status: 404 });
|
|
60
|
+
const parts = url.pathname.substring(root.pathname.length).split("/");
|
|
61
|
+
const client = parts.length === 2 && clients.get(parts[0]);
|
|
62
|
+
if (!client) return Response.json({ code: "denied" }, { status: 403 });
|
|
63
|
+
const isApplication = parts[1]?.startsWith("app-");
|
|
64
|
+
const isAuth = parts[1]?.startsWith("auth-");
|
|
65
|
+
if (request.method !== (isAuth || isApplication ? "POST" : "GET")) return Response.json({ code: "invalid-input" }, { status: 405 });
|
|
66
|
+
try {
|
|
67
|
+
const header = request.headers.get("authorization") ?? "";
|
|
68
|
+
if (!/^Bearer [a-f0-9]{64}$/.test(header)) return Response.json({ code: "denied" }, { status: 403 });
|
|
69
|
+
const credential = {
|
|
70
|
+
keyHex: header.substring(7),
|
|
71
|
+
kind: client.kind
|
|
72
|
+
};
|
|
73
|
+
if (isAuth) {
|
|
74
|
+
const input = await request.json();
|
|
75
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) throw Object.assign(/* @__PURE__ */ new Error("Invalid auth body"), { code: "invalid-input" });
|
|
76
|
+
const authenticationCredential = client.kind === "registration" && parts[1] === "auth-connected" && "keyHex" in input && input.keyHex === credential.keyHex ? {
|
|
77
|
+
...credential,
|
|
78
|
+
kind: "app-key"
|
|
79
|
+
} : credential;
|
|
80
|
+
return Response.json(runtime.authentication(authenticationCredential, parts[1], input, root.href));
|
|
81
|
+
}
|
|
82
|
+
const access = runtime.authenticate({
|
|
83
|
+
...credential,
|
|
84
|
+
kind: client.kind === "registration" ? "app-key" : client.kind
|
|
85
|
+
});
|
|
86
|
+
if (isApplication) {
|
|
87
|
+
if (client.kind !== "app-key" || client.credential !== credential.keyHex) throw Object.assign(/* @__PURE__ */ new Error("Owner connection required"), { code: "denied" });
|
|
88
|
+
for (const key of url.searchParams.keys()) if (key !== "token" || url.searchParams.getAll(key).length !== 1) throw Object.assign(/* @__PURE__ */ new Error("Invalid parameter"), { code: "invalid-input" });
|
|
89
|
+
const work = application.dispatch(request, parts[1], {
|
|
90
|
+
access,
|
|
91
|
+
actor: client.actor,
|
|
92
|
+
client,
|
|
93
|
+
createApplication: client.createApplication,
|
|
94
|
+
credential: credential.keyHex,
|
|
95
|
+
signal: client.abort.signal
|
|
96
|
+
});
|
|
97
|
+
client.pending.add(work);
|
|
98
|
+
try {
|
|
99
|
+
return await work;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof AggregateError) client.cleanupErrors.add(error);
|
|
102
|
+
throw error;
|
|
103
|
+
} finally {
|
|
104
|
+
client.pending.delete(work);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const allowed = parts[1] === "owner-shares" ? ["offset", "limit"] : parts[1] === "owner-share" ? ["ref"] : parts[1] === "owner-share-objects" ? [
|
|
108
|
+
"ref",
|
|
109
|
+
"offset",
|
|
110
|
+
"limit"
|
|
111
|
+
] : parts[1] === "objects" ? ["offset", "limit"] : parts[1] === "download" ? [
|
|
112
|
+
"id",
|
|
113
|
+
"offset",
|
|
114
|
+
"length"
|
|
115
|
+
] : ["id"];
|
|
116
|
+
for (const key of url.searchParams.keys()) if (!allowed.includes(key) || url.searchParams.getAll(key).length !== 1) throw Object.assign(/* @__PURE__ */ new Error("Invalid parameter"), { code: "invalid-input" });
|
|
117
|
+
const number = (key, fallback) => {
|
|
118
|
+
const value = url.searchParams.get(key);
|
|
119
|
+
if (value === null) return fallback;
|
|
120
|
+
if (!/^\d+$/.test(value) || !Number.isSafeInteger(Number(value))) throw invalidInput(key, `${key} must be a nonnegative safe integer`);
|
|
121
|
+
return Number(value);
|
|
122
|
+
};
|
|
123
|
+
if ([
|
|
124
|
+
"owner-share",
|
|
125
|
+
"owner-share-objects",
|
|
126
|
+
"owner-shares"
|
|
127
|
+
].includes(parts[1])) {
|
|
128
|
+
if (client.kind !== "app-key") throw Object.assign(/* @__PURE__ */ new Error("Owner connection required"), { code: "denied" });
|
|
129
|
+
const owner = mock.ownerShares({
|
|
130
|
+
access,
|
|
131
|
+
as: client.actor
|
|
132
|
+
});
|
|
133
|
+
const cancelled = () => {
|
|
134
|
+
owner.close();
|
|
135
|
+
};
|
|
136
|
+
request.signal.addEventListener("abort", cancelled, { once: true });
|
|
137
|
+
const wire = (details) => {
|
|
138
|
+
let token = client.tokens.get(details.ref);
|
|
139
|
+
if (!token) {
|
|
140
|
+
token = crypto.randomUUID();
|
|
141
|
+
client.tokens.set(details.ref, token);
|
|
142
|
+
client.shares.set(token, details.ref);
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
...details,
|
|
146
|
+
ref: token
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
try {
|
|
150
|
+
if (request.signal.aborted) cancelled();
|
|
151
|
+
const page = {
|
|
152
|
+
limit: number("limit", 100),
|
|
153
|
+
offset: number("offset", 0),
|
|
154
|
+
signal: request.signal
|
|
155
|
+
};
|
|
156
|
+
const ref = client.shares.get(url.searchParams.get("ref") ?? "");
|
|
157
|
+
if (parts[1] === "owner-share") return Response.json(wire(await owner.detail(ref, { signal: request.signal })));
|
|
158
|
+
if (parts[1] === "owner-shares") {
|
|
159
|
+
const result = await owner.list(page);
|
|
160
|
+
return Response.json({
|
|
161
|
+
...result,
|
|
162
|
+
items: result.items.map(wire)
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const result = await owner.objects(ref, page);
|
|
166
|
+
return Response.json({
|
|
167
|
+
...result,
|
|
168
|
+
items: result.items.map(wireObject)
|
|
169
|
+
});
|
|
170
|
+
} finally {
|
|
171
|
+
request.signal.removeEventListener("abort", cancelled);
|
|
172
|
+
await owner.close();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const reader = mock.reader({
|
|
176
|
+
access,
|
|
177
|
+
as: client.actor
|
|
178
|
+
});
|
|
179
|
+
const cancelled = () => {
|
|
180
|
+
reader.close();
|
|
181
|
+
};
|
|
182
|
+
request.signal.addEventListener("abort", cancelled, { once: true });
|
|
183
|
+
detach = () => request.signal.removeEventListener("abort", cancelled);
|
|
184
|
+
if (request.signal.aborted) cancelled();
|
|
185
|
+
const id = url.searchParams.get("id");
|
|
186
|
+
if (parts[1] === "objects") {
|
|
187
|
+
const page = await reader.objects({
|
|
188
|
+
limit: number("limit", 100),
|
|
189
|
+
offset: number("offset", 0),
|
|
190
|
+
signal: request.signal
|
|
191
|
+
});
|
|
192
|
+
return Response.json({
|
|
193
|
+
...page,
|
|
194
|
+
objects: page.objects.map(wireObject)
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (!id) throw Object.assign(/* @__PURE__ */ new Error("Object is required"), { code: "invalid-input" });
|
|
198
|
+
if (parts[1] === "object") return Response.json(wireObject(await reader.object(id, { signal: request.signal })));
|
|
199
|
+
if (parts[1] !== "download") return Response.json({ code: "not-found" }, { status: 404 });
|
|
200
|
+
const stream = await reader.download({ id }, {
|
|
201
|
+
...url.searchParams.has("length") ? { length: number("length", 0) } : {},
|
|
202
|
+
offset: number("offset", 0),
|
|
203
|
+
signal: request.signal
|
|
204
|
+
});
|
|
205
|
+
return new Response(stream, { headers: { "content-type": "application/octet-stream" } });
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (isLostReply(error)) throw error;
|
|
208
|
+
const shutdown = isEndpointShutdown(request.signal.reason) || isEndpointShutdown(error);
|
|
209
|
+
const field = diagnosticField(error);
|
|
210
|
+
const code = shutdown ? "closed" : error && typeof error === "object" && "code" in error && typeof error.code === "string" ? error.code : "invalid-input";
|
|
211
|
+
return Response.json({
|
|
212
|
+
code,
|
|
213
|
+
...field ? { field } : {}
|
|
214
|
+
}, { status: code === "denied" ? 403 : code === "not-found" ? 404 : code === "closed" ? 410 : 400 });
|
|
215
|
+
} finally {
|
|
216
|
+
detach?.();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
close: () => {
|
|
221
|
+
closed = true;
|
|
222
|
+
for (const client of clients.values()) client.release();
|
|
223
|
+
clients.clear();
|
|
224
|
+
return application.close();
|
|
225
|
+
},
|
|
226
|
+
dispatch,
|
|
227
|
+
endpoint
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function wireObject(object) {
|
|
231
|
+
return {
|
|
232
|
+
...object,
|
|
233
|
+
metadata: [...object.metadata]
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
export { createMockEndpoint };
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { MockFaults, RequestReceipt } from "./controls.js";
|
|
2
|
+
import { PackedSdk, PackedSdkOptions } from "./packed.js";
|
|
3
|
+
import { UploadSdk, UploadSdkOptions } from "./sdk-upload.js";
|
|
4
|
+
import { MockMutationFaults } from "./mutations.js";
|
|
5
|
+
|
|
6
|
+
//#region src/testing/types.d.ts
|
|
7
|
+
declare const ownership: unique symbol;
|
|
8
|
+
type ActorRef = Owned<"actor">;
|
|
9
|
+
interface AppDefinition {
|
|
10
|
+
readonly description: string;
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly serviceUrl: string;
|
|
14
|
+
}
|
|
15
|
+
type ApplicationObject = Owned<"application-object"> & {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
};
|
|
18
|
+
interface ApplicationPort {
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
delete(object: ApplicationObject, options?: {
|
|
21
|
+
readonly signal?: AbortSignal;
|
|
22
|
+
}): Promise<void>;
|
|
23
|
+
events(input: {
|
|
24
|
+
readonly cursor: null | ObjectCursor;
|
|
25
|
+
readonly limit: number;
|
|
26
|
+
readonly signal?: AbortSignal;
|
|
27
|
+
}): Promise<EventPage>;
|
|
28
|
+
inspect(object: ApplicationObject, options?: {
|
|
29
|
+
readonly signal?: AbortSignal;
|
|
30
|
+
}): Promise<"missing" | "pinned" | "uploaded">;
|
|
31
|
+
pin(object: ApplicationObject, options?: {
|
|
32
|
+
readonly metadata?: Uint8Array | undefined;
|
|
33
|
+
readonly signal?: AbortSignal;
|
|
34
|
+
}): Promise<void>;
|
|
35
|
+
readonly shares: ApplicationShares;
|
|
36
|
+
upload(input: {
|
|
37
|
+
readonly file: TestFile;
|
|
38
|
+
readonly metadata: Uint8Array; /** Defaults to the snapshotted file label; metadata remains explicit. */
|
|
39
|
+
readonly name?: string | undefined;
|
|
40
|
+
readonly signal?: AbortSignal;
|
|
41
|
+
}): Promise<ApplicationObject>;
|
|
42
|
+
}
|
|
43
|
+
type ApplicationShareRef = Owned<"application-share">;
|
|
44
|
+
interface ApplicationShares {
|
|
45
|
+
add(ref: ApplicationShareRef, object: ApplicationObject, options?: {
|
|
46
|
+
readonly signal?: AbortSignal;
|
|
47
|
+
}): Promise<void>;
|
|
48
|
+
end(ref: ApplicationShareRef, options?: {
|
|
49
|
+
readonly signal?: AbortSignal;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
inspect(ref: ApplicationShareRef, objectId: null | string, options?: {
|
|
52
|
+
readonly signal?: AbortSignal;
|
|
53
|
+
}): Promise<ShareInspection>;
|
|
54
|
+
remove(ref: ApplicationShareRef, objectId: string, options?: {
|
|
55
|
+
readonly signal?: AbortSignal;
|
|
56
|
+
}): Promise<void>;
|
|
57
|
+
targets(options?: {
|
|
58
|
+
readonly signal?: AbortSignal;
|
|
59
|
+
}): Promise<readonly ApplicationShareTarget[]>;
|
|
60
|
+
}
|
|
61
|
+
interface ApplicationShareTarget {
|
|
62
|
+
readonly expiresAt?: string;
|
|
63
|
+
readonly label: string;
|
|
64
|
+
readonly operations: readonly ShareOperation[];
|
|
65
|
+
readonly ref: ApplicationShareRef;
|
|
66
|
+
}
|
|
67
|
+
type BrowserAccess = Owned<"access">;
|
|
68
|
+
interface EventPage {
|
|
69
|
+
readonly done: boolean;
|
|
70
|
+
readonly events: readonly ObjectEvent[];
|
|
71
|
+
readonly nextCursor: null | ObjectCursor;
|
|
72
|
+
}
|
|
73
|
+
interface LiveRun {
|
|
74
|
+
readonly backend: "live";
|
|
75
|
+
readonly id: string;
|
|
76
|
+
readonly objects: ManagedObjects;
|
|
77
|
+
readonly ownerAccess: BrowserAccess;
|
|
78
|
+
readonly shares: ManagedShares;
|
|
79
|
+
}
|
|
80
|
+
interface LiveRunSummary {
|
|
81
|
+
readonly backend: "live";
|
|
82
|
+
readonly cleanup: "clean" | "uncertain";
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly journalFile: string;
|
|
85
|
+
readonly logicalUploadedBytes: number;
|
|
86
|
+
readonly observedShardBytes: number;
|
|
87
|
+
}
|
|
88
|
+
interface ManagedObjects {
|
|
89
|
+
delete(object: TestObject): Promise<void>;
|
|
90
|
+
inspect(object: TestObject): Promise<"missing" | "present">;
|
|
91
|
+
upload(input: {
|
|
92
|
+
readonly file: TestFile;
|
|
93
|
+
readonly metadata: Uint8Array; /** Defaults to the snapshotted file label; metadata remains explicit. */
|
|
94
|
+
readonly name?: string | undefined;
|
|
95
|
+
}): Promise<TestObject>;
|
|
96
|
+
}
|
|
97
|
+
interface ManagedShares {
|
|
98
|
+
attach(share: TestShare, object: TestObject): Promise<void>;
|
|
99
|
+
create(input: {
|
|
100
|
+
readonly expiresAt?: Date;
|
|
101
|
+
readonly objects: readonly TestObject[];
|
|
102
|
+
}): Promise<TestShare>;
|
|
103
|
+
detach(share: TestShare, object: TestObject): Promise<void>;
|
|
104
|
+
revoke(share: TestShare): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
interface MockManagedShares extends ManagedShares {
|
|
107
|
+
create(input: Parameters<ManagedShares["create"]>[0] & {
|
|
108
|
+
readonly description?: string;
|
|
109
|
+
}): Promise<TestShare>;
|
|
110
|
+
grant(share: TestShare, input: {
|
|
111
|
+
readonly as: ActorRef;
|
|
112
|
+
readonly expiresAt?: Date;
|
|
113
|
+
readonly label: string;
|
|
114
|
+
readonly operations: readonly ShareOperation[];
|
|
115
|
+
}): Promise<MockShareGrant>;
|
|
116
|
+
revokeGrant(grant: MockShareGrant): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
interface MockOptions {
|
|
119
|
+
readonly clock?: "realtime" | {
|
|
120
|
+
readonly start: string;
|
|
121
|
+
};
|
|
122
|
+
readonly quotaBytes?: number;
|
|
123
|
+
}
|
|
124
|
+
interface MockRun {
|
|
125
|
+
readonly auth: {
|
|
126
|
+
approve(url: string): Promise<void>;
|
|
127
|
+
deny(url: string): Promise<void>;
|
|
128
|
+
readonly fixturePhrase: string;
|
|
129
|
+
readonly kind: "approval";
|
|
130
|
+
readonly registrationAccess: BrowserAccess;
|
|
131
|
+
};
|
|
132
|
+
readonly backend: "mock";
|
|
133
|
+
readonly id: string;
|
|
134
|
+
readonly mock: MockSia;
|
|
135
|
+
readonly objects: ManagedObjects;
|
|
136
|
+
readonly ownerAccess: BrowserAccess;
|
|
137
|
+
readonly shares: MockManagedShares;
|
|
138
|
+
}
|
|
139
|
+
interface MockRunOptions {
|
|
140
|
+
readonly app: AppDefinition;
|
|
141
|
+
readonly limits?: Partial<RunLimits>;
|
|
142
|
+
readonly signal?: AbortSignal;
|
|
143
|
+
}
|
|
144
|
+
interface MockRunSummary {
|
|
145
|
+
readonly backend: "mock";
|
|
146
|
+
readonly cleanup: "clean";
|
|
147
|
+
readonly id: string;
|
|
148
|
+
readonly logicalUploadedBytes: number;
|
|
149
|
+
}
|
|
150
|
+
type MockShareGrant = Owned<"mock-share-grant">;
|
|
151
|
+
interface MockSia {
|
|
152
|
+
/** The label appears in driver receipts; only the returned reference selects this actor. */
|
|
153
|
+
actor(label: string): ActorRef;
|
|
154
|
+
advanceTime(milliseconds: number): void;
|
|
155
|
+
application(input: {
|
|
156
|
+
readonly access: BrowserAccess;
|
|
157
|
+
readonly as: ActorRef;
|
|
158
|
+
readonly shareGrants?: readonly MockShareGrant[];
|
|
159
|
+
}): ApplicationPort;
|
|
160
|
+
close(): Promise<void>;
|
|
161
|
+
readonly faults: MockFaults;
|
|
162
|
+
history(): readonly RequestReceipt[];
|
|
163
|
+
readonly mutationFaults: MockMutationFaults;
|
|
164
|
+
ownerShares(input: {
|
|
165
|
+
readonly access: BrowserAccess;
|
|
166
|
+
readonly as: ActorRef;
|
|
167
|
+
}): OwnerSharePort;
|
|
168
|
+
packedSdk<P extends "browser" | "node">(input: PackedSdkOptions<P>): PackedSdk<P>;
|
|
169
|
+
publish(input: {
|
|
170
|
+
readonly access: BrowserAccess;
|
|
171
|
+
}): Promise<void>;
|
|
172
|
+
reader(input: {
|
|
173
|
+
readonly access: BrowserAccess;
|
|
174
|
+
readonly as: ActorRef;
|
|
175
|
+
}): ReadPort;
|
|
176
|
+
run<T>(options: MockRunOptions, use: (run: MockRun) => Promise<T>): Promise<RunResult<T, MockRunSummary>>;
|
|
177
|
+
uploadSdk(input: UploadSdkOptions): UploadSdk;
|
|
178
|
+
}
|
|
179
|
+
interface ObjectCursor {
|
|
180
|
+
readonly after: string;
|
|
181
|
+
readonly id: string;
|
|
182
|
+
}
|
|
183
|
+
interface ObjectEvent {
|
|
184
|
+
readonly deleted: boolean;
|
|
185
|
+
readonly id: string;
|
|
186
|
+
readonly updatedAt: string;
|
|
187
|
+
}
|
|
188
|
+
interface OwnerShareDetails {
|
|
189
|
+
readonly description: string;
|
|
190
|
+
readonly expiresAt?: string;
|
|
191
|
+
readonly ref: OwnerShareRef;
|
|
192
|
+
readonly statistics: {
|
|
193
|
+
readonly logicalBytes: number;
|
|
194
|
+
readonly objectCount: number;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
interface OwnerSharePort {
|
|
198
|
+
close(): Promise<void>;
|
|
199
|
+
detail(share: OwnerShareRef, options?: {
|
|
200
|
+
readonly signal?: AbortSignal;
|
|
201
|
+
}): Promise<OwnerShareDetails>;
|
|
202
|
+
list(page: PageRequest): Promise<ReadPage<OwnerShareDetails>>;
|
|
203
|
+
objects(share: OwnerShareRef, page: PageRequest): Promise<ReadPage<ReadObject>>;
|
|
204
|
+
}
|
|
205
|
+
type OwnerShareRef = Owned<"owner-share">;
|
|
206
|
+
interface PageRequest {
|
|
207
|
+
readonly limit: number;
|
|
208
|
+
readonly offset: number;
|
|
209
|
+
readonly signal?: AbortSignal;
|
|
210
|
+
}
|
|
211
|
+
interface ReadObject {
|
|
212
|
+
readonly id: string;
|
|
213
|
+
readonly metadata: Uint8Array;
|
|
214
|
+
readonly sizeBytes: number;
|
|
215
|
+
}
|
|
216
|
+
interface ReadPage<T> {
|
|
217
|
+
readonly done: boolean;
|
|
218
|
+
readonly items: readonly T[];
|
|
219
|
+
readonly nextOffset: number;
|
|
220
|
+
}
|
|
221
|
+
interface ReadPort {
|
|
222
|
+
close(): Promise<void>;
|
|
223
|
+
download(object: Pick<ReadObject, "id">, range?: {
|
|
224
|
+
readonly length?: number;
|
|
225
|
+
readonly offset?: number;
|
|
226
|
+
readonly signal?: AbortSignal;
|
|
227
|
+
}): Promise<ReadableStream<Uint8Array>>;
|
|
228
|
+
object(id: string, options?: {
|
|
229
|
+
readonly signal?: AbortSignal;
|
|
230
|
+
}): Promise<ReadObject>;
|
|
231
|
+
objects(input: {
|
|
232
|
+
readonly limit: number;
|
|
233
|
+
readonly offset: number;
|
|
234
|
+
readonly signal?: AbortSignal;
|
|
235
|
+
}): Promise<{
|
|
236
|
+
readonly done: boolean;
|
|
237
|
+
readonly nextOffset: number;
|
|
238
|
+
readonly objects: readonly ReadObject[];
|
|
239
|
+
}>;
|
|
240
|
+
}
|
|
241
|
+
interface RunLimits {
|
|
242
|
+
readonly maxLogicalUploadBytes: number;
|
|
243
|
+
readonly maxObjects: number;
|
|
244
|
+
readonly maxShares: number;
|
|
245
|
+
}
|
|
246
|
+
interface RunResult<T, Summary extends RunSummary = RunSummary> {
|
|
247
|
+
readonly summary: Summary;
|
|
248
|
+
readonly value: T;
|
|
249
|
+
}
|
|
250
|
+
type RunSummary = LiveRunSummary | MockRunSummary;
|
|
251
|
+
interface ShareInspection {
|
|
252
|
+
readonly contains: boolean | null;
|
|
253
|
+
readonly state: "active" | "ended" | "expired";
|
|
254
|
+
}
|
|
255
|
+
type ShareOperation = "add" | "end" | "remove";
|
|
256
|
+
type SiaRun = LiveRun | MockRun;
|
|
257
|
+
interface TestFile {
|
|
258
|
+
readonly name: string;
|
|
259
|
+
open(): ReadableStream<Uint8Array>;
|
|
260
|
+
readonly sha256: string;
|
|
261
|
+
readonly sizeBytes: number;
|
|
262
|
+
}
|
|
263
|
+
type TestObject = Owned<"object"> & {
|
|
264
|
+
readonly id: string;
|
|
265
|
+
readonly sha256: string;
|
|
266
|
+
readonly sizeBytes: number;
|
|
267
|
+
};
|
|
268
|
+
type TestShare = Owned<"share"> & {
|
|
269
|
+
readonly access: BrowserAccess;
|
|
270
|
+
readonly id: string;
|
|
271
|
+
};
|
|
272
|
+
interface Owned<Kind extends string> {
|
|
273
|
+
readonly [ownership]: Kind;
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
276
|
+
export { ActorRef, AppDefinition, ApplicationObject, ApplicationPort, ApplicationShareRef, ApplicationShareTarget, ApplicationShares, BrowserAccess, EventPage, LiveRun, LiveRunSummary, ManagedObjects, ManagedShares, MockManagedShares, MockOptions, MockRun, MockRunOptions, MockRunSummary, MockShareGrant, MockSia, ObjectCursor, ObjectEvent, OwnerShareDetails, OwnerSharePort, OwnerShareRef, PageRequest, ReadObject, ReadPage, ReadPort, RunLimits, RunResult, RunSummary, ShareInspection, ShareOperation, SiaRun, TestFile, TestObject, TestShare };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AppDefinition, MockOptions, MockRun, RunLimits } from "./testing/types.js";
|
|
2
|
+
import { TestAPI } from "vitest";
|
|
3
|
+
|
|
4
|
+
//#region src/vitest.d.ts
|
|
5
|
+
interface MockSiaTestBackend {
|
|
6
|
+
readonly app: AppDefinition;
|
|
7
|
+
readonly kind: "mock";
|
|
8
|
+
readonly options?: MockOptions;
|
|
9
|
+
}
|
|
10
|
+
type SiaTest = TestAPI<SiaTestFixtures>;
|
|
11
|
+
interface SiaTestFixtures {
|
|
12
|
+
readonly sia: MockRun;
|
|
13
|
+
}
|
|
14
|
+
interface SiaTestOptions {
|
|
15
|
+
readonly backend: MockSiaTestBackend;
|
|
16
|
+
readonly limits?: Partial<RunLimits>;
|
|
17
|
+
/** Sharing this signal cancels every test using it. */
|
|
18
|
+
readonly signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
/** The finish hook closes runs even when a dependent fixture's teardown rejects. */
|
|
21
|
+
declare function createSiaTest(options: SiaTestOptions): SiaTest;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { type AppDefinition, type MockOptions, type MockRun, MockSiaTestBackend, type RunLimits, SiaTest, SiaTestFixtures, SiaTestOptions, createSiaTest };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createMockSia } from "./testing/mock.js";
|
|
2
|
+
import { test } from "vitest";
|
|
3
|
+
|
|
4
|
+
//#region src/vitest.ts
|
|
5
|
+
/** Native Vitest fixtures own portable mock state for each test that requests sia. */
|
|
6
|
+
/** The finish hook closes runs even when a dependent fixture's teardown rejects. */
|
|
7
|
+
function createSiaTest(options) {
|
|
8
|
+
return test.extend({ sia: async ({ onTestFinished, signal: runnerSignal }, use) => {
|
|
9
|
+
const mock = createMockSia(options.backend.options);
|
|
10
|
+
let closing;
|
|
11
|
+
const close = () => closing ??= mock.close();
|
|
12
|
+
let runFailed = false;
|
|
13
|
+
onTestFinished(async () => {
|
|
14
|
+
if (!runFailed) await close();
|
|
15
|
+
});
|
|
16
|
+
const signal = options.signal ? AbortSignal.any([runnerSignal, options.signal]) : runnerSignal;
|
|
17
|
+
try {
|
|
18
|
+
await mock.run({
|
|
19
|
+
app: options.backend.app,
|
|
20
|
+
...options.limits === void 0 ? {} : { limits: options.limits },
|
|
21
|
+
signal
|
|
22
|
+
}, use);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
runFailed = true;
|
|
25
|
+
throw error;
|
|
26
|
+
} finally {
|
|
27
|
+
if (runFailed) await close().catch(() => void 0);
|
|
28
|
+
else await close();
|
|
29
|
+
}
|
|
30
|
+
} });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { createSiaTest };
|