@specific.dev/spectest 0.121.0 → 0.122.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 CHANGED
@@ -20,6 +20,25 @@ Pub/Sub integrations are project-owned as of SDK 0.98.0. The former
20
20
  Use a local service definition or `serviceGroup()` wrapper; the repository’s
21
21
  `examples/google-pubsub` demonstrates this using public SDK APIs.
22
22
 
23
+ ## Supabase function coverage (0.122.0)
24
+
25
+ `supabase({ dir: "supabase" })` automatically collects coverage for its edge
26
+ functions, including local shared modules outside that directory. No application
27
+ instrumentation or coverage option is needed. Custom component names and folder
28
+ layouts work the same way. Stacks without functions have no extra collector.
29
+
30
+ Coverage starts before user code in both shared and per-function isolates. Setup
31
+ and individual test captures are separate, with source maps checked against the
32
+ uploaded TypeScript/JavaScript. Generated startup gates let functions warm
33
+ concurrently while preserving their Deno configuration and import maps.
34
+ Isolate captures, source metadata reads, and report writes run concurrently.
35
+ Lost workers or unusable source metadata fail
36
+ collection rather than silently reporting no execution. Changes to the function
37
+ catalogue or runtime configuration conservatively invalidate subset selection,
38
+ including newly added functions. This collects edge function code only: SQL and
39
+ application frontend coverage are separate, and subset enforcement remains a
40
+ project setting.
41
+
23
42
  ## Interactive entrypoints (0.114.0)
24
43
 
25
44
  Choose the links shown in the interactive dashboard and PR preview comment:
