// HEL-875 isolated contract mock for her reach-outs on an open page. Serves web/dist and the persisted-message // contract of the HEL-870 backend: GET /api/messages pages newest-first by row id (limit+1 probe, rows oldest-first, // next_before) and marks only her reach-out rows proactive:true (api/app.py, life/public.py). Synthetic data only: // no model, no embeddings, no network. // Usage: node tests/hel875_mock.mjs . Control: POST-free GETs /__reach?text=, /__epoch, /__down/, /__bulk/, /__log. import { createServer } from 'node:http'; import { readFile } from 'node:fs/promises'; import { extname, join, normalize } from 'node:path'; import { fileURLToPath } from 'node:url'; const dist = fileURLToPath(new URL('../dist/', import.meta.url)); const port = Number(process.argv[2] || 8875); const T0 = Date.UTC(2026, 9, 25, 12, 0); const identity = { id: '1', name: '栖', birth: '2000-03-14', hour: '子', relation: '朋友', nickname: '', boundary: '', color: { pillars: { year: '庚辰', month: '己卯', day: '丙午', hour: '戊子' }, traits: '合成测试档案' } }; let epoch = 3, down = 0; const log = [], rows = []; const add = row => { const id = rows.length + 1; rows.push({ id, ts: T0 + id * 60e3, status: 'sent', lifeIds: [], ...row }); return id; }; // History: two turns and one reach-out she left long before this browser first opened the page. add({ role: 'user', text: '下雨天的时候,你会想听什么?', turn_id: '00000000-0000-4000-8000-000000000001' }); add({ role: 'companion', text: '会想把窗开一条缝,听雨落在铁皮檐上。你呢,今天累不累?', turn_id: '00000000-0000-4000-8000-000000000001' }); add({ role: 'companion', text: '昨晚的月亮很亮,想起你说过喜欢夜里散步。', turn_id: 'reach-old', proactive: true }); add({ role: 'user', text: '今天加班到九点,好累。', turn_id: '00000000-0000-4000-8000-000000000002' }); add({ role: 'companion', text: '那先别急着做别的,喝口热水,慢慢跟我说。', turn_id: '00000000-0000-4000-8000-000000000002' }); const dto = r => ({ id: r.id, ts: r.ts, role: r.role, text: r.text, turn_id: r.turn_id, status: r.status, lifeIds: r.lifeIds, ...(r.proactive ? { proactive: true } : {}) }); const send = (res, status, body, type = 'application/json; charset=utf-8') => { res.writeHead(status, { 'Content-Type': type, 'Cache-Control': 'no-store' }); res.end(typeof body === 'string' || Buffer.isBuffer(body) ? body : JSON.stringify(body)); }; const sleep = ms => new Promise(r => setTimeout(r, ms)); createServer(async (req, res) => { const url = new URL(req.url, 'http://x'), p = url.pathname; if (!p.startsWith('/__')) log.push(`${req.method} ${p}${url.search}`); if (p === '/__log') return send(res, 200, log.splice(0)); if (p === '/__rows') return send(res, 200, rows.map(dto)); if (p === '/__reach') { const id = add({ role: 'companion', text: url.searchParams.get('text') || '窗外的雨停了,想跟你说一声。', turn_id: `reach-${rows.length + 1}`, proactive: true }); return send(res, 200, { id }); } // HEL-880: a long absence elsewhere — n plain turns (no reach-outs) land at once. if (p.startsWith('/__bulk/')) { const n = Number(p.slice(8)); for (let i = 0; i < n; i++) add({ role: i % 2 ? 'companion' : 'user', text: `别处的第${i + 1}句`, turn_id: `bulk-${rows.length + 1}` }); return send(res, 200, { top: rows.length }); } if (p === '/__epoch') { epoch++; return send(res, 200, { epoch }); } if (p.startsWith('/__down/')) { down = Number(p.slice(8)); return send(res, 200, { down }); } if (p === '/api/health') return send(res, 200, { ok: true, version: 'R4-assembly', epoch, llm: { ok: true, model: 'mock' }, backend: { life: true, persona: true, receipt: true, web_connected: true, mood: true, reach_out: true, semantic: false, semantic_backend: null }, life_runtime: { state: 'running', timezone: 'Asia/Shanghai', budget_warning: false, budget_stopped: false, last: null }, capabilities: { text: true, proactive: true, life: false, voice: false, vision: false, weather: false, materials: false, backup: false, memory: true, semantic_memory: null } }); if (p === '/api/companion') return send(res, 200, identity); if (p === '/api/messages') { if (down > 0) { down--; return send(res, 503, { detail: 'Service Unavailable' }); } const limit = Math.min(200, Number(url.searchParams.get('limit') || 200)), before = Number(url.searchParams.get('before') || Number.MAX_SAFE_INTEGER); const desc = rows.filter(r => r.id < before).sort((a, b) => b.id - a.id).slice(0, limit + 1), more = desc.length > limit, page = desc.slice(0, limit); return send(res, 200, { messages: [...page].reverse().map(dto), next_before: more ? page[page.length - 1].id : null, epoch }); } if (p === '/api/persona') return send(res, 200, { state: 'ready', interpretation: null, seeds: [], growth: [] }); if (p === '/api/life') return send(res, 200, { events: [], next_before: null, epoch }); if (p === '/api/memories') return send(res, 200, { memories: [], next_before: null, epoch }); if (p === '/api/chat' && req.method === 'POST') { let body = ''; for await (const chunk of req) body += chunk; const { text: said, turn_id } = JSON.parse(body), reply = '我在。今天的雨停了没有?要是还在下,就慢点回家。'; add({ role: 'user', text: said, turn_id, status: 'pending' }); res.writeHead(200, { 'Content-Type': 'text/event-stream' }); // A slow stream (about 4 s), so a reach-out can land in the middle of her answer. for (const part of reply.match(/.{1,4}/gu)) { res.write(`data: ${JSON.stringify({ t: 'chat.delta', epoch, turn_id, delta: part })}\n\n`); await sleep(330); } add({ role: 'companion', text: reply, turn_id }); rows.forEach(r => { if (r.turn_id === turn_id) r.status = 'sent'; }); res.end(`data: ${JSON.stringify({ t: 'chat.final', epoch, turn_id, utterance: { text: reply, speech_tags: [] }, receipt: null })}\n\n`); return; } if (p.startsWith('/api/')) return send(res, 404, { detail: 'Not Found' }); const file = normalize(join(dist, p === '/' ? 'index.html' : p)); if (!file.startsWith(normalize(dist))) return send(res, 403, 'no', 'text/plain'); try { send(res, 200, await readFile(file), { '.js': 'text/javascript', '.css': 'text/css', '.html': 'text/html; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.webp': 'image/webp' }[extname(file)] || 'application/octet-stream'); } catch { send(res, 200, await readFile(join(dist, 'index.html')), 'text/html; charset=utf-8'); } }).listen(port, '127.0.0.1', () => console.log(`hel875 mock on ${port}`));