signaltrail / tree / test/app.test.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { handle } from '../src/app.js';
import { deps } from './helpers.js';
const call = (d, path, init) => handle(new Request('http://x' + path, init), d);
test('API: run demo dataset, trail, replay, verify, ledger, portfolio, dashboard', async () => {
  const { d } = await deps();
  const r = await call(d, '/v1/run', { method: 'POST', body: '{}' });
  assert.equal(r.status, 200);
  const out = await r.json();
  assert.ok(out.decisions.some((x) => x.action === 'OPEN_PRACTICE_LONG'));
  const t = await (await call(d, '/v1/trail?limit=3')).json(); assert.equal(t.receipts.length, 3);
  const rp = await (await call(d, `/v1/replay/${out.decisions[0].seq}`)).json(); assert.equal(rp.reproducible, true);
  const rec = await (await call(d, `/v1/receipt/${out.decisions[0].seq}`)).json();
  assert.equal((await (await call(d, '/v1/verify', { method: 'POST', body: JSON.stringify(rec) })).json()).valid, true);
  assert.equal((await (await call(d, '/v1/ledger/verify')).json()).valid, true);
  const pf = await (await call(d, '/v1/portfolio')).json(); assert.equal(pf.practice_only, true); assert.equal(pf.positions.length, 2);
  const h = await call(d, '/'); assert.match(h.headers.get('content-type'), /text\/html/);
});
test('API: custom posts, validation errors, 404', async () => {
  const { d } = await deps();
  const r = await call(d, '/v1/run', { method: 'POST', body: JSON.stringify({ posts: [{ author: '@demo_dex_owl', text: '$AERO bullish' }] }) });
  assert.equal(r.status, 200); assert.equal((await r.json()).decisions[0].action, 'SKIP');
  const bad = await call(d, '/v1/run', { method: 'POST', body: JSON.stringify({ posts: [] }) }); assert.equal(bad.status, 400);
  const many = await call(d, '/v1/run', { method: 'POST', body: JSON.stringify({ config: { creators: Array.from({ length: 21 }, (_, i) => ({ handle: '@c' + i, weight: 1 })) } }) }); assert.equal(many.status, 400);
  assert.equal((await call(d, '/nope')).status, 404);
});
test('API: run throttle returns 429', async () => {
  let last = 0; const { d } = await deps({}, { store: Object.assign(new (await import('../src/store.js')).MemoryStore(), { claimRun(ms) { const n = Date.now(); if (n - last < ms) return false; last = n; return true; } }), runThrottleMs: 60000 });
  assert.equal((await call(d, '/v1/run', { method: 'POST', body: '{}' })).status, 200);
  assert.equal((await call(d, '/v1/run', { method: 'POST', body: '{}' })).status, 429);
});

agentgate-source: code mirror on Cloudflare Pages. All repos