@@ -0,0 +1,7 @@
1
+ import type { CoverageAdapter } from "../coverage.js";
2
+ /** The inspector stays on container loopback; only the component's router
3
+ * talks to it. Reports use the existing, runtime-independent V8 wire format. */
4
+ export declare function supabaseFunctionsCoverage(functionRoot: string): CoverageAdapter;
5
+ /** Runs in the trusted main worker, not in a sandboxed user function. Keep
6
+ * this plain JS so the same module is exercised against the pinned runtime. */
7
+ export declare const SUPABASE_COVERAGE_COLLECTOR: string;
@@ -0,0 +1,375 @@
1
+ import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
+ import { createHash } from "node:crypto";
4
+ import { rawFetch } from "../harness/raw-fetch.js";
5
+ // Drain all work even on failure: no writes may outlive the capture boundary.
6
+ async function finishAll(pending) {
7
+ const results = await Promise.allSettled(pending);
8
+ return results.map(result => {
9
+ if (result.status === "rejected")
10
+ throw result.reason;
11
+ return result.value;
12
+ });
13
+ }
14
+ /** The inspector stays on container loopback; only the component's router
15
+ * talks to it. Reports use the existing, runtime-independent V8 wire format. */
16
+ export function supabaseFunctionsCoverage(functionRoot) {
17
+ const shippedByService = new Map();
18
+ return {
19
+ name: "supabase-functions",
20
+ reports: "delta",
21
+ sourceOptions: { sourceRoot: "." },
22
+ ownsReport: name => name.startsWith("edge-") || name.startsWith("edge-scripts/"),
23
+ async inventory(ctx) {
24
+ // Functions are discovered by directory, not by an import in user code.
25
+ // Changing the catalogue must invalidate selection even for a new file.
26
+ // A removed fingerprint with no executed unit conservatively runs all.
27
+ const prefix = functionRoot + "/";
28
+ const files = await ctx.projectFiles();
29
+ const entries = files.filter(file => {
30
+ if (!file.startsWith(prefix))
31
+ return false;
32
+ const parts = file.slice(prefix.length).split("/");
33
+ return parts.length === 2 && !/^[_.]/.test(parts[0]) && parts[1] === "index.ts";
34
+ }).sort();
35
+ const configNames = new Set(["deno.json", "deno.jsonc", "deno.lock", "import_map.json", "package.json"]);
36
+ const configPaths = files.filter(file => {
37
+ if (!configNames.has(path.posix.basename(file)))
38
+ return file === path.posix.join(path.posix.dirname(functionRoot), "config.toml");
39
+ const dir = path.posix.dirname(file);
40
+ return dir === "." || file.startsWith(prefix) || functionRoot.startsWith(dir + "/") || dir === functionRoot;
41
+ }).sort();
42
+ const configs = await Promise.all(configPaths.map(async (file) => [file, await ctx.readProjectFile(file)]));
43
+ const digest = createHash("sha256").update(JSON.stringify({ entries, configs })).digest("hex");
44
+ return [`manifest:edge-functions:${encodeURIComponent(functionRoot)}#${digest}`];
45
+ },
46
+ async capture(ctx) {
47
+ let shipped = shippedByService.get(ctx.service);
48
+ if (!shipped)
49
+ shippedByService.set(ctx.service, shipped = new Set());
50
+ const response = await rawFetch(`http://${ctx.service}:9000/_spectest/coverage`, {
51
+ method: "POST", signal: ctx.signal,
52
+ });
53
+ if (!response.ok)
54
+ throw new Error(`edge coverage: ${await response.text()}`);
55
+ const payload = await response.json();
56
+ if (!payload || !Array.isArray(payload.reports) || !payload.reports.length)
57
+ throw new Error("edge coverage returned no reports");
58
+ for (const report of payload.reports) {
59
+ if (!report || !/^(edge-[\w-]+\.json|edge-scripts\/[a-f0-9]+\.json)$/.test(report.name) || typeof report.content !== "string") {
60
+ throw new Error("invalid edge coverage report");
61
+ }
62
+ }
63
+ // State and files both fork with the VM. Remove only reports that this
64
+ // adapter previously shipped, never another adapter's output.
65
+ await finishAll([...shipped].map(name => fs.rm(path.join(ctx.reportDir, name), { force: true })));
66
+ shipped.clear();
67
+ await finishAll(payload.reports.map(async (report) => {
68
+ // Track attempted writes too, so a failed write is cleaned up next time.
69
+ shipped.add(report.name);
70
+ await ctx.writeReport(report.name, report.content);
71
+ }));
72
+ },
73
+ };
74
+ }
75
+ /** Runs in the trusted main worker, not in a sandboxed user function. Keep
76
+ * this plain JS so the same module is exercised against the pinned runtime. */
77
+ export const SUPABASE_COVERAGE_COLLECTOR = String.raw `
78
+ const INTERNAL = /^(ext:|node:|internal:)/;
79
+ const COMPILED = /^\/var\/tmp\/sb-compile-[^/]+\//;
80
+ const TIMEOUT = 30000;
81
+ // Keep failed captures fenced until every independent operation has settled.
82
+ const finishAll = async pending => {
83
+ const results = await Promise.allSettled(pending);
84
+ return results.map(result => {
85
+ if (result.status === "rejected") throw result.reason;
86
+ return result.value;
87
+ });
88
+ };
89
+ const hash = async text => Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text))), b => b.toString(16).padStart(2, "0")).join("");
90
+ const fileUrl = path => "file://" + path.split("/").map(encodeURIComponent).join("/");
91
+
92
+ export class EdgeCoverage {
93
+ constructor({ inspector = "http://127.0.0.1:9229", projectRoot = "/repo", generatedRoot = "/home/deno/spectest-functions" } = {}) {
94
+ this.inspector = inspector;
95
+ this.projectRoot = projectRoot.replace(/\/$/, "");
96
+ this.generatedRoot = generatedRoot;
97
+ this.entries = new Set();
98
+ this.wrappers = new Map();
99
+ this.mirrors = new Map();
100
+ this.sessions = new Map();
101
+ this.records = new Set();
102
+ this.error = null;
103
+ this.polling = null;
104
+ this.capturing = false;
105
+ this.sequence = 0;
106
+ this.timer = setInterval(() => this.poll().catch(e => this.fail(e)), 100);
107
+ }
108
+ fail(error) { this.error ??= String(error?.stack ?? error); }
109
+ register(entry) { this.entries.add(entry); }
110
+ wrap(entry) {
111
+ if (!this.wrappers.has(entry)) this.wrappers.set(entry, this.writeEntry(entry));
112
+ return this.wrappers.get(entry);
113
+ }
114
+ mirrorDirectory(relative) {
115
+ if (this.mirrors.has(relative)) return this.mirrors.get(relative);
116
+ const pending = (async () => {
117
+ const source = this.projectRoot + (relative ? "/" + relative : "");
118
+ const target = this.generatedRoot + "/project" + (relative ? "/" + relative : "");
119
+ if (relative) {
120
+ await this.mirrorDirectory(relative.includes("/") ? relative.slice(0, relative.lastIndexOf("/")) : "");
121
+ await Deno.remove(target); // replace this directory's link with an overlay
122
+ }
123
+ await Deno.mkdir(target, { recursive: true });
124
+ const items = [];
125
+ for await (const item of Deno.readDir(source)) items.push(item.name);
126
+ await finishAll(items.map(name => Deno.symlink(source + "/" + name, target + "/" + name)));
127
+ return target;
128
+ })();
129
+ this.mirrors.set(relative, pending);
130
+ return pending;
131
+ }
132
+ async writeEntry(entry) {
133
+ const slash = entry.lastIndexOf("/");
134
+ let directory = entry.slice(0, slash);
135
+ if (entry.startsWith(this.projectRoot + "/")) {
136
+ // Keep the complete directory/config layout, including ancestor Deno
137
+ // workspaces, relative import maps and packages. Only directories along
138
+ // the entry's ancestry are materialized; everything else is a symlink.
139
+ directory = await this.mirrorDirectory(directory.slice(this.projectRoot.length + 1));
140
+ }
141
+ const wrapper = directory + "/.spectest-coverage-" + (await hash(entry)).slice(0, 16) + ".mjs";
142
+ const code = [
143
+ "// spectest-generated coverage gate. Do not edit.",
144
+ "const keepAlive = setInterval(() => {}, 25);",
145
+ "const gate = {};",
146
+ "globalThis[Symbol.for('spectest.edgeCoverage')] = gate;",
147
+ "await new Promise(resolve => { gate.release = resolve; });",
148
+ "clearInterval(keepAlive);",
149
+ "const mod = await import(" + JSON.stringify("./" + entry.slice(slash + 1)) + ");",
150
+ "export default ('default' in mod ? mod.default : {});",
151
+ ].join("\n");
152
+ await Deno.writeTextFile(wrapper, code, { createNew: true });
153
+ this.register(wrapper);
154
+ return fileUrl(wrapper);
155
+ }
156
+ async forgetUnstarted(entry) {
157
+ // Compilation can reject the shared graph before an isolate exists. That
158
+ // is a normal reason for the component to retry in per-function mode.
159
+ const wrapper = await this.wrappers.get(entry);
160
+ if (!wrapper) return;
161
+ const original = decodeURIComponent(new URL(wrapper).pathname);
162
+ if (![...this.sessions.values()].some(s => s.entry === original)) this.entries.delete(original);
163
+ }
164
+ async ready() {
165
+ const deadline = Date.now() + TIMEOUT;
166
+ while (true) {
167
+ await this.poll();
168
+ if (this.error) throw new Error(this.error);
169
+ const attached = new Set([...this.sessions.values()].map(s => s.entry));
170
+ if ([...this.entries].every(entry => attached.has(entry))) {
171
+ await Promise.all([...this.sessions.values()].map(s => s.ready));
172
+ return;
173
+ }
174
+ if (Date.now() >= deadline) throw new Error("edge coverage did not attach to every warmed worker");
175
+ await new Promise(resolve => setTimeout(resolve, 25));
176
+ }
177
+ }
178
+ async poll() {
179
+ if (this.polling) return this.polling;
180
+ this.polling = this.discover();
181
+ try { await this.polling; } finally { this.polling = null; }
182
+ }
183
+ async discover() {
184
+ const response = await fetch(this.inspector + "/json/list", { signal: AbortSignal.timeout(TIMEOUT) });
185
+ if (!response.ok) throw new Error("edge inspector discovery failed: " + response.status);
186
+ const targets = await response.json();
187
+ if (!Array.isArray(targets)) throw new Error("invalid edge inspector target list");
188
+ const live = new Set(targets.map(t => t.id));
189
+ for (const [id] of this.sessions) if (!live.has(id)) throw new Error("edge coverage lost worker " + id);
190
+ for (const target of targets) {
191
+ if (this.sessions.has(target.id)) continue;
192
+ const session = this.connect(target);
193
+ this.sessions.set(target.id, session);
194
+ // Discover all isolates while their sessions initialize. ready() joins
195
+ // initialization separately; one worker never blocks discovery of others.
196
+ session.ready.catch(e => this.fail(e));
197
+ }
198
+ }
199
+ connect(target) {
200
+ const url = new URL(target.url);
201
+ if (url.protocol !== "file:") throw new Error("unexpected edge worker entry: " + target.url);
202
+ const compiledPath = decodeURIComponent(url.pathname);
203
+ const relative = compiledPath.replace(COMPILED, "");
204
+ // The runtime relocates the entire module graph under sb-compile-*/.
205
+ // Its registered entrypoint and our exact original entrypoint establish
206
+ // the relocation, independently for every isolate. Never guess a source
207
+ // file by basename or by a repository suffix.
208
+ const candidates = [...this.entries].filter(entry => compiledPath === entry || entry.endsWith("/" + relative));
209
+ if (candidates.length !== 1) throw new Error("ambiguous edge worker entry: " + target.url);
210
+ const original = candidates[0];
211
+ const base = COMPILED.test(compiledPath) ? original.slice(0, -relative.length) : "/";
212
+ const prefix = COMPILED.exec(compiledPath)?.[0] ?? "/";
213
+ const socketUrl = new URL(target.webSocketDebuggerUrl);
214
+ const inspector = new URL(this.inspector);
215
+ socketUrl.host = inspector.host;
216
+ const ws = new WebSocket(socketUrl);
217
+ const session = { ws, base, prefix, entry: original, scripts: new Map(), descriptions: new Map(), nextId: 0, pending: new Map() };
218
+ session.post = (method, params = {}) => new Promise((resolve, reject) => {
219
+ const id = ++session.nextId;
220
+ const timer = setTimeout(() => { session.pending.delete(id); reject(new Error("edge inspector timeout: " + method)); }, TIMEOUT);
221
+ session.pending.set(id, { resolve, reject, timer });
222
+ ws.send(JSON.stringify({ id, method, params }));
223
+ });
224
+ ws.onmessage = event => {
225
+ try {
226
+ const message = JSON.parse(event.data);
227
+ if (message.id) {
228
+ const pending = session.pending.get(message.id);
229
+ if (!pending) return;
230
+ clearTimeout(pending.timer);
231
+ session.pending.delete(message.id);
232
+ if (message.error) pending.reject(new Error(JSON.stringify(message.error)));
233
+ else pending.resolve(message.result);
234
+ } else if (message.method === "Debugger.scriptParsed") {
235
+ session.scripts.set(message.params.scriptId, message.params);
236
+ } else if (message.method === "Debugger.paused") {
237
+ session.post("Debugger.resume").catch(e => this.fail(e));
238
+ }
239
+ } catch (e) { this.fail(e); }
240
+ };
241
+ ws.onclose = () => {
242
+ const error = new Error("edge coverage inspector disconnected: " + target.id);
243
+ for (const p of session.pending.values()) { clearTimeout(p.timer); p.reject(error); }
244
+ session.pending.clear();
245
+ this.fail(error);
246
+ };
247
+ session.ready = (async () => {
248
+ await new Promise((resolve, reject) => {
249
+ const timer = setTimeout(() => reject(new Error("edge inspector connection timeout")), TIMEOUT);
250
+ ws.onopen = () => { clearTimeout(timer); resolve(); };
251
+ ws.onerror = () => { clearTimeout(timer); reject(new Error("edge inspector connection failed")); };
252
+ });
253
+ await session.post("Debugger.enable", { maxScriptsCacheSize: 16 * 1024 * 1024 });
254
+ await session.post("Profiler.enable");
255
+ await session.post("Profiler.startPreciseCoverage", { callCount: true, detailed: true });
256
+ // --inspect-brk can park inside the pinned runtime's inspector before
257
+ // its pending session handshake is polled. Use a live JS gate instead:
258
+ // no project module executes until precise coverage is acknowledged.
259
+ const released = await session.post("Runtime.evaluate", {
260
+ expression: "(async () => { const key = Symbol.for('spectest.edgeCoverage'); while (!globalThis[key]?.release) await new Promise(resolve => setTimeout(resolve, 10)); globalThis[key].release(); await new Promise(resolve => setTimeout(resolve, 0)); })()\n//# sourceURL=spectest:edge-coverage-bootstrap",
261
+ awaitPromise: true,
262
+ });
263
+ if (released.exceptionDetails) throw new Error("edge coverage gate failed: " + JSON.stringify(released.exceptionDetails));
264
+ })();
265
+ return session;
266
+ }
267
+ originalPath(session, url) {
268
+ const parsed = new URL(url);
269
+ if (parsed.protocol !== "file:") return null;
270
+ const pathname = decodeURIComponent(parsed.pathname);
271
+ if (!pathname.startsWith(session.prefix)) throw new Error("unknown edge source root: " + url);
272
+ return session.base + pathname.slice(session.prefix.length);
273
+ }
274
+ sourcePath(file) {
275
+ const mirror = this.generatedRoot + "/project/";
276
+ return file.startsWith(mirror) ? this.projectRoot + "/" + file.slice(mirror.length) : file;
277
+ }
278
+ async describe(session, script, original) {
279
+ if (session.descriptions.has(script.scriptId)) return session.descriptions.get(script.scriptId);
280
+ const { scriptSource: text } = await session.post("Debugger.getScriptSource", { scriptId: script.scriptId });
281
+ if (typeof text !== "string") throw new Error("missing edge source: " + script.url);
282
+ const metadata = session.scripts.get(script.scriptId);
283
+ const mapUrl = metadata?.sourceMapURL;
284
+ let sourceMap = null;
285
+ if (mapUrl) {
286
+ if (!mapUrl.startsWith("data:")) throw new Error("unsupported edge source map: " + mapUrl);
287
+ const comma = mapUrl.indexOf(",");
288
+ const encoded = mapUrl.slice(comma + 1);
289
+ const decoded = mapUrl.slice(0, comma).endsWith(";base64")
290
+ ? new TextDecoder().decode(Uint8Array.from(atob(encoded), c => c.charCodeAt(0))) : decodeURIComponent(encoded);
291
+ const data = JSON.parse(decoded);
292
+ if (!Array.isArray(data.sources) || !Array.isArray(data.sourcesContent) || typeof data.mappings !== "string" || data.sections || data.sourceRoot) {
293
+ throw new Error("missing edge source-map identity: " + script.url);
294
+ }
295
+ // Edge Runtime's map sources are relative to the graph root, rather
296
+ // than the script directory. Validate their contents against the actual
297
+ // mounted sources before archiving the normalized identities.
298
+ data.sources = await finishAll(data.sources.map(async (name, i) => {
299
+ const source = new URL(name, fileUrl(session.base));
300
+ if (source.protocol !== "file:") throw new Error("unexpected mapped edge source: " + source.href);
301
+ const file = this.sourcePath(decodeURIComponent(source.pathname));
302
+ if (!file.startsWith(this.projectRoot + "/")) throw new Error("edge map escapes project: " + file);
303
+ if (await Deno.readTextFile(file) !== data.sourcesContent[i]) throw new Error("edge source content mismatch: " + file);
304
+ return fileUrl(file);
305
+ }));
306
+ delete data.sourcesContent;
307
+ delete data.sourceRoot;
308
+ sourceMap = { url: null, data };
309
+ } else {
310
+ // JavaScript may need no transpilation. TypeScript without a map must
311
+ // not silently attribute generated line numbers to authored source.
312
+ if (await Deno.readTextFile(original) !== text) throw new Error("edge source has no usable map: " + original);
313
+ }
314
+ const record = { url: fileUrl(original), sourceHash: await hash(text),
315
+ lineLengths: text.split(/[\n\u2028\u2029]/).map(line => line.length),
316
+ functions: script.functions.map(f => [f.functionName, f.ranges[0].startOffset, f.ranges[0].endOffset]), sourceMap };
317
+ const content = JSON.stringify({ spectestScript: record });
318
+ const descriptor = { id: await hash(content), content };
319
+ session.descriptions.set(script.scriptId, descriptor);
320
+ return descriptor;
321
+ }
322
+ async capture() {
323
+ if (this.capturing) throw new Error("edge coverage capture already in progress");
324
+ this.capturing = true;
325
+ try {
326
+ await this.ready();
327
+ if (this.error) throw new Error(this.error);
328
+ // Each isolate owns its counters. Take all deltas concurrently, and
329
+ // fetch independent script descriptors concurrently within each isolate.
330
+ // Join before merging so ordering/deduplication don't depend on timing.
331
+ const captures = await finishAll([...this.sessions.values()].map(async session => {
332
+ const dump = await session.post("Profiler.takePreciseCoverage");
333
+ return finishAll(dump.result.map(async script => {
334
+ if (!script.functions.some(f => f.ranges.some(r => r.count > 0))) return null;
335
+ if (INTERNAL.test(script.url)) return null;
336
+ if (script.url === "spectest:edge-coverage-bootstrap") return null;
337
+ if (/^(https?:|npm:|jsr:)/.test(script.url)) return { unit: "deno:" + script.url };
338
+ if (!script.url) throw new Error("anonymous executed edge script cannot be mapped");
339
+ const rawPath = this.originalPath(session, script.url);
340
+ if (rawPath === session.entry) return null; // the generated startup gate
341
+ const original = rawPath && this.sourcePath(rawPath);
342
+ if (original?.startsWith(this.generatedRoot + "/")) return null;
343
+ if (original?.includes("/node_modules/")) return { unit: "deno:" + script.url };
344
+ if (!original?.startsWith(this.projectRoot + "/")) throw new Error("unmapped edge script: " + script.url);
345
+ const descriptor = await this.describe(session, script, original);
346
+ return { descriptor, result: { url: fileUrl(original), spectestScript: descriptor.id,
347
+ functions: script.functions.filter(f => f.ranges.some(r => r.count > 0)).map(f => {
348
+ const start = f.ranges[0].startOffset;
349
+ return f.ranges.flatMap((r, i) => [i ? r.startOffset - start : start, r.endOffset - r.startOffset, r.count]);
350
+ }) } };
351
+ }));
352
+ }));
353
+ const reports = [], result = [], units = new Set();
354
+ for (const capture of captures) for (const item of capture) {
355
+ if (!item) continue;
356
+ if (item.unit) { units.add(item.unit); continue; }
357
+ const { descriptor } = item;
358
+ if (!this.records.has(descriptor.id)) {
359
+ reports.push({ name: "edge-scripts/" + descriptor.id + ".json", content: descriptor.content });
360
+ this.records.add(descriptor.id);
361
+ }
362
+ result.push(item.result);
363
+ }
364
+ if (this.error) throw new Error(this.error);
365
+ reports.push({ name: "edge-" + (++this.sequence) + ".json", content: JSON.stringify({ spectestVersion: 2, result, units: [...units] }) });
366
+ return { reports };
367
+ } catch (e) {
368
+ // A take resets V8 counters. If processing fails afterwards, subsequent
369
+ // captures cannot repair that gap. Keep the failure sticky on this fork.
370
+ this.fail(e);
371
+ throw e;
372
+ } finally { this.capturing = false; }
373
+ }
374
+ }
375
+ `;
@@ -65,6 +65,7 @@ import { microsoft } from "./microsoft.js";
65
65
  import { SUPABASE_WEBHOOKS_SQL } from "./supabase-webhooks.js";
