'use strict'; /* Regression: sharing a report when no reportCleanup event is pending used to throw (ensureCleanup needs ctx.enqueue, which a plain * command ctx lacks) and surface as a 500. Found in a browser run with hand-made reports. */ const test = require('node:test'); const assert = require('node:assert'); const T = require('./military-helpers'); const A = require('../server/alliances'); test('reportShare works when no cleanup event is pending and then schedules one', () => { const h = T.setup(); const a = T.addOperator(h, 'alice'); const al = Number(h.db.run('INSERT INTO alliances (world_id, name, tag, size_cap, created_by, created_at) VALUES (?, ?, ?, 10, ?, ?)', h.worldId, 'Alpha', 'ALP', a.opId, h.clock.now()).lastInsertRowid); A.addMember(h.db, al, a.opId, 'leader', h.clock.now()); const rid = Number(h.db.run('INSERT INTO reports (world_id, type, created_at, expires_at, summary, data) VALUES (?, ?, ?, ?, ?, ?)', h.worldId, 'scan', h.clock.now(), h.clock.now() + 1000, '{}', '{}').lastInsertRowid); h.db.run('INSERT INTO report_recipients (report_id, user_id, role) VALUES (?, ?, ?)', rid, a.userId, 'attacker'); assert.equal(h.db.get("SELECT COUNT(*) n FROM events WHERE type = 'reportCleanup' AND status = 'pending'").n, 0); const r = h.run('reportShare', { userId: a.userId, reportId: rid }); assert.equal(r.changed, true); assert.equal(h.db.get("SELECT COUNT(*) n FROM events WHERE type = 'reportCleanup' AND status = 'pending'").n, 1); assert.equal(h.run('reportShare', { userId: a.userId, reportId: rid }).changed, false); });