import { nanoid } from 'nanoid' import * as Runtime from '../../../test/runtime.js' import * as Db from '../Db.js' import * as Projects from './projects.js' const create = () => Db.postgres({ connectionString: Runtime.postgresUrl, schema: `t_${nanoid()}` }) describe('activateSponsorship', () => { test('behavior: stores the first activation time once', async () => { const db = create() await db.migrate() const project = await Projects.create(db, { name: 'Privy app', orgId: 'org_1' }) const first = await Projects.activateSponsorship(db, { at: '2026-01-01T00:00:00.000Z', endsAt: '2026-04-01T00:00:00.000Z', id: project.id, orgId: 'org_1', spendLimit: '100000000', }) const second = await Projects.activateSponsorship(db, { at: '2026-02-01T00:00:00.000Z', endsAt: '2026-05-02T00:00:00.000Z', id: project.id, orgId: 'org_1', spendLimit: '200000000', }) expect(first?.sponsorshipSubsidyStartsAt).toMatchInlineSnapshot(`"2026-01-01T00:00:00.000Z"`) expect(first?.sponsorshipSubsidyEndsAt).toMatchInlineSnapshot(`"2026-04-01T00:00:00.000Z"`) expect(second?.sponsorshipSubsidyStartsAt).toBe(first?.sponsorshipSubsidyStartsAt) expect(second?.sponsorshipSubsidyEndsAt).toBe(first?.sponsorshipSubsidyEndsAt) expect(second?.sponsorshipSpendLimit).toBe(first?.sponsorshipSpendLimit) expect(second?.sponsorshipSpendLimit).toBe('100000000') await db.close() }) test('behavior: requires matching organization ownership', async () => { const db = create() await db.migrate() const project = await Projects.create(db, { name: 'Privy app', orgId: 'org_1' }) expect( await Projects.activateSponsorship(db, { at: '2026-01-01T00:00:00.000Z', endsAt: '2026-04-01T00:00:00.000Z', id: project.id, orgId: 'org_2', spendLimit: '100000000', }), ).toBeUndefined() await db.close() }) })