66
66
  import { postgresTls } from "./postgres-tls.js";
67
67
  import { supabasePoolerService } from "./supabase-pooler.js";
68
+ import { SUPABASE_COVERAGE_COLLECTOR, supabaseFunctionsCoverage } from "./supabase-coverage.js";
68
69
  import { mapSupabaseConfig, parseSupabaseConfigFile, } from "./supabase-config.js";
69
70
  // ──────────────────────────────────────────────────────────────────────────
70
71
  // Pinned upstream image tags (github.com/supabase/supabase docker-compose.yml,
@@ -690,6 +691,8 @@ services:
690
691
  // with, and Web Crypto verifies it in a few lines with no dependency.
691
692
  // ──────────────────────────────────────────────────────────────────────────
692
693
  const FUNCTIONS_MAIN_TS = String.raw `// spectest-generated edge-functions router. Do not edit.
694
+ import { EdgeCoverage } from "./coverage.js";
695
+ const edgeCoverage = new EdgeCoverage();
693
696
  const FUNCTIONS_DIR = Deno.env.get("FUNCTIONS_DIR") ?? "/repo/supabase/functions";
694
697
  const JWT_SECRET = Deno.env.get("JWT_SECRET") ?? "";
695
698
  const VERIFY_JWT = Deno.env.get("VERIFY_JWT") === "true";
@@ -754,8 +757,10 @@ const WORKER_TIMEOUT_MS = 24 * 60 * 60 * 1000;
754
757
  * isolates, and an isolate reaped for idleness is rebooted by the next
755
758
  * test that calls the function — a boot's worth of dirtied memory in that
756
759
  * test's own snapshot diff. */
757
- function workerFor(name: string) {
760
+ async function workerFor(name: string) {
761
+ const maybeEntrypoint = await edgeCoverage.wrap(FUNCTIONS_DIR + "/" + name + "/index.ts");
758
762
  return EdgeRuntime.userWorkers.create({
763
+ maybeEntrypoint,
759
764
  servicePath: FUNCTIONS_DIR + "/" + name,
760
765
  memoryLimitMb: 256,
761
766
  workerTimeoutMs: WORKER_TIMEOUT_MS,
@@ -978,6 +983,7 @@ async function sharedWorker(): Promise<any | null> {
978
983
  try {
979
984
  await createShared();
980
985
  } catch (e) {
986
+ await edgeCoverage.forgetUnstarted(SHARED_DIR + "/index.ts");
981
987
  reason = "the shared isolate failed to boot: " + String(e);
982
988
  }
983
989
  }
@@ -989,8 +995,10 @@ async function sharedWorker(): Promise<any | null> {
989
995
  return spec.reason === null ? createShared() : null;
990
996
  }
991
997
 
992
- function createShared() {
998
+ async function createShared() {
999
+ const maybeEntrypoint = await edgeCoverage.wrap(SHARED_DIR + "/index.ts");
993
1000
  return EdgeRuntime.userWorkers.create({
1001
+ maybeEntrypoint,
994
1002
  servicePath: SHARED_DIR,
995
1003
  // A cap on the one isolate that holds every function, not a reservation.
996
1004
  memoryLimitMb: 2048,
@@ -1018,8 +1026,8 @@ function createShared() {
1018
1026
  async function warmAll(): Promise<{ mode: "shared" | "per-function"; warmed: string[]; failed: Record<string, string> }> {
1019
1027
  const names = await functionNames();
1020
1028
  if ((await sharedWorker()) !== null) return { mode: "shared", warmed: names, failed: {} };
1021
- // All at once: each boot is mostly module loading, and the isolates come
1022
- // up concurrently.
1029
+ // Each worker starts behind a generated gate; attach and initialize them
1030
+ // concurrently, then release each only after precise coverage has started.
1023
1031
  const results = await Promise.allSettled(names.map((name) => workerFor(name)));
1024
1032
  const warmed: string[] = [];
1025
1033
  const failed: Record<string, string> = {};
@@ -1036,8 +1044,16 @@ Deno.serve(async (req: Request) => {
1036
1044
  // Ready probe — answered before any auth check, so bring-up never needs a
1037
1045
  // token, and never treated as a function name.
1038
1046
  if (pathname === "/_spectest/health") return new Response("ok");
1047
+ if (pathname === "/_spectest/coverage" && req.method === "POST") {
1048
+ try { return json(await edgeCoverage.capture()); }
1049
+ catch (error) { return json({ error: String(error) }, 500); }
1050
+ }
1039
1051
  // Setup-time warm-up (see warmAll); never treated as a function name.
1040
- if (pathname === "/_spectest/warm") return json(await warmAll());
1052
+ if (pathname === "/_spectest/warm") {
1053
+ const warmed = await warmAll();
1054
+ await edgeCoverage.ready();
1055
+ return json(warmed);
1056
+ }
1041
1057
 
1042
1058
  const name = pathname.split("/")[1] ?? "";
1043
1059
  if (name === "") return json({ msg: "missing function name in request" }, 400);
@@ -1787,8 +1803,15 @@ CREATE SCHEMA IF NOT EXISTS _supavisor AUTHORIZATION supabase_admin;
1787
1803
  volumes: [{ source: projectRootPath(), target: REPO_MOUNT, readOnly: true, reachExempt: true }],
1788
1804
  // Our router (see FUNCTIONS_MAIN_TS) sits outside that mount, so
1789
1805
  // the project never has to carry a `main/` function of ours.
1790
- files: [{ path: "/home/deno/main/index.ts", content: FUNCTIONS_MAIN_TS }],
1791
- args: ["start", "--main-service", "/home/deno/main", "--port", "9000"],
1806
+ files: [
1807
+ { path: "/home/deno/main/index.ts", content: FUNCTIONS_MAIN_TS },
1808
+ { path: "/home/deno/main/coverage.js", content: SUPABASE_COVERAGE_COLLECTOR },
1809
+ ],
1810
+ // Generated entry gates hold project imports until coverage starts;
1811
+ // the inspector stays live and is not exposed to peer containers.
1812
+ args: ["start", "--main-service", "/home/deno/main", "--port", "9000", "--inspect=127.0.0.1:9229"],
1813
+ workdir: REPO_MOUNT,
1814
+ coverage: { adapters: [supabaseFunctionsCoverage(fnRelPath)] },
1792
1815
  env: {
1793
1816
  JWT_SECRET: jwtSecret,
1794
1817
  // Where the router looks for `<name>/`, inside the repo mount.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specific.dev/spectest",
3
- "version": "0.121.0",
3
+ "version": "0.122.0",
4
4
  "description": "Spectest SDK for defining test environments in TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -11,6 +11,7 @@ async function check(code: string, workspace?: string): Promise<void> {
11
11
  import { strict as assert } from "node:assert";
12
12
  import { s3 } from ${moduleUrl("./s3.ts")};
13
13
  import { supabase } from ${moduleUrl("./supabase.ts")};
14
+ import { supabaseFunctionsCoverage } from ${moduleUrl("./supabase-coverage.ts")};
14
15
  import { emailHelpers } from ${moduleUrl("./email.ts")};
15
16
  import { installFetchWrapper } from ${moduleUrl("../harness/fetch.ts")};
16
17
  import { createInstrumentationScope, runInstrumented } from ${moduleUrl("../harness/instrumentation-scope.ts")};
@@ -56,6 +57,42 @@ test.each([200, 201, 409, 403, 500])("S3 setup handles HTTP %i inside a wrapped
56
57
  `);
