refactor(persistence): migrate fulfillment-store to canonical atomic-write util (DC-103) [glm-grade=A]
CI / Test & Lint (push) Canceled after 0s
CI / Security audit (push) Canceled after 0s

writeState() drops its private tmp+writeFileSync+rename copy (no fsync,
Date.now() tmp names, best-effort chmod) and delegates to
src/utils/atomic-write.js atomicWriteJSON (exclusive-create tmp, fsync,
rename, parent-dir fsync, 0600). A torn stripe-fulfillments.json could
previously make a webhook retry mint a SECOND valid license key for an
order that already has one. Both consumers (routes/billing.js,
scripts/stripe-license-bridge.js) JSON.parse only - trailing-newline
drop in the canonical serializer is harmless. Unused crypto require
removed. +2 DC-103 pins: full lifecycle 0600/complete/zero-leftovers,
and dual-instance interleaved writes (bridge+API file-IPC) with no tmp
collisions. 121 suites / 2776 tests green.

Judge: GLM-5.3 round-1 A (deleg_bd49a97b), URN urn:ump:layk7h326sqymcsh6tiusrs2sdbqdcx6ygvcuwwoxqn4rf7ycoua
This commit is contained in:
Hermes
2026-08-23 01:21:28 -07:00
parent 521b2f24a1
commit 6f8fac142f
2 changed files with 68 additions and 10 deletions
@@ -360,3 +360,65 @@ describe('DC-102 share-store on canonical atomic-write (real fs)', () => {
expect(leftovers).toEqual([]);
});
});
// DC-103: fulfillment-store (Stripe license state, shared file-IPC between
// the API's lookup endpoint and the stripe-license-bridge process) migrated
// off its private tmp+rename copy onto the canonical writer. Pins: the file
// lands at 0600, parses as complete JSON after every mutation class, and no
// temp files survive — a torn write here would make a webhook retry mint a
// SECOND valid license key for an order that already has one.
describe('DC-103 fulfillment-store on canonical atomic-write (real fs)', () => {
let dir, store;
beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dc103-fulfill-'));
store = require('../src/billing/fulfillment-store').createFulfillmentStore({ filePath: path.join(dir, 'stripe-fulfillments.json') });
});
afterEach(() => { try { fs.rmSync(dir, { recursive: true, force: true }); } catch (_) {} });
test('claim → saveLicense → claimDelivery → markDelivered lands at 0600, complete JSON, no temp leftovers', async () => {
const claimed = await store.claim({ eventId: 'evt_dc103', sessionId: 'cs_dc103', productId: 'pro-30d', durationDays: 30, email: 'dc103@x.com' });
expect(claimed.claimed).toBe(true);
const saved = await store.saveLicense({ eventId: 'evt_dc103', sessionId: 'cs_dc103', code: 'DC103-KEY-XXXX', codeId: 'kg_dc103' });
expect(saved.saved).toBe(true);
const delivery = await store.claimDelivery({ sessionId: 'cs_dc103', ownerToken: 'own_1' });
expect(delivery.claimed).toBe(true);
const delivered = await store.markDelivered({ sessionId: 'cs_dc103', ownerToken: 'own_1', deliveredVia: 'smtp' });
expect(delivered.saved).toBe(true);
const file = path.join(dir, 'stripe-fulfillments.json');
const st = fs.statSync(file);
expect(st.mode & 0o777).toBe(0o600);
// complete JSON carrying the full lifecycle — a torn write would fail
// JSON.parse right here
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
expect(data.bySessionId['cs_dc103'].status).toBe('delivered');
expect(data.bySessionId['cs_dc103'].code).toBe('DC103-KEY-XXXX');
expect(data.bySessionId['cs_dc103'].eventId).toBe('evt_dc103');
// both index maps point at the same record
expect(data.byEventId['evt_dc103'].sessionId).toBe('cs_dc103');
const leftovers = fs.readdirSync(dir).filter((f) => f !== 'stripe-fulfillments.json');
expect(leftovers).toEqual([]);
});
test('back-to-back mutations across separate store instances never collide on tmp names', async () => {
// Two processes share this file (bridge + API lookup). Two store
// instances writing interleaved must never collide on the same tmp name
// (the counter is per-process, so cross-instance is the real pin).
const storeA = require('../src/billing/fulfillment-store').createFulfillmentStore({ filePath: path.join(dir, 'stripe-fulfillments.json') });
const storeB = require('../src/billing/fulfillment-store').createFulfillmentStore({ filePath: path.join(dir, 'stripe-fulfillments.json') });
for (let i = 0; i < 6; i += 1) {
const a = await storeA.claim({ eventId: `evt_a${i}`, sessionId: `cs_a${i}`, productId: 'pro-30d', durationDays: 30, email: 'a@x.com' });
expect(a.claimed).toBe(true);
const b = await storeB.claim({ eventId: `evt_b${i}`, sessionId: `cs_b${i}`, productId: 'pro-30d', durationDays: 30, email: 'b@x.com' });
expect(b.claimed).toBe(true);
}
const file = path.join(dir, 'stripe-fulfillments.json');
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
expect(Object.keys(data.byEventId)).toHaveLength(12);
const leftovers = fs.readdirSync(dir).filter((f) => f !== 'stripe-fulfillments.json');
expect(leftovers).toEqual([]);
});
});