Skip to content

Commit b933b0a

Browse files
committed
chore: fix tests and back to acorn
1 parent e069593 commit b933b0a

File tree

9 files changed

+234
-309
lines changed

9 files changed

+234
-309
lines changed

npm-shrinkwrap.json

Lines changed: 15 additions & 293 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@orama/orama": "^3.1.16",
4848
"@orama/react-components": "^0.8.1",
4949
"@rollup/plugin-virtual": "^3.0.2",
50-
"oxc-parser": "^0.101.0",
50+
"acorn": "^8.15.0",
5151
"commander": "^14.0.2",
5252
"dedent": "^1.7.0",
5353
"eslint-plugin-react-x": "^2.3.12",

src/generators/api-links/__tests__/fixtures.test.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { readdir } from 'node:fs/promises';
2+
import { cpus } from 'node:os';
23
import { basename, extname, join } from 'node:path';
34
import { describe, it } from 'node:test';
45

6+
import WorkerPool from '../../../threading/index.mjs';
7+
import createParallelWorker from '../../../threading/parallel.mjs';
58
import astJs from '../../ast-js/index.mjs';
69
import apiLinks from '../index.mjs';
710

@@ -16,8 +19,16 @@ describe('api links', () => {
1619
describe('should work correctly for all fixtures', () => {
1720
sourceFiles.forEach(sourceFile => {
1821
it(`${basename(sourceFile)}`, async t => {
22+
const pool = new WorkerPool('../chunk-worker.mjs', cpus().length);
23+
24+
const worker = createParallelWorker('ast-js', pool, {
25+
threads: 1,
26+
chunkSize: 10,
27+
});
28+
1929
const astJsResult = await astJs.generate(undefined, {
2030
input: [sourceFile],
31+
worker,
2132
});
2233

2334
const actualOutput = await apiLinks.generate(astJsResult, {

src/generators/web/utils/bundle.mjs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,7 @@ export default async function bundleCode(codeMap, { server = false } = {}) {
8484
resolve: {
8585
// Alias react imports to preact/compat for smaller bundle sizes.
8686
// Explicit jsx-runtime aliases are required for the automatic JSX transform.
87-
alias: {
88-
react: 'preact/compat',
89-
'react-dom': 'preact/compat',
90-
'react/jsx-runtime': 'preact/jsx-runtime',
91-
'react/jsx-dev-runtime': 'preact/jsx-dev-runtime',
92-
},
87+
alias: { react: 'preact/compat' },
9388

9489
// Tell the bundler where to find node_modules.
9590
// This ensures packages are found when running doc-kit from external directories

src/logger/__tests__/transports/console.test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import console from '../../transports/console.mjs';
66

77
describe('console', () => {
88
it('should print debug messages', t => {
9+
process.env.FORCE_COLOR = '1';
10+
911
t.mock.timers.enable({ apis: ['Date'] });
1012

1113
const fn = t.mock.method(process.stdout, 'write');
@@ -33,6 +35,8 @@ describe('console', () => {
3335
});
3436

3537
it('should print info messages', t => {
38+
process.env.FORCE_COLOR = '1';
39+
3640
t.mock.timers.enable({ apis: ['Date'] });
3741
const fn = t.mock.method(process.stdout, 'write');
3842

@@ -59,6 +63,8 @@ describe('console', () => {
5963
});
6064

6165
it('should print error messages ', t => {
66+
process.env.FORCE_COLOR = '1';
67+
6268
t.mock.timers.enable({ apis: ['Date'] });
6369

6470
const fn = t.mock.method(process.stdout, 'write');
@@ -86,6 +92,8 @@ describe('console', () => {
8692
});
8793

8894
it('should print fatal messages', t => {
95+
process.env.FORCE_COLOR = '1';
96+
8997
t.mock.timers.enable({ apis: ['Date'] });
9098

9199
const fn = t.mock.method(process.stdout, 'write');
@@ -113,6 +121,8 @@ describe('console', () => {
113121
});
114122

115123
it('should print messages with file', t => {
124+
process.env.FORCE_COLOR = '1';
125+
116126
t.mock.timers.enable({ apis: ['Date'] });
117127

118128
const fn = t.mock.method(process.stdout, 'write');
@@ -151,6 +161,8 @@ describe('console', () => {
151161
});
152162

153163
it('should print child logger name', t => {
164+
process.env.FORCE_COLOR = '1';
165+
154166
t.mock.timers.enable({ apis: ['Date'] });
155167

156168
const fn = t.mock.method(process.stdout, 'write');

src/logger/__tests__/transports/github.test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import github from '../../transports/github.mjs';
66

77
describe('github', () => {
88
it('should print debug messages', t => {
9+
process.env.FORCE_COLOR = '1';
10+
911
t.mock.timers.enable({ apis: ['Date'] });
1012

1113
const fn = t.mock.method(process.stdout, 'write');
@@ -31,6 +33,8 @@ describe('github', () => {
3133
});
3234

3335
it('should print info messages', t => {
36+
process.env.FORCE_COLOR = '1';
37+
3438
t.mock.timers.enable({ apis: ['Date'] });
3539
const fn = t.mock.method(process.stdout, 'write');
3640

@@ -55,6 +59,8 @@ describe('github', () => {
5559
});
5660

5761
it('should print error messages ', t => {
62+
process.env.FORCE_COLOR = '1';
63+
5864
t.mock.timers.enable({ apis: ['Date'] });
5965

6066
const fn = t.mock.method(process.stdout, 'write');
@@ -80,6 +86,8 @@ describe('github', () => {
8086
});
8187

8288
it('should print fatal messages', t => {
89+
process.env.FORCE_COLOR = '1';
90+
8391
t.mock.timers.enable({ apis: ['Date'] });
8492

8593
const fn = t.mock.method(process.stdout, 'write');
@@ -105,6 +113,8 @@ describe('github', () => {
105113
});
106114

107115
it('should print messages with file', t => {
116+
process.env.FORCE_COLOR = '1';
117+
108118
t.mock.timers.enable({ apis: ['Date'] });
109119

110120
const fn = t.mock.method(process.stdout, 'write');
@@ -139,6 +149,8 @@ describe('github', () => {
139149
});
140150

141151
it('should print child logger name', t => {
152+
process.env.FORCE_COLOR = '1';
153+
142154
t.mock.timers.enable({ apis: ['Date'] });
143155
const fn = t.mock.method(process.stdout, 'write');
144156

src/parsers/javascript.mjs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { parseSync } from 'oxc-parser';
3+
import * as acorn from 'acorn';
44

55
/**
66
* Creates a Javascript source parser for a given source file
@@ -23,15 +23,13 @@ const createParser = () => {
2323
);
2424
}
2525

26-
const result = parseSync(resolvedSourceFile.path, resolvedSourceFile.value);
26+
const res = acorn.parse(resolvedSourceFile.value, {
27+
allowReturnOutsideFunction: true,
28+
ecmaVersion: 'latest',
29+
locations: true,
30+
});
2731

28-
if (result.errors.length > 0) {
29-
throw new Error(
30-
`Failed to parse ${resolvedSourceFile.path}: ${result.errors[0].message}`
31-
);
32-
}
33-
34-
return { ...result.program, path: resolvedSourceFile.path };
32+
return { ...res, path: resolvedSourceFile.path };
3533
};
3634

3735
/**
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
2+
import { describe, it } from 'node:test';
3+
4+
import WorkerPool from '../index.mjs';
5+
6+
describe('WorkerPool', () => {
7+
// Use relative path from WorkerPool's location (src/threading/)
8+
const workerPath = './chunk-worker.mjs';
9+
10+
it('should create a worker pool with specified thread count', () => {
11+
const pool = new WorkerPool(workerPath, 4);
12+
13+
strictEqual(pool.threads, 4);
14+
strictEqual(pool.getActiveThreadCount(), 0);
15+
});
16+
17+
it('should initialize with zero active threads', () => {
18+
const pool = new WorkerPool(workerPath, 2);
19+
20+
strictEqual(pool.getActiveThreadCount(), 0);
21+
});
22+
23+
it('should change active thread count atomically', () => {
24+
const pool = new WorkerPool(workerPath, 2);
25+
26+
pool.changeActiveThreadCount(1);
27+
strictEqual(pool.getActiveThreadCount(), 1);
28+
29+
pool.changeActiveThreadCount(2);
30+
strictEqual(pool.getActiveThreadCount(), 3);
31+
32+
pool.changeActiveThreadCount(-1);
33+
strictEqual(pool.getActiveThreadCount(), 2);
34+
});
35+
36+
it('should queue tasks when thread limit is reached', async () => {
37+
const pool = new WorkerPool(workerPath, 1);
38+
39+
const task1 = pool.run({
40+
generatorName: 'ast-js',
41+
fullInput: [],
42+
itemIndices: [],
43+
options: {},
44+
});
45+
46+
const task2 = pool.run({
47+
generatorName: 'ast-js',
48+
fullInput: [],
49+
itemIndices: [],
50+
options: {},
51+
});
52+
53+
const results = await Promise.all([task1, task2]);
54+
55+
ok(Array.isArray(results));
56+
strictEqual(results.length, 2);
57+
});
58+
59+
it('should run multiple tasks in parallel with runAll', async () => {
60+
const pool = new WorkerPool(workerPath, 2);
61+
62+
const tasks = [
63+
{
64+
generatorName: 'ast-js',
65+
fullInput: [],
66+
itemIndices: [],
67+
options: {},
68+
},
69+
{
70+
generatorName: 'ast-js',
71+
fullInput: [],
72+
itemIndices: [],
73+
options: {},
74+
},
75+
];
76+
77+
const results = await pool.runAll(tasks);
78+
79+
ok(Array.isArray(results));
80+
strictEqual(results.length, 2);
81+
});
82+
83+
it('should handle empty task array', async () => {
84+
const pool = new WorkerPool(workerPath, 2);
85+
86+
const results = await pool.runAll([]);
87+
88+
deepStrictEqual(results, []);
89+
});
90+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
2+
import { describe, it } from 'node:test';
3+
4+
import WorkerPool from '../index.mjs';
5+
import createParallelWorker from '../parallel.mjs';
6+
7+
describe('createParallelWorker', () => {
8+
// Use relative path from WorkerPool's location (src/threading/)
9+
const workerPath = './chunk-worker.mjs';
10+
11+
it('should create a ParallelWorker with map method', () => {
12+
const pool = new WorkerPool(workerPath, 2);
13+
14+
const worker = createParallelWorker('metadata', pool, { threads: 2 });
15+
16+
ok(worker);
17+
strictEqual(typeof worker.map, 'function');
18+
});
19+
20+
it('should use main thread for single-threaded execution', async () => {
21+
const pool = new WorkerPool(workerPath, 1);
22+
23+
const worker = createParallelWorker('ast-js', pool, { threads: 1 });
24+
const items = [];
25+
const results = await worker.map(items, items, {});
26+
27+
ok(Array.isArray(results));
28+
strictEqual(results.length, 0);
29+
});
30+
31+
it('should use main thread for small item counts', async () => {
32+
const pool = new WorkerPool(workerPath, 4);
33+
34+
const worker = createParallelWorker('ast-js', pool, { threads: 4 });
35+
const items = [];
36+
const results = await worker.map(items, items, {});
37+
38+
ok(Array.isArray(results));
39+
strictEqual(results.length, 0);
40+
});
41+
42+
it('should chunk items for parallel processing', async () => {
43+
const pool = new WorkerPool(workerPath, 2);
44+
45+
const worker = createParallelWorker('ast-js', pool, { threads: 2 });
46+
const items = [];
47+
48+
const results = await worker.map(items, items, {});
49+
50+
strictEqual(results.length, 0);
51+
ok(Array.isArray(results));
52+
});
53+
54+
it('should pass extra options to worker', async () => {
55+
const pool = new WorkerPool(workerPath, 1);
56+
57+
const worker = createParallelWorker('ast-js', pool, { threads: 1 });
58+
const extra = { gitRef: 'main', customOption: 'value' };
59+
const items = [];
60+
61+
const results = await worker.map(items, items, extra);
62+
63+
ok(Array.isArray(results));
64+
});
65+
66+
it('should serialize and deserialize data correctly', async () => {
67+
const pool = new WorkerPool(workerPath, 2);
68+
69+
const worker = createParallelWorker('ast-js', pool, { threads: 2 });
70+
const items = [];
71+
72+
const results = await worker.map(items, items, {});
73+
74+
ok(Array.isArray(results));
75+
});
76+
77+
it('should handle empty items array', async () => {
78+
const pool = new WorkerPool(workerPath, 2);
79+
80+
const worker = createParallelWorker('ast-js', pool, { threads: 2 });
81+
const results = await worker.map([], [], {});
82+
83+
deepStrictEqual(results, []);
84+
});
85+
});

0 commit comments

Comments
 (0)