diff --git a/apps/accounts/backend/tests/support/harness.ts b/apps/accounts/backend/tests/support/harness.ts index 55a6937..f87768e 100644 --- a/apps/accounts/backend/tests/support/harness.ts +++ b/apps/accounts/backend/tests/support/harness.ts @@ -83,13 +83,14 @@ export type Received = { path: string; headers: http.IncomingHttpHeaders; body: export type Harness = Awaited>; -export async function startHarness(opts: { now?: () => Date } = {}) { +/** `db`: a real Prisma client instead of the in-memory one (an empty, migrated database). */ +export async function startHarness(opts: { now?: () => Date; db?: unknown } = {}) { // Accounts. const server = http.createServer(); const port = await listen(server); const publicUrl = `http://127.0.0.1:${port}`; const config = { ...loadConfig({ ACCOUNTS_URL: publicUrl, NODE_ENV: 'test', JOBS_ENABLED: 'false', LINKS_ALLOW_PRIVATE: 'true', HUB_URL: 'http://127.0.0.1:1' }) }; - const db = memoryDb(); + const db = (opts.db ?? memoryDb()) as MemoryDb; const messenger = new MemoryMessenger(); const ctx: Ctx = createCtx(config, db as never, { messenger, now: opts.now }); const provider = await createProvider(ctx); diff --git a/apps/events/backend/tests/accounts-integration/accounts.int.mts b/apps/events/backend/tests/accounts-integration/accounts.int.mts index 09a433b..e189467 100644 --- a/apps/events/backend/tests/accounts-integration/accounts.int.mts +++ b/apps/events/backend/tests/accounts-integration/accounts.int.mts @@ -23,7 +23,10 @@ const require = createRequire(import.meta.url); process.env.NODE_ENV = 'test'; process.env.JWT_SECRET = 'an-events-test-secret-long-enough-for-the-encryption-key'; -// Events' database: the in-memory stand-in, with Prisma's `{ increment }` for tokenVersion. +// With EVENTS_TEST_DATABASE_URL and ACCOUNTS_TEST_DATABASE_URL (empty, migrated databases), both +// apps use real Postgres; otherwise in-memory stand-ins (Events' with Prisma's `{ increment }`). +const PG = !!(process.env.EVENTS_TEST_DATABASE_URL && process.env.ACCOUNTS_TEST_DATABASE_URL); +if (PG) process.env.DATABASE_URL = process.env.EVENTS_TEST_DATABASE_URL; const raw = memoryDb(); const eventsDb = new Proxy(raw, { get(target, prop: string) { @@ -45,7 +48,9 @@ const eventsDb = new Proxy(raw, { }, }); const dbPath = require.resolve('../../src/config/db.js'); -require.cache[dbPath] = { id: dbPath, filename: dbPath, loaded: true, exports: eventsDb } as never; +if (!PG) require.cache[dbPath] = { id: dbPath, filename: dbPath, loaded: true, exports: eventsDb } as never; +const realEventsDb = PG ? require(dbPath) : null; +const edb = (realEventsDb ?? eventsDb) as typeof eventsDb; const { encrypt } = require('../../src/utils/encryption.js'); const { accountsRouter } = require('../../src/accounts/signin.ts'); @@ -63,7 +68,7 @@ const totpSecret = generateSecret(); const ids = { thandi: crypto.randomUUID(), sam: crypto.randomUUID(), lerato: crypto.randomUUID(), guest: crypto.randomUUID(), sipho: crypto.randomUUID() }; async function eventsUser(data: Record) { - return eventsDb.user.create({ + return edb.user.create({ data: { accountId: null, role: 'user', isActive: true, tokenVersion: 0, phoneNumber: null, failedAttempts: 0, lockUntil: null, twoFactorEnabled: false, twoFactorMethod: null, totpSecret: null, totpLastStep: null, newsEmail: false, newsWhatsApp: false, newsAskedAt: new Date(), noticeVersionAccepted: 0, lastLoginAt: new Date('2026-10-01'), @@ -73,7 +78,13 @@ async function eventsUser(data: Record) { } before(async () => { - h = await startHarness(); + if (PG) { + const { PrismaClient } = await import('../../../../accounts/backend/src/generated/prisma/client.ts'); + const { PrismaPg } = await import('@prisma/adapter-pg'); + h = await startHarness({ db: new PrismaClient({ adapter: new PrismaPg({ connectionString: process.env.ACCOUNTS_TEST_DATABASE_URL! }) }) }); + } else { + h = await startHarness(); + } setAuthClientLoader(() => import('@crosscode/auth-client')); // Events on a real port: the sign-in routes, the guard, and one page that needs signing in. @@ -91,8 +102,8 @@ before(async () => { // Connect (as Linked products would): the same secret on both sides. const secret = newLinkSecret(); await h.db.productLink.create({ data: { installId: EVENTS_ID, product: 'events', url: eventsUrl, publicKey: ed25519PublicToPem(generateEd25519().publicKey), secret: h.ctx.secrets.encrypt(secret), connectedAt: new Date() } }); - await eventsDb.productLink.create({ data: { installId: ACCOUNTS_ID, product: 'accounts', url: h.publicUrl, publicKey: 'x', secret: encrypt(secret), connectedAt: new Date(), updatedAt: new Date() } }); - await eventsDb.appSetting.create({ data: { key: 'hub_install_id', value: EVENTS_ID } }); + await edb.productLink.create({ data: { installId: ACCOUNTS_ID, product: 'accounts', url: h.publicUrl, publicKey: 'x', secret: encrypt(secret), connectedAt: new Date(), updatedAt: new Date() } }); + await edb.appSetting.create({ data: { key: 'hub_install_id', value: EVENTS_ID } }); // Events' people before the move. await eventsUser({ id: ids.thandi, name: 'Thandi Mokoena', email: 'thandi@hope.test', password: await bcrypt.hash('Old-events-pass1', 4), newsEmail: true }); @@ -150,12 +161,12 @@ describe('moving Events\' sign-in to Accounts', () => { assert.equal(report.linked.length, 1); mode.forgetMode(); assert.equal(await mode.authMode(), 'accounts'); - const thandi = await eventsDb.user.findUnique({ where: { id: ids.thandi } }) as { accountId: string }; + const thandi = await edb.user.findUnique({ where: { id: ids.thandi } }) as { accountId: string }; assert.equal(thandi.accountId, ids.thandi, 'sub = Events id'); - const lerato = await eventsDb.user.findUnique({ where: { id: ids.lerato } }) as { accountId: string }; + const lerato = await edb.user.findUnique({ where: { id: ids.lerato } }) as { accountId: string }; const account = await h.db.account.findUnique({ where: { email: 'lerato@hope.test' } }) as { id: string }; assert.equal(lerato.accountId, account.id, 'linked to the account she had'); - assert.equal((await eventsDb.user.findUnique({ where: { id: ids.guest } }) as { accountId: string | null }).accountId, null); + assert.equal((await edb.user.findUnique({ where: { id: ids.guest } }) as { accountId: string | null }).accountId, null); assert.equal((await h.db.account.findUnique({ where: { id: ids.sam } }) as { role: string }).role, 'admin'); }); }); @@ -228,7 +239,7 @@ describe('signing out, and Accounts\' calls', () => { await h.db.account.update({ where: { id: ids.thandi }, data: { name: 'Thandi M. Mokoena' } }); await profileChanged(h.ctx, ids.thandi); await runDue(h.ctx); - assert.equal((await eventsDb.user.findUnique({ where: { id: ids.thandi } }) as { name: string }).name, 'Thandi M. Mokoena'); + assert.equal((await edb.user.findUnique({ where: { id: ids.thandi } }) as { name: string }).name, 'Thandi M. Mokoena'); }); test('people added here after the move (demo data) are moved too, and can sign in', async () => { @@ -236,7 +247,7 @@ describe('signing out, and Accounts\' calls', () => { await eventsUser({ id, name: 'Demo Person', email: 'demo.person@hope.test', password: await bcrypt.hash('Demo-pass-123', 4) }); const r = await move.moveNewPeople(null); assert.equal(r.moved, 1); - assert.equal((await eventsDb.user.findUnique({ where: { id } }) as { accountId: string }).accountId, id); + assert.equal((await edb.user.findUnique({ where: { id } }) as { accountId: string }).accountId, id); const signed = await signInThroughEvents(new Browser(), 'demo.person@hope.test', 'Demo-pass-123', '/'); assert.ok('token' in signed && signed.token); }); @@ -245,7 +256,7 @@ describe('signing out, and Accounts\' calls', () => { const a = await h.makeAccount({ email: 'newcomer@hope.test', password: 'Newcomer-pass-1', name: 'Newcomer' }); const r = await signInThroughEvents(new Browser(), 'newcomer@hope.test', 'Newcomer-pass-1', '/events'); assert.ok('token' in r && r.token, JSON.stringify(r)); - assert.equal((await eventsDb.user.findUnique({ where: { id: r.user.id } }) as { accountId: string; role: string }).accountId, a.id); + assert.equal((await edb.user.findUnique({ where: { id: r.user.id } }) as { accountId: string; role: string }).accountId, a.id); }); test('Undo goes back to Events\' own sign-in', async () => { diff --git a/apps/events/e2e/excel.spec.ts b/apps/events/e2e/excel.spec.ts index a673dff..9f5d35d 100644 --- a/apps/events/e2e/excel.spec.ts +++ b/apps/events/e2e/excel.spec.ts @@ -4,7 +4,7 @@ import { test, expect, seed, API } from "./fixtures"; // Excel exports (ExcelTS since 1.51.6): a styled report and the attendance register come back as // real workbooks that open and hold what was asked for. // eslint-disable-next-line @typescript-eslint/no-require-imports -const { Workbook } = require(path.join(__dirname, "..", "backend", "node_modules", "@cj-tech-master", "excelts")); +const { Workbook } = require(require.resolve("@cj-tech-master/excelts", { paths: [path.join(__dirname, "..", "backend")] })); const XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; diff --git a/scripts/products/events.sh b/scripts/products/events.sh index c6b3f01..cb28df9 100644 --- a/scripts/products/events.sh +++ b/scripts/products/events.sh @@ -31,11 +31,12 @@ events_backup() { .catch(e => { console.error(" Backup failed: " + e.message); process.exit(1); });' } -# The shared packages this checkout's Events uses (from 2.4.0: contracts and seo), built before it. +# The shared packages this checkout's Events uses (from 2.4.0: contracts and seo; from 3.0.0: +# auth-client, built after contracts, which it uses), built before it. # Only the ones in the checkout, so going back to an older tag still installs and builds. events_shared_packages() { local p - for p in packages/contracts packages/seo; do + for p in packages/contracts packages/auth-client packages/seo; do if [ -f "$1/$p/package.json" ] && grep -q "\"@crosscode/${p#packages/}\"" "$1/apps/events/backend/package.json" "$1/apps/events/frontend/package.json" 2>/dev/null; then printf '%s\n' "$p" fi diff --git a/scripts/tests/events.bats b/scripts/tests/events.bats index 48a8674..20b592f 100644 --- a/scripts/tests/events.bats +++ b/scripts/tests/events.bats @@ -15,8 +15,9 @@ setup() { checkout() { local dir="$TMP/$1"; shift - mkdir -p "$dir/apps/events/backend" "$dir/apps/events/frontend" "$dir/packages/contracts" "$dir/packages/seo" + mkdir -p "$dir/apps/events/backend" "$dir/apps/events/frontend" "$dir/packages/contracts" "$dir/packages/seo" "$dir/packages/auth-client" echo '{"name":"@crosscode/contracts"}' > "$dir/packages/contracts/package.json" + echo '{"name":"@crosscode/auth-client"}' > "$dir/packages/auth-client/package.json" echo '{"name":"@crosscode/seo"}' > "$dir/packages/seo/package.json" printf '{"dependencies":{%s}}' "$1" > "$dir/apps/events/backend/package.json" printf '{"dependencies":{%s}}' "$2" > "$dir/apps/events/frontend/package.json" @@ -35,6 +36,17 @@ checkout() { [ "$(cut -d'|' -f2 "$CALLS" | tr '\n' ' ')" = "new/packages/contracts new/packages/seo new/apps/events/backend new/apps/events/frontend " ] } +@test "3.0.0: auth-client is installed and built after contracts, before Events" { + checkout new '"@crosscode/auth-client":"*","@crosscode/contracts":"*"' '"@crosscode/contracts":"*","@crosscode/seo":"*"' + run events_shared_packages "$TMP/new" + [ "$output" = $'packages/contracts +packages/auth-client +packages/seo' ] + events_build "$TMP/new" hope + [ "$(cut -d'|' -f2 "$CALLS" | tr ' +' ' ')" = "new/packages/contracts new/packages/auth-client new/packages/seo new/apps/events/backend new/apps/events/frontend " ] +} + @test "2.3.x (no shared packages used): installed and built as before" { checkout old '' '' run events_shared_packages "$TMP/old"