okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-022-refusals: environmental refusals repair or warn instead of blocking (audit items 1-4, 6, 8-10, 12-15)bcf88b738

main
5.5 KiB77 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { EventEmitter } from 'node:events';
4import { readFile } from 'node:fs/promises';
5import { createRequire } from 'node:module';
6import { pathToFileURL } from 'node:url';
7import { transform } from 'esbuild';
8import { createCloudflare, tunnelHostname } from '../server/cloudflare.mjs';
9import { saveSnapshot, snapshotBytes } from '../server/snapshot.mjs';
10
11function fixture(extra = {}) {
12  let stopped = 0, launched;
13  const child = new EventEmitter(); child.stdout = new EventEmitter(); child.stderr = new EventEmitter();
14  child.kill = () => { queueMicrotask(() => child.emit('exit', 0)); };
15  const connector = createCloudflare({ origin: { start: async () => {}, stop: async () => { stopped++; } }, originUrl: 'http://127.0.0.1:8793', hasKey: () => true, locate: async () => '/test/cloudflared', launch: (...args) => { launched = args; return child; }, ...extra });
16  return { connector, child, launched: () => launched, stopped: () => stopped };
17}
18test('quick tunnel publishes its URL only after registration, and stops its private origin', async () => {
19  const f = fixture();
20  await f.connector.start({ mode: 'quick' });
21  f.child.stderr.emit('data', 'URL https://quiet-field.trycloud');
22  f.child.stderr.emit('data', 'flare.com\n');
23  assert.equal(f.connector.state().status, 'starting');
24  assert.equal(f.connector.state().url, null);
25  f.child.stderr.emit('data', 'Registered tunnel connection connIndex=0\n');
26  assert.equal(f.connector.state().url, 'https://quiet-field.trycloudflare.com/v1');
27  assert.equal(f.connector.state().status, 'connected');
28  await assert.rejects(f.connector.start({ mode: 'quick' }), /Stop/);
29  await f.connector.stop();
30  assert.equal(f.connector.state().status, 'idle'); assert.equal(f.stopped(), 1);
31});
32test('domain tunnel validates hostnames and keeps the token out of arguments and state', async () => {
33  for (const value of ['https://ai.example.com', 'example.com/path', 'localhost', 'ai.example.com;echo x']) assert.throws(() => tunnelHostname(value));
34  const f = fixture(), token = 'test-only-tunnel-token-12345';
35  await f.connector.start({ mode: 'domain', hostname: 'ai.example.com', token });
36  assert.equal(f.launched()[2].env.TUNNEL_TOKEN, token);
37  assert.ok(!JSON.stringify(f.launched()[1]).includes(token));
38  assert.ok(!JSON.stringify(f.connector.state()).includes(token));
39  f.child.stderr.emit('data', 'Registered tunnel connection\n');
40  assert.equal(f.connector.state().url, 'https://ai.example.com/v1');
41  await f.connector.stop();
42});
43test('missing prerequisites and connector failure leave no inference listener running', async () => {
44  const noKey = fixture({ hasKey: () => false });          // without a key the tunnel starts (owner decision, 0.2.2) and the state carries a warning
45  await noKey.connector.start({ mode: 'quick' });
46  assert.notEqual(noKey.launched(), undefined);
47  const missing = fixture({ locate: async () => null });
48  await assert.rejects(missing.connector.start({ mode: 'quick' }), /connector could not be prepared automatically/i);
49  assert.equal(missing.connector.state().status, 'error');
50  const failed = fixture(); await failed.connector.start({ mode: 'quick' });
51  failed.child.emit('exit', 1); await new Promise(resolve => setImmediate(resolve));
52  assert.equal(failed.connector.state().status, 'error'); assert.equal(failed.stopped(), 1);
53});
54test('snapshot validates PNGs, writes only a dialog-selected path, and respects Cancel', async () => {
55  const png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aQ1sAAAAASUVORK5CYII=';
56  assert.throws(() => snapshotBytes('data:image/png;base64,ZXZpbA=='));
57  let writes = [];
58  const saved = await saveSnapshot(png, { platform: 'darwin', run: async () => ({ stdout: '/tmp/user-selected.png\n' }), write: async (...args) => { writes.push(args); } });
59  assert.equal(saved.saved, true); assert.equal(writes[0][0], '/tmp/user-selected.png'); assert.deepEqual(writes[0][1], snapshotBytes(png));
60  const cancelled = await saveSnapshot(png, { platform: 'darwin', run: async () => { throw { stderr: 'User canceled. (-128)' }; }, write: async () => { throw new Error('Must not write'); } });
61  assert.equal(cancelled.cancelled, true);
62});
63test('shortcut overrides persist, clear, restore, and change the composer send combination', async () => {
64  const memory = new Map(); globalThis.localStorage = { getItem: k => memory.get(k), setItem: (k,v) => memory.set(k,v), removeItem: k => memory.delete(k) };
65  const require = createRequire(import.meta.url);
66  const source = (await readFile(new URL('../src/lib/shortcut-bindings.ts', import.meta.url), 'utf8')).replace("from 'react'", `from '${pathToFileURL(require.resolve('react')).href}'`);
67  const { code } = await transform(source, { loader: 'ts', format: 'esm' });
68  const m = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
69  const enter = { key: 'Enter', ctrlKey: false, metaKey: false, altKey: false, shiftKey: false };
70  assert.equal(m.composerSends(enter), true);
71  m.setBinding('chat:send', { key: 'enter', mod: true, shift: false, alt: false });
72  assert.equal(m.composerSends(enter), false); assert.equal(m.composerSends({ ...enter, metaKey: true }), true);
73  assert.ok(memory.get('strata.shortcuts.v1').includes('chat:send'));
74  m.setBinding('new', null); assert.equal(m.bindingFor('new', { key: 'n' }), null);
75  m.resetBindings(); assert.equal(m.composerSends(enter), true); assert.equal(memory.size, 0);
76  delete globalThis.localStorage;
77});