Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/pie-night

Pie night sign-ups: hosts start an event, guests add the pies they are bringing and watch the list fill in live. Installable, with optional push notifications.

FollowForkOpen app

Files

store.test.ts

60 lines · 3.1 KB · TypeScript
import assert from 'node:assert/strict'
import { registerHooks } from 'node:module'
import { test } from 'node:test'

// Package storage stub: a key/value map plus a real in-memory SQLite database.
const STUB = `
import { DatabaseSync } from 'node:sqlite';
const g = globalThis;
g.__pieKv ??= new Map();
g.__pieDb ??= new DatabaseSync(':memory:');
export const packageStorage = () => ({
	async get(k) { return g.__pieKv.get(k) ?? null },
	async set(k, v) { g.__pieKv.set(k, v) },
	async sql(q, b = []) { const st = g.__pieDb.prepare(q); return /^\\s*(select|pragma)/i.test(q) ? { rows: st.all(...b) } : (st.run(...b), { rows: [] }) },
});
export const packageContext = { appBasePath: '/' };
`
registerHooks({
	resolve(spec, ctx, next) {
		if (spec === 'kody:runtime') return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true }
		return next(spec, ctx)
	},
})

const store = await import('../src/store.ts')
const kv = (globalThis as unknown as { __pieKv: Map<string, unknown> }).__pieKv

test('legacy single-event data moves into a named event without touching old keys', async () => {
	assert.equal((await store.migrateLegacy({ code: 'Nope', creator: '' })).ok, false, 'needs a host')
	await store.addPies('2026-11-27', [{ who: 'The Smiths', pie: 'Apple', note: '' }, { who: 'The Smiths', pie: 'Pecan', note: '' }])
	kv.set('admin_usernames', ['cohost'])
	const result = await store.migrateLegacy({ code: 'Our Family 2026', creator: 'Host', place: 'At our house' })
	assert.equal(result.ok, true)
	assert.equal(result.slug, 'our-family-2026')
	assert.equal(result.pies, 2)
	const event = await store.getEvent('our-family-2026')
	assert.ok(event)
	assert.equal(event.night, '2026-11-27')
	assert.equal(event.creator, 'host')
	assert.deepEqual(event.hosts, ['cohost'])
	assert.equal(event.place, 'At our house')
	assert.equal(event.code, 'Our Family 2026')
	assert.equal((await store.listPies(event.night)).length, 2)
	assert.deepEqual(kv.get('admin_usernames'), ['cohost'], 'legacy keys stay for the old version')
	const again = await store.migrateLegacy({ code: 'our family 2026', creator: 'host' })
	assert.equal(again.created, false, 'idempotent')
})

test('events keep their pies apart', async () => {
	const a = await store.createEvent({ slug: 'a-night', code: 'A night', title: 'A', date: '2026-12-01', time: '18:00', place: '', blurb: '', theme: 'harvest', creator: 'x', hosts: [] })
	const b = await store.createEvent({ slug: 'b-night', code: 'B night', title: 'B', date: '2026-12-01', time: '18:00', place: '', blurb: '', theme: 'harvest', creator: 'y', hosts: [] })
	assert.ok(a && b)
	assert.equal(await store.createEvent({ slug: 'a-night', code: 'A', title: 'A', date: '2026-12-01', time: '18:00', place: '', blurb: '', theme: 'harvest', creator: 'z', hosts: [] }), null, 'codes are unique')
	await store.addPies(a.night, [{ who: 'Ann', pie: 'Cherry', note: '' }])
	assert.equal((await store.listPies(a.night)).length, 1)
	assert.equal((await store.listPies(b.night)).length, 0)
	assert.deepEqual((await store.listEventsFor('x')).map((e) => e.slug), ['a-night'])
	const next = await store.updateEvent({ ...a, title: 'A2' })
	assert.equal(next.version, a.version + 1)
})