import { nanoid } from 'nanoid' import * as Runtime from '../../../test/runtime.js' import * as Db from '../Db.js' import * as SponsoredTransactions from './sponsoredTransactions.js' import * as SponsorshipAttributions from './sponsorshipAttributions.js' const create = () => Db.postgres({ connectionString: Runtime.postgresUrl, schema: `t_${nanoid()}` }) describe('getExternal', () => { test('behavior: reads only existing attributions in the owning organization', async () => { const db = create() await db.migrate() const attribution = await SponsorshipAttributions.resolveExternal(db, { externalId: 'partner-app', orgId: 'org_1', }) expect( await SponsorshipAttributions.getExternal(db, { externalId: 'partner-app', orgId: 'org_1' }), ).toStrictEqual(attribution) expect( await SponsorshipAttributions.getExternal(db, { externalId: 'partner-app', orgId: 'org_2' }), ).toBeUndefined() expect( await SponsorshipAttributions.getExternal(db, { externalId: 'unknown-app', orgId: 'org_1' }), ).toBeUndefined() expect(await db.kysely.selectFrom('sponsorship_attributions').selectAll().execute()).toStrictEqual([attribution]) // prettier-ignore await db.close() }) }) describe('resolveExternal', () => { test('behavior: resolves one canonical id per organization and external id', async () => { const db = create() await db.migrate() const options = { externalId: 'privy-account-1', orgId: 'org_1' } const [first, retried, otherOrganization] = await Promise.all([ SponsorshipAttributions.resolveExternal(db, options), SponsorshipAttributions.resolveExternal(db, options), SponsorshipAttributions.resolveExternal(db, { ...options, orgId: 'org_2' }), ]) expect(retried.id).toBe(first.id) expect(otherOrganization.id).not.toBe(first.id) await db.close() }) }) describe('resolveProject', () => { test('behavior: resolves Tempo Projects through the canonical attribution table', async () => { const db = create() await db.migrate() const attribution = await SponsorshipAttributions.resolveProject(db, { orgId: 'org_1', projectId: 'prj_1', }) expect(attribution).toMatchObject({ externalId: null, orgId: 'org_1', projectId: 'prj_1' }) await db.close() }) test('migration: preserves legacy project promotion state and spend attribution', async () => { const db = create() await db.migrate() await db.kysely .insertInto('projects') .values({ createdAt: '2026-01-01T00:00:00.000Z', id: 'prj_1', name: 'App', orgId: 'org_1', sponsorshipSubsidyEndsAt: '2026-04-01T00:00:00.000Z', sponsorshipSubsidyStartsAt: '2026-01-01T00:00:00.000Z', sponsorshipSpendLimit: '1000000', updatedAt: '2026-01-01T00:00:00.000Z', }) .execute() await SponsoredTransactions.upsert(db, { apiKeyId: 'key_1', billable: false, chainId: 4217, environment: 'production', feeMax: '500000', orgId: 'org_1', projectId: 'prj_1', signPayload: `0x${'11'.repeat(32)}`, transaction: `0x${'22'.repeat(32)}`, }) for (const name of [ '0156_project_sponsorship_attributions', '0157_project_sponsored_transaction_attributions', ]) { const migration = Db.migrations.find((candidate) => candidate.name === name) if (!migration) throw new Error(`Expected ${name}.`) await migration.up(db.kysely).execute() } const attribution = await SponsorshipAttributions.resolveProject(db, { orgId: 'org_1', projectId: 'prj_1', }) expect(attribution).toMatchObject({ endsAt: '2026-04-01T00:00:00.000Z', spendLimit: '1000000', startsAt: '2026-01-01T00:00:00.000Z', }) expect(await SponsoredTransactions.listPending(db)).toMatchObject([ { sponsorshipAttributionId: attribution.id }, ]) await db.close() }) }) describe('activate', () => { test('behavior: preserves the first promotion snapshot', async () => { const db = create() await db.migrate() const attribution = await SponsorshipAttributions.resolveExternal(db, { externalId: 'privy-account-1', orgId: 'org_1', }) const options = { endsAt: '2026-04-01T00:00:00.000Z', id: attribution.id, orgId: 'org_1', spendLimit: '1000000', startsAt: '2026-01-01T00:00:00.000Z', } const first = await SponsorshipAttributions.activate(db, options) const retried = await SponsorshipAttributions.activate(db, { ...options, endsAt: '2027-04-01T00:00:00.000Z', spendLimit: '2000000', startsAt: '2027-01-01T00:00:00.000Z', }) expect(retried).toStrictEqual(first) await db.close() }) }) describe('get', () => { test('behavior: does not cross organization boundaries', async () => { const db = create() await db.migrate() const attribution = await SponsorshipAttributions.resolveExternal(db, { externalId: 'privy-account-1', orgId: 'org_1', }) expect( await SponsorshipAttributions.get(db, { id: attribution.id, orgId: 'org_2' }), ).toBeUndefined() await db.close() }) })