workers-port 0.1.0 → 0.1.1
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 +308 -236
- package/bin/workers.js +19 -3
- package/demo/serve.js +9 -3
- package/package.json +1 -1
- package/src/loop.js +2 -2
- package/src/mutants.js +5 -1
- package/src/port.js +191 -180
- package/src/report.js +1 -1
- package/src/rules.md +1 -0
- package/src/run-kane.js +9 -5
- package/src/run-playwright.js +7 -3
- package/src/server.js +24 -23
- package/src/translate.js +83 -5
- package/src/util.js +9 -5
package/src/server.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
|
-
import { killTree } from './util.js';
|
|
3
|
-
|
|
4
|
-
async function up(url) {
|
|
5
|
-
try {
|
|
6
|
-
return (await fetch(url, { signal: AbortSignal.timeout(1500) })).ok;
|
|
7
|
-
} catch {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// Make sure the app is being served at `url`. Returns a stop() function.
|
|
13
|
-
export async function ensureServer({ url, command, cwd }) {
|
|
14
|
-
if (await up(url)) return async () => {};
|
|
15
|
-
if (!command) throw new Error(`Nothing is serving ${url} and the Playwright config has no webServer.command to start it.`);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
await
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { killTree } from './util.js';
|
|
3
|
+
|
|
4
|
+
async function up(url) {
|
|
5
|
+
try {
|
|
6
|
+
return (await fetch(url, { signal: AbortSignal.timeout(1500) })).ok;
|
|
7
|
+
} catch {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Make sure the app is being served at `url`. Returns a stop() function.
|
|
13
|
+
export async function ensureServer({ url, command, cwd, env }) {
|
|
14
|
+
if (await up(url)) return async () => {};
|
|
15
|
+
if (!command) throw new Error(`Nothing is serving ${url} and the Playwright config has no webServer.command to start it.`);
|
|
16
|
+
// webServer.env from the Playwright config (for example PORT) applies here too
|
|
17
|
+
const child = spawn(command, { cwd, shell: true, stdio: 'ignore', windowsHide: true, env: { ...process.env, ...(env ?? {}) } });
|
|
18
|
+
for (let i = 0; i < 40; i++) {
|
|
19
|
+
if (await up(url)) return async () => killTree(child);
|
|
20
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
21
|
+
}
|
|
22
|
+
killTree(child);
|
|
23
|
+
throw new Error(`App did not come up at ${url} using "${command}".`);
|
|
24
|
+
}
|
package/src/translate.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { readFileSync } from 'node:fs';
|
|
2
|
-
import { dirname, join } from 'node:path';
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
|
|
5
5
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
@@ -70,9 +70,86 @@ export function extractSource(src, line) {
|
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
// ---- imported local modules (page objects, fixtures) -----------------------------------------
|
|
74
|
+
|
|
75
|
+
const EXTS = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.mts'];
|
|
76
|
+
const IMPORT_RE = /(?:\bimport|\bexport)\s[^'"`;]*?\bfrom\s*['"]([^'"]+)['"]|\bimport\s*['"]([^'"]+)['"]|\brequire\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
77
|
+
|
|
78
|
+
const isFile = (p) => existsSync(p) && statSync(p).isFile();
|
|
79
|
+
|
|
80
|
+
// Resolve a relative import specifier to an existing file, trying extensions and index files.
|
|
81
|
+
export function resolveImport(fromFile, spec) {
|
|
82
|
+
if (!spec.startsWith('.')) return null; // package imports (and node_modules) are never followed
|
|
83
|
+
const base = resolve(dirname(fromFile), spec);
|
|
84
|
+
const candidates = [base, ...EXTS.map((e) => base + e), ...EXTS.map((e) => join(base, 'index' + e))];
|
|
85
|
+
const stripped = base.replace(/\.(m?js|jsx)$/, ''); // './x.js' may really be x.ts
|
|
86
|
+
if (stripped !== base) candidates.push(...EXTS.map((e) => stripped + e));
|
|
87
|
+
return candidates.find((c) => isFile(c)) ?? null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function importSpecifiers(src) {
|
|
91
|
+
const out = [];
|
|
92
|
+
let m;
|
|
93
|
+
IMPORT_RE.lastIndex = 0;
|
|
94
|
+
while ((m = IMPORT_RE.exec(src))) out.push(m[1] ?? m[2] ?? m[3]);
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Local files the spec imports (one level), plus what those import (one more level). Deduplicated,
|
|
99
|
+
// capped at maxBytes in total. Returns {files: [{path, source}], truncated}; path is relative to the spec's folder.
|
|
100
|
+
export function collectImports(specFile, { maxBytes = 40 * 1024, depth = 2 } = {}) {
|
|
101
|
+
const root = dirname(resolve(specFile));
|
|
102
|
+
const seen = new Set([resolve(specFile)]);
|
|
103
|
+
const files = [];
|
|
104
|
+
let used = 0;
|
|
105
|
+
let truncated = false;
|
|
106
|
+
let level = [resolve(specFile)];
|
|
107
|
+
for (let d = 0; d < depth; d++) {
|
|
108
|
+
const next = [];
|
|
109
|
+
for (const from of level) {
|
|
110
|
+
for (const spec of importSpecifiers(readFileSync(from, 'utf8'))) {
|
|
111
|
+
const file = resolveImport(from, spec);
|
|
112
|
+
if (!file || seen.has(file)) continue;
|
|
113
|
+
seen.add(file);
|
|
114
|
+
next.push(file);
|
|
115
|
+
let source = readFileSync(file, 'utf8');
|
|
116
|
+
const room = maxBytes - used;
|
|
117
|
+
if (room <= 0) {
|
|
118
|
+
truncated = true;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (Buffer.byteLength(source) > room) {
|
|
122
|
+
source = Buffer.from(source).subarray(0, room).toString('utf8') + '\n// ... truncated (size cap reached)';
|
|
123
|
+
truncated = true;
|
|
124
|
+
}
|
|
125
|
+
used += Buffer.byteLength(source);
|
|
126
|
+
files.push({ path: relative(root, file).split(sep).join('/'), source });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
level = next;
|
|
130
|
+
}
|
|
131
|
+
return { files, truncated };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Prompt section for the collected imports ('' when there are none).
|
|
135
|
+
export function formatImports({ files, truncated }) {
|
|
136
|
+
if (!files.length) return '';
|
|
137
|
+
const blocks = files.map((f) => `// ===== FILE: ${f.path} =====
|
|
138
|
+
${f.source.trim()}`).join('\n\n');
|
|
139
|
+
const note = truncated ? ' Some files were omitted or cut because of a size cap.' : '';
|
|
140
|
+
return `
|
|
141
|
+
# Imported local files (page objects, fixtures, helpers)
|
|
142
|
+
The test uses these files. Expand every page-object method, fixture and helper it calls into the concrete steps and values it performs.${note}
|
|
143
|
+
|
|
144
|
+
\`\`\`ts
|
|
145
|
+
${blocks}
|
|
146
|
+
\`\`\`
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
|
|
73
150
|
// ---- prompt --------------------------------------------------------------------------------
|
|
74
151
|
|
|
75
|
-
export function buildTranslatePrompt({ title, source, url, rules = loadRules() }) {
|
|
152
|
+
export function buildTranslatePrompt({ title, source, url, imports = '', rules = loadRules() }) {
|
|
76
153
|
return `You convert a Playwright test into a Kane CLI test file (\`_test.md\`).
|
|
77
154
|
|
|
78
155
|
# Translation rules
|
|
@@ -87,7 +164,7 @@ The code below contains this one test plus its enclosing describe blocks, hooks
|
|
|
87
164
|
\`\`\`ts
|
|
88
165
|
${source}
|
|
89
166
|
\`\`\`
|
|
90
|
-
|
|
167
|
+
${imports}
|
|
91
168
|
# Output
|
|
92
169
|
Return ONLY the full contents of the \`_test.md\` file, starting with the \`---\` frontmatter line. No commentary, no code fences.`;
|
|
93
170
|
}
|
|
@@ -130,7 +207,8 @@ export async function translateTest({ test, url, agent, model }) {
|
|
|
130
207
|
const src = readFileSync(test.file, 'utf8');
|
|
131
208
|
const { source } = extractSource(src, test.line);
|
|
132
209
|
const expectCount = countExpects(source);
|
|
133
|
-
const
|
|
210
|
+
const imports = formatImports(collectImports(test.file));
|
|
211
|
+
const base = buildTranslatePrompt({ title: test.title, source, url, imports });
|
|
134
212
|
let prompt = base;
|
|
135
213
|
let problems = [];
|
|
136
214
|
for (let attempt = 1; attempt <= 2; attempt++) {
|
package/src/util.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
|
|
3
|
-
function quote(arg) {
|
|
3
|
+
export function quote(arg, win = process.platform === 'win32') {
|
|
4
4
|
const s = String(arg);
|
|
5
|
-
|
|
5
|
+
if (/^[\w@+=:,./\\-]+$/.test(s)) return s;
|
|
6
|
+
// POSIX: single quotes, nothing inside is interpreted (no $(), backticks or $VAR expansion).
|
|
7
|
+
if (!win) return `'${s.replace(/'/g, `'\\''`)}'`;
|
|
8
|
+
return `"${s.replace(/"/g, '\\"')}"`;
|
|
6
9
|
}
|
|
7
10
|
|
|
8
11
|
// Run a command (shell on Windows so .cmd shims like npx/claude work).
|
|
@@ -15,7 +18,8 @@ export function run(cmd, args = [], { cwd, env, input, timeoutMs, onLine } = {})
|
|
|
15
18
|
let stderr = '';
|
|
16
19
|
let buf = '';
|
|
17
20
|
let timer;
|
|
18
|
-
|
|
21
|
+
let timedOut = false;
|
|
22
|
+
if (timeoutMs) timer = setTimeout(() => ((timedOut = true), killTree(child)), timeoutMs);
|
|
19
23
|
child.stdout.on('data', (d) => {
|
|
20
24
|
const s = d.toString();
|
|
21
25
|
stdout += s;
|
|
@@ -27,11 +31,11 @@ export function run(cmd, args = [], { cwd, env, input, timeoutMs, onLine } = {})
|
|
|
27
31
|
}
|
|
28
32
|
});
|
|
29
33
|
child.stderr.on('data', (d) => (stderr += d.toString()));
|
|
30
|
-
child.on('error', (e) => resolve({ code: 127, stdout, stderr: stderr + e.message }));
|
|
34
|
+
child.on('error', (e) => resolve({ code: 127, stdout, stderr: stderr + e.message, timedOut }));
|
|
31
35
|
child.on('close', (code) => {
|
|
32
36
|
clearTimeout(timer);
|
|
33
37
|
if (onLine && buf) onLine(buf);
|
|
34
|
-
resolve({ code: code ?? 1, stdout, stderr });
|
|
38
|
+
resolve({ code: code ?? 1, stdout, stderr, timedOut });
|
|
35
39
|
});
|
|
36
40
|
child.stdin.end(input !== undefined ? input : undefined);
|
|
37
41
|
});
|