T-HUB/infra/peertube-workers/tests/test_observer.cjs
glass beadian aa5e262f3a Tactical Media Broadcast Hub — initial release
Self-hosted multi-platform broadcast newsroom for activist collectives:
publishing pipeline with group approval and DST-proof scheduling, TweetDeck-style
column wall with hostile-content-safe rendering, live control room with Auto
Director, bridged chat governance with polls and quorum, distributed sandboxed
transcode workers, encrypted content-addressed archive, monthly analytics.

MIT licensed. See README.md and docs/.
2026-10-10 11:56:42 +00:00

62 lines
3.3 KiB
JavaScript

'use strict';
// Real loopback transfers exercise routing, byte counts, file/job correlation, and secret exclusion.
const fs = require('node:fs');
const http = require('node:http');
const path = require('node:path');
const os = require('node:os');
const assert = require('node:assert/strict');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'pt-observer-'));
const uuid = '11111111-1111-4111-8111-111111111111';
const source = Buffer.alloc(300000, 7);
let uploaded = 0;
const server = http.createServer((req, res) => {
if (req.url.includes('/files/abort')) {
res.writeHead(200, { 'Content-Length': '1000000' });
res.write(Buffer.alloc(1000)); setTimeout(() => res.destroy(), 20);
} else if (req.url.includes('/files/')) { res.writeHead(200); res.end(source); }
else {
req.on('data', chunk => { uploaded += chunk.length; });
req.on('end', () => { res.writeHead(204); res.end(); });
}
});
server.listen(0, '127.0.0.1', async () => {
const port = server.address().port;
const cfg = path.join(tmp, 'worker.json');
fs.writeFileSync(cfg, JSON.stringify({ canonical_origin: 'https://canonical.invalid', peertube_url: `http://127.0.0.1:${port}` }));
process.env.PT_WORKER_CONFIG = cfg;
process.env.PT_STATE_DIR = tmp;
require('../observe.cjs');
const https = require('node:https');
try {
await new Promise((ok, fail) => {
const req = https.request(`https://canonical.invalid/api/v1/runners/jobs/${uuid}/files/videos/source?secret=NEVER_RECORD_ME`, res => {
const file = fs.createWriteStream(path.join(tmp, 'source'));
res.pipe(file); file.on('finish', ok);
});
req.on('error', fail); req.end('runnerToken=NEVER_RECORD_ME');
});
await new Promise((ok, fail) => {
const req = http.request({ hostname: '127.0.0.1', port, path: `/api/v1/runners/jobs/${uuid}/success`, method: 'POST' }, res => {
res.resume(); res.on('end', ok);
});
req.on('error', fail);
req.write(Buffer.alloc(100000, 1)); req.end(Buffer.alloc(200000, 2));
});
const events = fs.readFileSync(path.join(tmp, 'events.jsonl'), 'utf8');
assert(!events.includes('NEVER_RECORD_ME'));
const parsed = events.trim().split('\n').map(JSON.parse);
assert.equal(parsed.find(e => e.kind === 'transfer' && e.direction === 'download').bytes, source.length);
assert.equal(parsed.find(e => e.kind === 'transfer' && e.direction === 'upload').bytes, uploaded);
assert.equal(uploaded, 300000);
assert.equal(JSON.parse(fs.readFileSync(path.join(tmp, 'inputs/source.json'))).uuid, uuid);
await new Promise((ok, fail) => {
const timeout = setTimeout(() => fail(new Error('interrupted transfer did not fail promptly')), 2000);
const req = http.request({ hostname: '127.0.0.1', port, path: `/api/v1/runners/jobs/${uuid}/files/abort` }, res => res.resume());
req.on('error', () => { clearTimeout(timeout); ok(); }); req.end();
});
const failed = fs.readFileSync(path.join(tmp, 'events.jsonl'), 'utf8').trim().split('\n').map(JSON.parse);
assert(failed.some(e => e.kind === 'transfer' && !e.ok));
console.log('PASS: real download/upload counts, Tailscale-origin routing, UUID correlation, interrupted-transfer failure, no secrets');
} catch (e) { console.error(e); process.exitCode = 1; }
finally { server.close(); fs.rmSync(tmp, { recursive: true, force: true }); }
});