57
58
  });
58
59
 
60
+ test("Supabase report writes overlap and drain before a failed capture returns", async () => {
61
+ const root = await mkdtemp(join(tmpdir(), "spectest-edge-parallel-"));
62
+ try {
63
+ await check(`
64
+ const fs = await import("node:fs/promises");
65
+ const root = ${JSON.stringify(root)};
66
+ const adapter = supabaseFunctionsCoverage("custom/functions");
67
+ const slow = Promise.withResolvers();
68
+ const bothStarted = Promise.withResolvers();
69
+ let started = 0, settled = false;
70
+ respond = () => Response.json({ reports: [
71
+ { name: "edge-1.json", content: "one" },
72
+ { name: "edge-2.json", content: "two" },
73
+ ] });
74
+ const ctx = { service: "functions", reportDir: root, signal: AbortSignal.timeout(5000),
75
+ async writeReport(name, content) {
76
+ if (++started === 2) bothStarted.resolve();
77
+ if (name === "edge-1.json") throw new Error("write failed");
78
+ await slow.promise;
79
+ await fs.writeFile(root + "/" + name, content);
80
+ },
81
+ };
82
+ const pending = adapter.capture(ctx).then(() => null, error => { settled = true; return error; });
83
+ await bothStarted.promise;
84
+ await Bun.sleep(0);
85
+ assert.equal(settled, false);
86
+ slow.resolve();
87
+ assert.match((await pending).message, /write failed/);
88
+ respond = () => Response.json({ reports: [{ name: "edge-3.json", content: "three" }] });
89
+ ctx.writeReport = (name, content) => fs.writeFile(root + "/" + name, content);
90
+ await adapter.capture(ctx);
91
+ assert.deepEqual(await fs.readdir(root), ["edge-3.json"]);
92
+ `);
93
+ } finally { await rm(root, { recursive: true, force: true }); }
94
+ });
95
+
59
96
  test("S3 setup propagates transport failures", async () => {
60
97
  await check(`
61
98
  respond = () => { throw new Error("connection refused"); };
@@ -107,3 +144,34 @@ test("email helpers reject HTTP errors and retain their public event without int
107
144
  assert.equal(events.some(e => e.error), false);
108
145
  `);
109
146
  });
