/** * SCONST-01..10 — `superadmin.const` (catalogue §8.2). * * Global constants live in `global_consts`, legacy leftovers in `key_val_store`. * Both routers accept `{ constants: [{ key, value }] }` with `value: any`, and * neither *deletes* on a null value — `upsert*` writes the null through, so the * key stays with a null value (SCONST-03 is pinned `(current)`). Object values * are accepted and stringified to `"[object Object]"` (SCONST-10). * * Local-dev limitation: the `key_val_store` table was migrated without a PRIMARY * KEY/unique constraint, so `updateLegacyConstants` fails its upsert * (`ON CONFLICT` matches nothing) with a 500 — SCONST-06 is pinned `(current)`. * The read (`getLegacyConstants`) and the empty-array short-circuit still work. * * `updateGlobalConstants` rebuilds the KV mirror for *every* bound, which is what * SCONST-02/09 observe through the public `common.essentialConsts`. */ import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api' import { anonClient, staffSession, superadminClient, userSession } from '../helpers/sessions' import { getEssentialConsts } from '../helpers/cache' import { TEST_BOUND_ID, TEST_OTHER_BOUND_ID } from '../helpers/env' jest.setTimeout(180_000) const asRows = (value: unknown): Array<{ key: string; value: string | null }> => Array.isArray(value) ? (value as Array<{ key: string; value: string | null }>) : [] const toMap = (rows: Array<{ key: string; value: string | null }>): Record => Object.fromEntries(rows.map((row) => [row.key, row.value])) describe('superadmin.const.getGlobalConstants (SCONST-01)', () => { it('SCONST-01: getGlobalConstants returns a key/value list', async () => { const superadmin = await superadminClient() const rows = asRows(await superadmin.superadmin.const.getGlobalConstants.query()) expect(rows.length).toBeGreaterThan(0) for (const row of rows) { expect(row).toEqual(expect.objectContaining({ key: expect.any(String) })) expect(Object.prototype.hasOwnProperty.call(row, 'value')).toBe(true) } const map = toMap(rows) expect(map).toEqual( expect.objectContaining({ versionNum: expect.any(String), playStoreUrl: expect.any(String), appStoreUrl: expect.any(String), }) ) }) }) describe('superadmin.const.updateGlobalConstants (SCONST-02..04, 09, 10)', () => { it('SCONST-02: update returns counts/keys and refreshes the KV mirror', async () => { const superadmin = await superadminClient() const before = toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query())) const original = before.versionNum ?? null const next = `9.9.9-${uniqueSuffix()}` try { const result: any = await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key: 'versionNum', value: next }], }) expect(result.success).toBe(true) expect(result.updatedCount).toBe(1) expect(result.keys).toEqual(['versionNum']) // updateGlobalConstants awaits refreshGenKv(), so the public read already // serves the new value from KV. const consts = await getEssentialConsts(TEST_BOUND_ID) expect(consts.versionNum).toBe(next) } finally { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key: 'versionNum', value: original }], }) } expect(toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query())).versionNum).toBe( original ) }) it('(current) SCONST-03: a null value does not delete — the key remains with a null value', async () => { const superadmin = await superadminClient() const key = `__e2e_sconst_null_${uniqueSuffix()}__` try { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: 'present' }], }) expect(toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query()))[key]).toBe( 'present' ) const deleted: any = await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: null }], }) // upsertGlobalConsts writes the null through; it never runs a DELETE. expect(deleted.success).toBe(true) expect(deleted.updatedCount).toBe(1) const map = toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query())) expect(Object.prototype.hasOwnProperty.call(map, key)).toBe(true) expect(map[key]).toBeNull() } finally { // Leave the throwaway key at its null state (there is no delete path). await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: null }], }) } }) it('SCONST-04: an unknown key is inserted', async () => { const superadmin = await superadminClient() const key = `__e2e_sconst_unknown_${uniqueSuffix()}__` try { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: 'fresh' }], }) expect(toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query()))[key]).toBe( 'fresh' ) } finally { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: null }], }) } }) it('SCONST-09: a global update refreshes every bound’s essentialConsts', async () => { const superadmin = await superadminClient() const original = toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query())).versionNum ?? null const next = `8.8.8-${uniqueSuffix()}` try { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key: 'versionNum', value: next }], }) // updateGlobalConstants rebuilds every bound, not just one. expect((await getEssentialConsts(TEST_BOUND_ID)).versionNum).toBe(next) expect((await getEssentialConsts(TEST_OTHER_BOUND_ID)).versionNum).toBe(next) } finally { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key: 'versionNum', value: original }], }) } expect((await getEssentialConsts(TEST_BOUND_ID)).versionNum).toBe(original) }) it('(current) SCONST-10: an object value is accepted and stringified, not rejected', async () => { const superadmin = await superadminClient() const key = `__e2e_sconst_obj_${uniqueSuffix()}__` try { // The input schema types `value` as `z.any()`, so an object passes // validation; `toRows` then does `String(value)` -> "[object Object]". const result: any = await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: { nested: true } }], }) expect(result.success).toBe(true) expect(toMap(asRows(await superadmin.superadmin.const.getGlobalConstants.query()))[key]).toBe( '[object Object]' ) // The one shape that *is* rejected is a malformed `constants` payload. await expectTrpcError( () => superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: 'not-an-array', } as any), 'BAD_REQUEST' ) } finally { await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [{ key, value: null }], }) } }) }) describe('superadmin.const legacy constants (SCONST-05..07)', () => { it('SCONST-05: getLegacyConstants returns a key/value list', async () => { const superadmin = await superadminClient() const rows = asRows(await superadmin.superadmin.const.getLegacyConstants.query()) expect(rows.length).toBeGreaterThan(0) for (const row of rows) { expect(row).toEqual(expect.objectContaining({ key: expect.any(String) })) expect(Object.prototype.hasOwnProperty.call(row, 'value')).toBe(true) } expect(toMap(rows)).toEqual( expect.objectContaining({ tester: expect.anything() }) ) }) it('(current) SCONST-06: updateLegacyConstants is a 500 in this local DB', async () => { // The declared schema gives `key_val_store.key` a PRIMARY KEY, but the local // D1 migration created the table without one, so drizzle's // `ON CONFLICT ("key_val_store"."key")` matches no constraint and D1 rejects // the statement. The catalogue's "counts/keys" is therefore unreachable here. const superadmin = await superadminClient() await expectTrpcError( () => superadmin.superadmin.const.updateLegacyConstants.mutate({ constants: [{ key: 'tester', value: `value-${uniqueSuffix()}` }], }), 'INTERNAL_SERVER_ERROR' ) // A brand-new key fails identically (it is the upsert, not the existing row). await expectTrpcError( () => superadmin.superadmin.const.updateLegacyConstants.mutate({ constants: [{ key: `__e2e_legacy_${uniqueSuffix()}__`, value: 'x' }], }), 'INTERNAL_SERVER_ERROR' ) // The read path still returns the seeded legacy rows untouched. const rows = asRows(await superadmin.superadmin.const.getLegacyConstants.query()) expect(toMap(rows)).toEqual(expect.objectContaining({ tester: expect.anything() })) }) it('SCONST-07: an empty array succeeds with 0 updated', async () => { const superadmin = await superadminClient() const result: any = await superadmin.superadmin.const.updateLegacyConstants.mutate({ constants: [], }) expect(result).toEqual({ success: true, updatedCount: 0, keys: [] }) const globalResult: any = await superadmin.superadmin.const.updateGlobalConstants.mutate({ constants: [], }) expect(globalResult).toEqual({ success: true, updatedCount: 0, keys: [] }) }) }) describe('superadmin.const auth (SCONST-08)', () => { it('SCONST-08: anonymous, staff and user tokens are 401', async () => { const staff = await staffSession() const user = await userSession() for (const client of [anonClient(), apiClient(staff.token), apiClient(user.token)]) { for (const call of [ () => client.superadmin.const.getGlobalConstants.query(), () => client.superadmin.const.getLegacyConstants.query(), () => client.superadmin.const.updateGlobalConstants.mutate({ constants: [] }), () => client.superadmin.const.updateLegacyConstants.mutate({ constants: [] }), ]) { await expectTrpcError(call, 'UNAUTHORIZED') } } }) })