Spaces:
Running

bet / test /api.test.js
ismizo's picture
Deploy Veto Audit run selector, /api/runs/:runId, and full Audit/Replay flow (a3561c8)
f5ecc1c verified
Raw History Blame Contribute Delete
7.16 kB
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { createApp } from '../server.js';
function startTestServer() {
return new Promise((resolve) => {
const app = createApp();
const server = http.createServer(app);
server.listen(0, '127.0.0.1', () => {
const { port } = server.address();
resolve({ server, baseUrl: `http://127.0.0.1:${port}` });
});
});
}
test('personal-only Express API routes, auth gate, cron secret, and JSON preview', async () => {
const prevPw = process.env.APP_PASSWORD;
const prevCron = process.env.CRON_SECRET;
process.env.APP_PASSWORD = 'test-personal-password';
process.env.CRON_SECRET = 'test-cron-secret';
const { server, baseUrl } = await startTestServer();
try {
// 1. Public health endpoint (pinged by FrenchSchool)
const health = await fetch(`${baseUrl}/api/health`).then((r) => r.json());
assert.equal(health.status, 'ok');
// 2. Public config endpoint
const cfg = await fetch(`${baseUrl}/api/config`).then((r) => r.json());
assert.equal(cfg.authRequired, true);
assert.equal(cfg.schedule.scoutTime, '13:00 EAT');
assert.equal(cfg.schedule.settleTime, '02:00 EAT');
// 3. Protected dashboard rejects unauthenticated call
const unauth = await fetch(`${baseUrl}/api/dashboard`);
assert.equal(unauth.status, 401);
// 4. Cron endpoint rejects missing/wrong secret
const badCron = await fetch(`${baseUrl}/api/trigger-cron?secret=wrong`);
assert.equal(badCron.status, 401);
// 5. Unlock with password and call preview-json
const unlock = await fetch(`${baseUrl}/api/auth/unlock`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password: 'test-personal-password' }),
}).then((r) => r.json());
assert.equal(unlock.unlocked, true);
assert.ok(unlock.token);
const preview = await fetch(`${baseUrl}/api/scout/preview-json`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${unlock.token}`,
},
body: JSON.stringify({
preset: 'sniper',
payload: {
slateDate: '2026-11-24',
matches: [
{
id: 'm1',
kickoffEAT: '20:00 EAT',
league: 'UEFA Champions League',
tier: 1,
isReserveOrYouth: false,
isPostIntlBreak: false,
hasVerifiedTeamNews: true,
home: {
name: 'Real Madrid',
gp: 12,
gf: 24,
ga: 14,
xg: 23.0,
xga: 15.0,
managerGamesInCharge: 40,
newManagerTightenedDefense: false,
confirmedOut: { gk: 0, startingCB: 0, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: [] },
doubts5050: { gk: 0, startingCB: 0, otherDef: 0, topScorer: 0, otherAtt: 0, names: [] },
},
away: {
name: 'PSV',
gp: 12,
gf: 22,
ga: 16,
xg: 21.0,
xga: 16.0,
managerGamesInCharge: 30,
newManagerTightenedDefense: false,
confirmedOut: { gk: 0, startingCB: 0, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: [] },
doubts5050: { gk: 0, startingCB: 0, otherDef: 0, topScorer: 0, otherAtt: 0, names: [] },
},
moneyline: { home: 1.55, draw: 4.3, away: 5.5 },
totals: { '2.5': { over: 1.56, under: 2.45, sportybetOver: 1.55 } },
weather: { tempC: 12, windKmh: 8, precipType: 'none' },
},
{
id: 'm2',
kickoffEAT: '22:00 EAT',
league: 'UEFA Champions League',
tier: 1,
isReserveOrYouth: false,
isPostIntlBreak: false,
hasVerifiedTeamNews: true,
home: {
name: 'Bayern Munich',
gp: 12,
gf: 26,
ga: 14,
xg: 25.0,
xga: 15.0,
managerGamesInCharge: 35,
newManagerTightenedDefense: false,
confirmedOut: { gk: 0, startingCB: 0, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: [] },
doubts5050: { gk: 0, startingCB: 0, otherDef: 0, topScorer: 0, otherAtt: 0, names: [] },
},
away: {
name: 'Celtic',
gp: 12,
gf: 21,
ga: 16,
xg: 20.5,
xga: 16.0,
managerGamesInCharge: 40,
newManagerTightenedDefense: false,
confirmedOut: { gk: 0, startingCB: 0, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: [] },
doubts5050: { gk: 0, startingCB: 0, otherDef: 0, topScorer: 0, otherAtt: 0, names: [] },
},
moneyline: { home: 1.48, draw: 4.6, away: 6.2 },
totals: { '2.5': { over: 1.72, under: 2.15, sportybetOver: 1.70 } },
weather: { tempC: 10, windKmh: 7, precipType: 'none' },
},
],
},
}),
}).then((r) => r.json());
assert.equal(preview.matchCount, 2);
assert.equal(preview.optimization.recommendation, 'BET');
assert.equal(preview.optimization.primarySlip.legs.length, 1);
assert.equal(preview.optimization.alternativeSlip, null);
assert.equal(preview.optimization.primarySlip.metrics.consensusOdds, 1.56);
// 6. History filtering endpoint (week default + bad range validation)
const histWeek = await fetch(`${baseUrl}/api/history?mode=week&date=2026-11-24`, {
headers: { Authorization: `Bearer ${unlock.token}` },
}).then((r) => r.json());
assert.equal(histWeek.period.mode, 'week');
assert.equal(histWeek.period.startDate, '2026-11-23');
assert.equal(histWeek.period.endDate, '2026-11-29');
const badRange = await fetch(`${baseUrl}/api/history?mode=range&from=2026-11-28&to=2026-11-01`, {
headers: { Authorization: `Bearer ${unlock.token}` },
});
assert.equal(badRange.status, 400);
// 7. Dashboard includes auditRun and single-run lookup enforces auth / DB state
const dash = await fetch(`${baseUrl}/api/dashboard`, {
headers: { Authorization: `Bearer ${unlock.token}` },
}).then((r) => r.json());
assert.equal(dash.auditRun, null);
const unauthRun = await fetch(`${baseUrl}/api/runs/123`);
assert.equal(unauthRun.status, 401);
const offlineRun = await fetch(`${baseUrl}/api/runs/not-an-id`, {
headers: { Authorization: `Bearer ${unlock.token}` },
});
assert.equal(offlineRun.status, 503);
} finally {
server.closeAllConnections?.();
server.close();
if (prevPw === undefined) delete process.env.APP_PASSWORD;
else process.env.APP_PASSWORD = prevPw;
if (prevCron === undefined) delete process.env.CRON_SECRET;
else process.env.CRON_SECRET = prevCron;
}
});