147
+
148
+ test("automatic edge coverage uses raw fetch and replaces only its previously shipped reports", async () => {
149
+ const root = await mkdtemp(join(tmpdir(), "spectest-edge-capture-"));
150
+ try {
151
+ await check(`
152
+ const fs = await import("node:fs/promises");
153
+ const root = ${JSON.stringify(root)};
154
+ const adapter = supabaseFunctionsCoverage("custom/functions");
155
+ const ctx = {
156
+ service: "custom-functions", reportDir: root, signal: AbortSignal.timeout(5000),
157
+ writeReport: (name, content) => fs.writeFile(root + "/" + name, content),
158
+ };
159
+ await fs.writeFile(root + "/other.lcov", "TN:");
160
+ const content = JSON.stringify({ spectestVersion: 2, result: [] });
161
+ respond = () => Response.json({ reports: [{ name: "edge-1.json", content }] });
162
+ await adapter.capture(ctx);
163
+ respond = () => Response.json({ reports: [{ name: "edge-2.json", content }] });
164
+ await adapter.capture(ctx);
165
+ assert.deepEqual((await fs.readdir(root)).sort(), ["edge-2.json", "other.lcov"]);
166
+ assert.deepEqual(requests, [
167
+ { url: "http://custom-functions:9000/_spectest/coverage", method: "POST" },
168
+ { url: "http://custom-functions:9000/_spectest/coverage", method: "POST" },
169
+ ]);
170
+ respond = () => Response.json({ reports: [{ name: "../escape.json", content }] });
171
+ await assert.rejects(() => adapter.capture(ctx), /invalid edge coverage report/);
172
+ respond = () => new Response("lost worker", { status: 500 });
173
+ await assert.rejects(() => adapter.capture(ctx), /lost worker/);
174
+ assert.equal(await fs.readFile(root + "/edge-2.json", "utf8"), content);
175
+ `);
176
+ } finally { await rm(root, { recursive: true, force: true }); }
177
+ });