Mystery Hunt Box + Ad Detective, badge tiers

Finish the day's Code Hunts to unlock one box a day. It opens to an Ad
Detective case on one site with a fixed 0.15 POL reward shown up front:
visit (the embed records the visit after the dwell, no code), then answer
one question, one attempt. Paid inside the house cap, never counted as a
find. 34-question bank seeded from lib/detective-bank.json; a hunter never
repeats a question until they have seen every one for that site. Admin
panel with per-question stats and retire/restore.

New badges (all ledger-derived): Detective, Sleuth, Investigator, Master
Detective; Scout, Guide, Trail Boss; Week Sweep; 7-Day Streak. Badges
already earned on deploy are marked announced without posting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-26 16:10:17 -05:00
parent 64ab0b505f
commit 469cbf69a5
25 changed files with 1334 additions and 423 deletions
+53 -3
View File
@@ -59,7 +59,7 @@ const sleep = ms => new Promise(r => setTimeout(r, ms));
// claim
const wrong = await call('/api/my/submit', { method: 'POST', body: JSON.stringify({ token: t, code: 'ZZZZZZ' }) }); eq(wrong.status, 400, 'a wrong code is refused');
const ok = await call('/api/my/submit', { method: 'POST', body: JSON.stringify({ token: t, code: code.code }) });
eq([ok.status, ok.body.pol >= 0.05 && ok.body.pol <= 1, ok.body.queued, ok.body.unlocked], [200, true, false, ['first', 'sweep']], 'the right code pays a drip in range, not queued, and unlocks First Find (and Full Sweep: the test has one site)');
eq([ok.status, ok.body.pol >= 0.05 && ok.body.pol <= 1, ok.body.queued, ok.body.unlocked], [200, true, false, ['first', 'sweep', 'weeksweep']], 'the right code pays a drip in range, not queued, and unlocks First Find (and Full Sweep + Week Sweep: the test has one site)');
const twice = await call('/api/my/submit', { method: 'POST', body: JSON.stringify({ token: t, code: code.code }) }); eq(twice.status, 400, 'the same mission cannot be claimed twice');
const b2 = await call('/api/my/board'); eq([b2.body.missions[0].done, b2.body.drips.length, b2.body.drips[0].status], [true, 1, 'due'], 'board: done, one drip due (faucet off, so it waits)');
// missions reset with the Central day: a find dated yesterday no longer blocks the mission today
@@ -92,11 +92,11 @@ const sleep = ms => new Promise(r => setTimeout(r, ms));
// social: the find is on the leaderboard with a badge, the board carries rank and a share link,
// and a share link visit sets the referral cookie that turns sign-ups into that member's IAP join link
const lb = await call('/api/leaders?period=all'); eq([lb.status, lb.body.rows.length >= 1, lb.body.rows[0].memberId, lb.body.rows[0].finds >= 1, lb.body.rows[0].badges.length >= 1], [200, true, 42, true, true], 'leaderboard: hunter 42 leads all time with a badge');
const bd = await call('/api/badges'); eq([bd.body.badges.length, bd.body.badges[0].art], [7, '/badges/badge-first.jpg'], 'seven badges are defined, each with artwork');
const bd = await call('/api/badges'); const NB = require('../lib/social').BADGES.length; eq([bd.body.badges.length, bd.body.badges[0].art, bd.body.badges.every(x => fs.existsSync(path.join(__dirname, '..', 'public', x.art)))], [NB, '/badges/badge-first.jpg', true], NB + ' badges are defined, each with artwork on disk');
// badge cards: the board lists every card with lock state and share URLs; the share page and the
// stamped image exist only for badges the hunter holds
const bc = await call('/api/my/board'); const fc = bc.body.badgeCards.find(x => x.id === 'first'), tc = bc.body.badgeCards.find(x => x.id === 'tracker');
eq([bc.body.badgeCards.length, fc.earned, fc.page, tc.earned, tc.page], [7, true, B + '/b/hunter42/first', false, null], 'board: badge cards with lock state and share page');
eq([bc.body.badgeCards.length, fc.earned, fc.page, tc.earned, tc.page], [NB, true, B + '/b/hunter42/first', false, null], 'board: badge cards with lock state and share page');
const sp = await fetch(B + '/b/hunter42/first'); const spb = await sp.text(); eq([sp.status, /og:image" content="[^"]*\/badge-img\/hunter42\/first\.jpg"/.test(spb), /unlocked/.test(spb)], [200, true, true], 'share page serves with the stamped card as its OG image');
const sp404 = await fetch(B + '/b/hunter42/tracker'); eq(sp404.status, 404, 'no share page for a badge not held');
const bi = await fetch(B + '/badge-img/hunter42/first.jpg'); eq([bi.status, bi.headers.get('content-type')], [200, 'image/jpeg'], 'stamped badge image serves as JPEG');
@@ -122,6 +122,56 @@ const sleep = ms => new Promise(r => setTimeout(r, ms));
const badRef = await fetch(B + '/?r=<script>', { redirect: 'manual' }); eq(/ph\.ref/.test(badRef.headers.get('set-cookie') || ''), false, 'a malformed ref sets no cookie');
for (const pg of ['/leaders', '/promo']) { const r = await fetch(B + pg); eq([r.status, /<title>/.test(await r.text())], [200, true], pg + ' serves'); }
// ---- Mystery Hunt Box + Ad Detective (2026-09-26)
{
const ORG = { Origin: 'http://127.0.0.1:' + PORT };
const bx0 = await call('/api/my/board'); eq([bx0.body.box.on, bx0.body.box.status, bx0.body.box.need, bx0.body.box.done, bx0.body.box.pol], [true, 'locked', 3, 1, 0.15], 'box: locked until today\u2019s 3 hunts are done, fixed 0.15 POL shown');
const op0 = await call('/api/my/box/open', { method: 'POST', body: '{}' }); eq(op0.status, 400, 'box: cannot be opened before the hunts are done');
const qbad = await admin('/api/admin/detective/question', { method: 'POST', body: JSON.stringify({ missionId: 'test-1', q: 'x?', options: ['a', 'b'] }) }); eq(qbad.status, 400, 'admin: a question needs three to five options');
const qok = await admin('/api/admin/detective/question', { method: 'POST', body: JSON.stringify({ id: 'test-1-a', missionId: 'test-1', q: 'What colour is the coin?', options: ['Purple', 'Red', 'Blue', 'Green'], fact: 'the coin is purple' }) }); eq([qok.status, qok.body.question.active], [200, true], 'admin: adds a question to the bank');
await admin('/api/admin/settings', { method: 'POST', body: JSON.stringify({ missionsPerDay: 1 }) });
const bx1 = await call('/api/my/board'); eq(bx1.body.box.status, 'ready', 'box: ready once the day\u2019s hunts are done');
const op1 = await call('/api/my/box/open', { method: 'POST', body: '{}' }); eq([op1.status, op1.body.box.status, op1.body.box.site, op1.body.box.pol, 'question' in op1.body.box], [200, 'open', 'Test site', 0.15, false], 'box: opens to an Ad Detective case with the site and fixed reward, no question yet');
const op2 = await call('/api/my/box/open', { method: 'POST', body: '{}' }); eq(op2.body.box.status, 'open', 'box: opening again returns the same box');
const an0 = await call('/api/my/box/answer', { method: 'POST', body: JSON.stringify({ choice: 0 }) }); eq([an0.status, /Visit the site/.test(an0.body.error)], [400, true], 'answer: refused before the visit is recorded');
const sb = await call('/api/my/box/start', { method: 'POST', body: '{}' }); eq([sb.status, /#ph=[a-f0-9]{32}$/.test(sb.body.url)], [200, true], 'case: Begin issues a token on the site link');
const dt = sb.body.url.split('#ph=')[1];
const sb2 = await call('/api/my/box/start', { method: 'POST', body: '{}' }); eq(sb2.body.url, sb.body.url, 'case: a second Begin reuses the live token (the dwell is not restarted)');
eq((await fetch(B + '/api/embed/code?t=' + dt, { headers: { Origin: 'https://evil.example' } })).status, 403, 'case: embed refuses the wrong origin');
eq(typeof (await (await fetch(B + '/api/embed/code?t=' + dt, { headers: ORG })).json()).wait, 'number', 'case: embed says wait before the dwell');
const bvw = await call('/api/my/board'); eq([bvw.body.box.status, 'question' in bvw.body.box], ['started', false], 'case: no question on the board before the visit');
await sleep(5500);
const rc = await (await fetch(B + '/api/embed/code?t=' + dt, { headers: ORG })).json(); eq([rc.visited, 'code' in rc], [true, false], 'case: after the dwell the embed records the visit and shows no code');
const cs = await call('/api/my/submit', { method: 'POST', body: JSON.stringify({ token: dt, code: 'ABCDEF' }) }); eq(cs.status, 400, 'case: a detective token cannot be claimed as a Code Hunt');
const bq = await call('/api/my/board'); const opts = bq.body.box.options || [];
eq([bq.body.box.status, bq.body.box.question, opts.length, opts.includes('Purple'), 'answer' in bq.body.box], ['visited', 'What colour is the coin?', 4, true, false], 'case: after the visit the question shows with its options, never the answer');
const ansOk = await call('/api/my/box/answer', { method: 'POST', body: JSON.stringify({ choice: opts.indexOf('Purple') }) });
eq([ansOk.status, ansOk.body.correct, ansOk.body.pol, ansOk.body.queued, ansOk.body.unlocked], [200, true, 0.15, false, ['detective']], 'answer: the right option pays the fixed 0.15 POL and unlocks Detective');
const ansTwice = await call('/api/my/box/answer', { method: 'POST', body: JSON.stringify({ choice: 0 }) }); eq(ansTwice.status, 400, 'answer: one answer only');
const bw = await call('/api/my/board'); const dd = bw.body.drips.find(d => String(d.missionId).startsWith('detective:'));
eq([bw.body.box.status, bw.body.box.answer, !!dd, dd && dd.pol, dd && dd.status, bw.body.daily.done, bw.body.rank.all.finds], ['won', 'Purple', true, 0.15, 'due', 1, 1], 'after: box won, a due 0.15 POL drip, and it is NOT a find (daily done and rank finds unchanged)');
const lb3 = await call('/api/leaders?period=all'); eq(lb3.body.rows[0].finds, 1, 'the detective drip is not a find on the leaderboard');
const op3 = await call('/api/my/box/open', { method: 'POST', body: '{}' }); eq(op3.body.box.status, 'won', 'one box a day: opening again after the case is closed returns the closed box');
// a second hunter answers wrong: no payout, the answer is logged
const jar1 = jar; jar = '';
await call('/auth?t=' + sso.mint({ memberId: 45, email: 'h45@example.com', wallet: '0x' + 'cd'.repeat(20), username: 'hunter45' }));
{ const pf = path.join(DIR, 'payouts.json'); const all = JSON.parse(fs.readFileSync(pf, 'utf8')); all.push({ id: 'syn45', memberId: 45, missionId: 'test-2', site: 'Test site', pol: 0.1, day: new Date().toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }), at: Date.now(), status: 'due' }); fs.writeFileSync(pf, JSON.stringify(all)); }
await call('/api/my/box/open', { method: 'POST', body: '{}' }); const s45 = await call('/api/my/box/start', { method: 'POST', body: '{}' }); const t45 = s45.body.url.split('#ph=')[1];
await sleep(5500); await fetch(B + '/api/embed/code?t=' + t45, { headers: ORG });
const q45 = await call('/api/my/board'); const o45 = q45.body.box.options;
const wrongA = await call('/api/my/box/answer', { method: 'POST', body: JSON.stringify({ choice: o45.indexOf('Red') }) });
eq([wrongA.status, wrongA.body.correct, wrongA.body.pol, /The answer was: Purple/.test(wrongA.body.message)], [200, false, 0, true], 'a wrong answer pays nothing and says the right answer');
const b45 = await call('/api/my/board'); eq([b45.body.box.status, b45.body.drips.some(d => String(d.missionId).startsWith('detective:'))], ['missed', false], 'after a wrong answer: box missed, no detective drip');
jar = jar1;
const st = await admin('/api/admin/detective'); const qa = st.body.questions.find(q => q.id === 'test-1-a');
eq([qa.asked, qa.correct, st.body.today.boxes, st.body.today.won, st.body.today.missed], [2, 1, 2, 1, 1], 'admin stats: asked 2, correct 1; two boxes today, one won, one missed');
// variety: hunter 42 has answered test-1-a, so a new question on the same site is picked next time
await admin('/api/admin/detective/question', { method: 'POST', body: JSON.stringify({ id: 'test-1-b', missionId: 'test-1', q: 'Second?', options: ['Yes', 'No', 'Maybe'] }) });
{ require('../lib/store').init(DIR); const det = require('../lib/detective'); const picks = ['2099-01-01', '2099-01-02', '2099-01-03'].map(d => det.pickCase(42, d).qid); eq(picks, ['test-1-b', 'test-1-b', 'test-1-b'], 'variety: a hunter gets the question they have not seen, whatever the day'); }
const rt = await admin('/api/admin/detective/question', { method: 'POST', body: JSON.stringify({ id: 'test-1-b', active: false }) }); eq(rt.status, 200, 'admin: a question can be retired');
await admin('/api/admin/settings', { method: 'POST', body: JSON.stringify({ missionsPerDay: 3 }) });
}
// nothing outward: no telegram env, no faucet env
const led = await call('/api/ledger'); eq(led.body.totals.paid, 0, 'nothing has been paid, because no faucet is configured');