@1agh/maude 0.17.1 → 0.17.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.
@@ -0,0 +1,134 @@
1
+ // Regression test for writeCompileEntry — the per-target build helper that
2
+ // works around Bun 1.3.4+ --compile NAPI native-binding embedding (DDR-NNN-
3
+ // oxc-parser-bun-compile-workaround). The helper itself is pure (no compile,
4
+ // no spawn): given a target, it writes two files (`init-oxc-<slug>.ts` +
5
+ // `server-<slug>.ts`) under dist/.compile-entries/ and returns the entry path.
6
+
7
+ import { existsSync, readFileSync, rmSync } from 'node:fs';
8
+ import { dirname, join } from 'node:path';
9
+ import { fileURLToPath } from 'node:url';
10
+
11
+ import { describe, expect, test } from 'bun:test';
12
+
13
+ import { writeCompileEntry } from '../build.ts';
14
+
15
+ const ROOT = dirname(fileURLToPath(import.meta.url));
16
+ const DEV_SERVER_ROOT = join(ROOT, '..');
17
+ const ENTRY_DIR = join(DEV_SERVER_ROOT, 'dist', '.compile-entries');
18
+
19
+ // PlatformTarget union mirrored from build.ts. Kept inline so the test fails
20
+ // loudly if build.ts adds/removes a target without updating the test.
21
+ const ALL_TARGETS = [
22
+ 'bun-darwin-arm64',
23
+ 'bun-darwin-x64',
24
+ 'bun-linux-x64',
25
+ 'bun-linux-arm64',
26
+ 'bun-linux-x64-musl',
27
+ 'bun-linux-arm64-musl',
28
+ 'bun-windows-x64',
29
+ ] as const;
30
+
31
+ // Maude-slug → @oxc-parser/binding-<X> NAPI slug. Mirrored from
32
+ // build.ts:oxcBindingSlug — kept in sync intentionally.
33
+ const EXPECTED_OXC_SLUG: Record<string, string> = {
34
+ 'darwin-arm64': 'darwin-arm64',
35
+ 'darwin-x64': 'darwin-x64',
36
+ 'linux-x64': 'linux-x64-gnu',
37
+ 'linux-arm64': 'linux-arm64-gnu',
38
+ 'linux-x64-musl': 'linux-x64-musl',
39
+ 'linux-arm64-musl': 'linux-arm64-musl',
40
+ 'win32-x64': 'win32-x64-msvc',
41
+ };
42
+
43
+ function maudeSlug(target: string): string {
44
+ const s = target.replace(/^bun-/, '');
45
+ return s === 'windows-x64' ? 'win32-x64' : s;
46
+ }
47
+
48
+ describe('writeCompileEntry', () => {
49
+ test('produces init + entry files for every supported target', () => {
50
+ for (const target of ALL_TARGETS) {
51
+ const slug = maudeSlug(target);
52
+ const initPath = join(ENTRY_DIR, `init-oxc-${slug}.ts`);
53
+ const entryPath = join(ENTRY_DIR, `server-${slug}.ts`);
54
+
55
+ const returned = writeCompileEntry(target);
56
+ expect(returned).toBe(entryPath);
57
+ expect(existsSync(initPath)).toBe(true);
58
+ expect(existsSync(entryPath)).toBe(true);
59
+ }
60
+ });
61
+
62
+ test('init file embeds the matching oxc binding via with-type-file', () => {
63
+ for (const target of ALL_TARGETS) {
64
+ const slug = maudeSlug(target);
65
+ const oxcSlug = EXPECTED_OXC_SLUG[slug];
66
+ const initPath = join(ENTRY_DIR, `init-oxc-${slug}.ts`);
67
+
68
+ writeCompileEntry(target);
69
+ const content = readFileSync(initPath, 'utf8');
70
+
71
+ // Asset embed via Bun's with-type-file syntax — load-bearing for the
72
+ // workaround. Without `with { type: 'file' }` the import resolves to
73
+ // the .node's exports rather than its filesystem path.
74
+ expect(content).toContain(
75
+ `import bindingPath from "@oxc-parser/binding-${oxcSlug}/parser.${oxcSlug}.node" with { type: 'file' };`
76
+ );
77
+ // Env var must be set so oxc-parser's NAPI-RS loader (bindings.js)
78
+ // skips its broken platform-detection switch.
79
+ expect(content).toContain('process.env.NAPI_RS_NATIVE_LIBRARY_PATH = bindingPath;');
80
+ }
81
+ });
82
+
83
+ test('entry file imports init BEFORE server.ts (ESM evaluation order matters)', () => {
84
+ for (const target of ALL_TARGETS) {
85
+ const slug = maudeSlug(target);
86
+ const entryPath = join(ENTRY_DIR, `server-${slug}.ts`);
87
+
88
+ writeCompileEntry(target);
89
+ const content = readFileSync(entryPath, 'utf8');
90
+
91
+ const initIdx = content.indexOf(`./init-oxc-${slug}.ts`);
92
+ const serverIdx = content.indexOf('server.ts');
93
+ expect(initIdx).toBeGreaterThanOrEqual(0);
94
+ expect(serverIdx).toBeGreaterThanOrEqual(0);
95
+ // If server.ts is imported before init-oxc, oxc-parser evaluates first
96
+ // and reads NAPI_RS_NATIVE_LIBRARY_PATH before our env-var setter runs.
97
+ expect(initIdx).toBeLessThan(serverIdx);
98
+ }
99
+ });
100
+
101
+ test('entry uses POSIX path separators in the server.ts import', () => {
102
+ writeCompileEntry('bun-windows-x64');
103
+ const content = readFileSync(join(ENTRY_DIR, 'server-win32-x64.ts'), 'utf8');
104
+ // Even on a Windows host the generated import specifier must use forward
105
+ // slashes — ESM specifiers are not OS paths.
106
+ expect(content).not.toMatch(/\\/);
107
+ });
108
+
109
+ test('idempotent — calling twice yields identical content', () => {
110
+ const target = 'bun-darwin-arm64';
111
+ const slug = maudeSlug(target);
112
+
113
+ writeCompileEntry(target);
114
+ const a1 = readFileSync(join(ENTRY_DIR, `init-oxc-${slug}.ts`), 'utf8');
115
+ const e1 = readFileSync(join(ENTRY_DIR, `server-${slug}.ts`), 'utf8');
116
+
117
+ writeCompileEntry(target);
118
+ const a2 = readFileSync(join(ENTRY_DIR, `init-oxc-${slug}.ts`), 'utf8');
119
+ const e2 = readFileSync(join(ENTRY_DIR, `server-${slug}.ts`), 'utf8');
120
+
121
+ expect(a2).toBe(a1);
122
+ expect(e2).toBe(e1);
123
+ });
124
+
125
+ test('cleanup — generated files live under dist/.compile-entries/', () => {
126
+ // Sanity: the helper writes only to the expected directory; nothing
127
+ // leaks elsewhere. Sweep a stale prior dir, regenerate, verify scope.
128
+ rmSync(ENTRY_DIR, { recursive: true, force: true });
129
+ writeCompileEntry('bun-darwin-arm64');
130
+ expect(existsSync(ENTRY_DIR)).toBe(true);
131
+ expect(existsSync(join(ENTRY_DIR, 'init-oxc-darwin-arm64.ts'))).toBe(true);
132
+ expect(existsSync(join(ENTRY_DIR, 'server-darwin-arm64.ts'))).toBe(true);
133
+ });
134
+ });