-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.spec.ts
More file actions
104 lines (88 loc) · 2.65 KB
/
Copy pathvite.spec.ts
File metadata and controls
104 lines (88 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { describe, expect, mock, test } from "bun:test";
import stllAhoCorasickWasmVite, {
WASM_VITE_PACKAGES,
buildAhoCorasickWasmViteConfig,
} from "../src/vite";
import { injectWasmFetchGuard } from "../src/wasm-fetch-guard";
describe("stllAhoCorasickWasmVite", () => {
test("merges optimizeDeps.exclude and ssr.external", () => {
const config = buildAhoCorasickWasmViteConfig({
optimizeDeps: { exclude: ["existing-package"] },
ssr: { external: ["server-only-dep"] },
});
expect(config.optimizeDeps?.exclude).toEqual([
"existing-package",
...WASM_VITE_PACKAGES,
]);
expect(config.ssr?.external).toEqual([
"server-only-dep",
...WASM_VITE_PACKAGES,
]);
});
test("preserves ssr.external=true", () => {
const config = buildAhoCorasickWasmViteConfig({
ssr: { external: true },
});
expect(config.ssr?.external).toBe(true);
});
test("plugin returns merged vite config", () => {
const plugin = stllAhoCorasickWasmVite();
expect(plugin.name).toBe("stll-aho-corasick-wasm");
expect(
plugin.config?.({
optimizeDeps: { exclude: ["existing-package"] },
}),
).toEqual({
optimizeDeps: {
exclude: [...WASM_VITE_PACKAGES],
},
ssr: {
external: [...WASM_VITE_PACKAGES],
},
});
});
});
describe("injectWasmFetchGuard", () => {
test("wraps the napi-rs fetch with a wasm byte check", () => {
const code = `
const bytes = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
`;
const transformed = injectWasmFetchGuard(
code,
"aho-corasick.wasi-browser.js",
"@stll/aho-corasick-wasm",
);
expect(transformed).toContain(
"const view = new Uint8Array(bytes)",
);
expect(transformed).toContain("view[0] !== 0x00");
expect(transformed).toContain(
"@stll/aho-corasick-wasm/vite",
);
});
test("warns when napi-rs loader format changes", () => {
const warn = mock();
const transformed = injectWasmFetchGuard(
"const bytes = await fetch(url)",
"aho-corasick.wasi-browser.js",
"@stll/aho-corasick-wasm",
warn,
);
expect(transformed).toBeNull();
expect(warn).toHaveBeenCalledTimes(1);
expect(warn.mock.calls[0]?.[0]).toContain(
"The magic-bytes guard was not applied",
);
});
test("ignores non-wasi browser loaders", () => {
const warn = mock();
const transformed = injectWasmFetchGuard(
"await fetch(__wasmUrl).then((res) => res.arrayBuffer())",
"other-file.js",
"@stll/aho-corasick-wasm",
warn,
);
expect(transformed).toBeNull();
expect(warn).not.toHaveBeenCalled();
});
});