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
1.9 KiB41 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { EventEmitter } from 'node:events';
4import { createLocalShare, NO_KEY_WARNING } from '../server/local-share.mjs';
5import { createCloudflare, NO_KEY_TUNNEL_WARNING } from '../server/cloudflare.mjs';
6
7test('sharing on the network works without an API key and says so; with a key there is no warning', async () => {
8  let key = false;
9  const share = createLocalShare({ handle: async (req, res) => res.end('ok'), hasKey: () => key, host: '127.0.0.1', port: 0 });
10  try {
11    const started = await share.start();
12    assert.equal(started.active, true);
13    assert.equal(started.warning, NO_KEY_WARNING);
14    key = true;
15    assert.equal(share.state().warning, null);
16  } finally { await share.stop(); }
17});
18
19test('a Cloudflare tunnel connects without an API key and carries the stronger warning', async () => {
20  let key = false;
21  const origin = { start: async () => {}, stop: async () => {} };
22  const launch = () => {
23    const child = new EventEmitter();
24    child.stdout = new EventEmitter(); child.stderr = new EventEmitter();
25    child.kill = () => { setImmediate(() => child.emit('exit')); };
26    setImmediate(() => child.stderr.emit('data', 'https://demo-abc.trycloudflare.com\nRegistered tunnel connection\n'));
27    return child;
28  };
29  const tunnel = createCloudflare({ origin, originUrl: 'http://127.0.0.1:1', hasKey: () => key, launch, locate: async () => '/bin/cloudflared', timeoutMs: 5000 });
30  try {
31    const first = await tunnel.start({ mode: 'quick' });
32    assert.ok(['starting', 'connected'].includes(first.status));
33    await new Promise(resolve => setTimeout(resolve, 50));
34    const now = tunnel.state();
35    assert.equal(now.status, 'connected');
36    assert.equal(now.warning, NO_KEY_TUNNEL_WARNING);
37    assert.match(NO_KEY_TUNNEL_WARNING, /internet/);
38    key = true;
39    assert.equal(tunnel.state().warning, null);
40  } finally { await tunnel.stop(); }
41});