122 lines
6.7 KiB
JavaScript
122 lines
6.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { spawn, execFileSync } from 'node:child_process';
|
|
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join, resolve, isAbsolute } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { chooseFolder, cleanChosenPath, typedFolder, windowsFolderScript } from '../server/folder-dialog.mjs';
|
|
|
|
const HOST_ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
|
const failure = (code) => Object.assign(new Error(`exit ${code}`), { code });
|
|
const recorder = () => {
|
|
const seen = [];
|
|
return { seen, run: (outcome) => async (command, args) => { seen.push({ command, args }); if (outcome instanceof Error) throw outcome; return { stdout: outcome }; } };
|
|
};
|
|
|
|
test('macOS keeps the osascript chooser and treats any dismissal as a cancel', async () => {
|
|
const c = recorder();
|
|
assert.deepEqual(await chooseFolder('Pick', { platform: 'darwin', run: c.run('/Users/me/skills/\n') }), { path: '/Users/me/skills' });
|
|
assert.equal(c.seen[0].command, 'osascript');
|
|
assert.equal(c.seen[0].args[1], 'POSIX path of (choose folder with prompt "Pick")');
|
|
assert.deepEqual(await chooseFolder('Pick', { platform: 'darwin', run: c.run(failure(1)) }), { cancelled: true });
|
|
});
|
|
|
|
test('Windows uses a PowerShell FolderBrowserDialog passed as an encoded command', async () => {
|
|
const c = recorder();
|
|
const result = await chooseFolder("Don't panic", { platform: 'win32', run: c.run('C:\\Users\\me\\Skills\r\n') });
|
|
assert.deepEqual(result, { path: 'C:\\Users\\me\\Skills' });
|
|
assert.match(c.seen[0].command, /powershell\.exe$/i);
|
|
assert.ok(c.seen[0].args.includes('-STA'));
|
|
const script = Buffer.from(c.seen[0].args.at(-1), 'base64').toString('utf16le');
|
|
assert.equal(script, windowsFolderScript("Don't panic"));
|
|
assert.match(script, /FolderBrowserDialog/);
|
|
assert.match(script, /Description = 'Don''t panic'/);
|
|
assert.deepEqual(await chooseFolder('Pick', { platform: 'win32', run: c.run('') }), { cancelled: true });
|
|
const missing = await chooseFolder('Pick', { platform: 'win32', run: c.run(failure('ENOENT')) });
|
|
assert.equal(missing.unavailable, true);
|
|
await assert.rejects(chooseFolder('Pick', { platform: 'win32', run: c.run(failure(2)) }), /could not open/);
|
|
});
|
|
|
|
test('the generated PowerShell parses without errors', { skip: process.platform !== 'win32' }, () => {
|
|
const shell = join(process.env.SystemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
|
|
const out = execFileSync(shell,
|
|
['-NoProfile', '-NonInteractive', '-Command', '$e=$null; [void][System.Management.Automation.Language.Parser]::ParseInput($env:SCRIPT,[ref]$null,[ref]$e); $e.Count'],
|
|
{ encoding: 'utf8', env: { ...process.env, SCRIPT: windowsFolderScript('Choose a folder for Strata') }, windowsHide: true }).trim();
|
|
assert.equal(out, '0');
|
|
});
|
|
|
|
test('Linux tries zenity, falls back to kdialog, then reports that none exists', async () => {
|
|
const order = [];
|
|
const run = async (command) => { order.push(command); if (command === 'zenity') throw failure('ENOENT'); return { stdout: '/home/me/skills/\n' }; };
|
|
assert.deepEqual(await chooseFolder('Pick', { platform: 'linux', run }), { path: '/home/me/skills' });
|
|
assert.deepEqual(order, ['zenity', 'kdialog']);
|
|
const none = await chooseFolder('Pick', { platform: 'linux', run: async () => { throw failure('ENOENT'); } });
|
|
assert.equal(none.unavailable, true);
|
|
assert.match(none.reason, /zenity or kdialog/);
|
|
assert.deepEqual(await chooseFolder('Pick', { platform: 'linux', run: async () => { throw failure(1); } }), { cancelled: true });
|
|
assert.deepEqual(await chooseFolder('Pick', { platform: 'linux', run: async () => ({ stdout: '' }) }), { cancelled: true });
|
|
});
|
|
|
|
test('chosen paths lose trailing separators but roots survive', () => {
|
|
assert.equal(cleanChosenPath('/a/b/\n'), '/a/b');
|
|
assert.equal(cleanChosenPath('/'), '/');
|
|
assert.equal(cleanChosenPath('C:\\Users\\me\\'), 'C:\\Users\\me');
|
|
assert.equal(cleanChosenPath('C:\\'), 'C:\\');
|
|
});
|
|
|
|
test('a typed path must be an absolute existing directory', async () => {
|
|
const dir = await mkdtemp(join(tmpdir(), 'strata-typed-'));
|
|
try {
|
|
await writeFile(join(dir, 'file.txt'), 'x');
|
|
assert.equal(await typedFolder(dir, { isAbsolute }), dir);
|
|
assert.equal(await typedFolder(undefined, { isAbsolute }), null);
|
|
await assert.rejects(typedFolder('relative/dir', { isAbsolute }), /full path/);
|
|
await assert.rejects(typedFolder(join(dir, 'missing'), { isAbsolute }), /does not exist/);
|
|
await assert.rejects(typedFolder(join(dir, 'file.txt'), { isAbsolute }), /not a file/);
|
|
} finally { await rm(dir, { recursive: true, force: true }); }
|
|
});
|
|
|
|
async function startHost() {
|
|
const home = await mkdtemp(join(tmpdir(), 'strata-folder-home-'));
|
|
const net = await import('node:net');
|
|
const probe = net.createServer();
|
|
await new Promise(r => probe.listen(0, '127.0.0.1', r));
|
|
const { port } = probe.address();
|
|
await new Promise(r => probe.close(r));
|
|
const child = spawn(process.execPath, [join(HOST_ROOT, 'server', 'index.mjs'), '--dist', join(HOST_ROOT, 'dist')], {
|
|
cwd: HOST_ROOT, env: { ...process.env, STRATA_HOME: home, STRATA_WORKSPACE: home, STRATA_PORT: String(port), STRATA_REAL_ENGINE: '0' }, stdio: 'ignore',
|
|
});
|
|
const base = `http://127.0.0.1:${port}`;
|
|
for (let i = 0; i < 150; i++) {
|
|
try { if ((await fetch(`${base}/api/health`)).ok) return { child, base, home }; } catch { /* not up yet */ }
|
|
await new Promise(r => setTimeout(r, 100));
|
|
}
|
|
child.kill();
|
|
throw new Error('the host did not become healthy');
|
|
}
|
|
|
|
test('the host accepts a typed folder path for the picker and for adding a skill, on every OS', { timeout: 60000 }, async () => {
|
|
const { child, base, home } = await startHost();
|
|
const skill = await mkdtemp(join(tmpdir(), 'strata-skill-'));
|
|
try {
|
|
const post = (path, body) => fetch(`${base}${path}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
|
|
let r = await post('/api/workspaces/pick', { path: skill });
|
|
assert.deepEqual(await r.json(), { path: skill });
|
|
r = await post('/api/workspaces/pick', { path: join(skill, 'nope') });
|
|
assert.equal(r.status, 400);
|
|
assert.match((await r.json()).error, /does not exist/);
|
|
r = await post('/api/skills/add', { path: skill });
|
|
assert.equal(r.status, 400);
|
|
assert.match((await r.json()).error, /no SKILL\.md/);
|
|
await writeFile(join(skill, 'SKILL.md'), '---\nname: Typed\n---\nbody\n');
|
|
r = await post('/api/skills/add', { path: skill });
|
|
assert.equal(r.status, 200);
|
|
assert.ok((await r.json()).added);
|
|
} finally {
|
|
child.kill();
|
|
await rm(skill, { recursive: true, force: true });
|
|
await rm(home, { recursive: true, force: true }).catch(() => {});
|
|
}
|
|
});
|