|
1 | 1 | import { describe, expect, test } from "vitest"; |
| 2 | +import type { RegistryConfig } from "./config"; |
| 3 | +import { loadAutoRuntime, type RuntimeLoaders } from "./native"; |
2 | 4 | import { |
| 5 | + type CoreRuntime, |
3 | 6 | normalizeRuntimeSqlExecuteResult, |
4 | 7 | type RuntimeSqlBindParam, |
5 | 8 | type RuntimeSqlBindParams, |
@@ -48,3 +51,74 @@ describe("runtime SQL boundary", () => { |
48 | 51 | expect(normalizeRuntimeSqlExecuteResult(base)).toEqual(base); |
49 | 52 | }); |
50 | 53 | }); |
| 54 | + |
| 55 | +describe("loadAutoRuntime failure reporting", () => { |
| 56 | + const wasmRuntime = { kind: "wasm" } as unknown as CoreRuntime; |
| 57 | + const nativeRuntime = { kind: "napi" } as unknown as CoreRuntime; |
| 58 | + const config = {} as RegistryConfig; |
| 59 | + |
| 60 | + function loaders(overrides: Partial<RuntimeLoaders>): RuntimeLoaders { |
| 61 | + return { |
| 62 | + detectHost: () => "node-like", |
| 63 | + loadNative: async () => ({ runtime: nativeRuntime }) as never, |
| 64 | + loadWasm: async () => ({ runtime: wasmRuntime }) as never, |
| 65 | + ...overrides, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + test("prefers native when it loads", async () => { |
| 70 | + const runtime = await loadAutoRuntime(config, loaders({})); |
| 71 | + expect(runtime).toBe(nativeRuntime); |
| 72 | + }); |
| 73 | + |
| 74 | + test("falls back to wasm when native fails", async () => { |
| 75 | + const runtime = await loadAutoRuntime( |
| 76 | + config, |
| 77 | + loaders({ |
| 78 | + loadNative: async () => { |
| 79 | + throw new Error("missing platform binding"); |
| 80 | + }, |
| 81 | + }), |
| 82 | + ); |
| 83 | + expect(runtime).toBe(wasmRuntime); |
| 84 | + }); |
| 85 | + |
| 86 | + test("reports the native cause when both runtimes fail", async () => { |
| 87 | + // The native failure is the actionable one. Before this was reported, |
| 88 | + // a skipped platform binding surfaced only as the wasm loader's |
| 89 | + // unrelated `file://` fetch error. |
| 90 | + const promise = loadAutoRuntime( |
| 91 | + config, |
| 92 | + loaders({ |
| 93 | + loadNative: async () => { |
| 94 | + throw new Error( |
| 95 | + "Cannot find module '@rivetkit/rivetkit-napi-linux-x64-musl'", |
| 96 | + ); |
| 97 | + }, |
| 98 | + loadWasm: async () => { |
| 99 | + throw new Error("fetch failed"); |
| 100 | + }, |
| 101 | + }), |
| 102 | + ); |
| 103 | + await expect(promise).rejects.toThrow(/rivetkit-napi-linux-x64-musl/); |
| 104 | + await expect(promise).rejects.toThrow(/fetch failed/); |
| 105 | + }); |
| 106 | + |
| 107 | + test("uses wasm directly on an edge-like host without touching native", async () => { |
| 108 | + let nativeCalls = 0; |
| 109 | + const runtime = await loadAutoRuntime( |
| 110 | + config, |
| 111 | + loaders({ |
| 112 | + detectHost: () => "edge-like", |
| 113 | + loadNative: async () => { |
| 114 | + nativeCalls += 1; |
| 115 | + throw new Error( |
| 116 | + "native must not be attempted on edge hosts", |
| 117 | + ); |
| 118 | + }, |
| 119 | + }), |
| 120 | + ); |
| 121 | + expect(runtime).toBe(wasmRuntime); |
| 122 | + expect(nativeCalls).toBe(0); |
| 123 | + }); |
| 124 | +}); |
0 commit comments