@jskit-ai/payments-core 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import knex from 'knex';
7
+ import { validatePaymentConfiguration } from '@jskit-ai/payments-core/shared';
8
+ import { createPaymentService } from '@jskit-ai/payments-core/server';
9
+ import { createKnexPaymentStore } from '@jskit-ai/payments-core/server/storage';
10
+ import migration from '../migrations/payments_core_initial.cjs';
11
+
12
+ // No editor, Genesis project, network or generated application is involved.
13
+ test('a hand-written app reloads payment policy from disk while retaining its own billing state', async () => {
14
+ const directory = await mkdtemp(join(tmpdir(), 'standalone-payments-'));
15
+ const path = join(directory, 'integrations.json');
16
+ const db = knex({ client: 'better-sqlite3', connection: { filename: join(directory, 'app.sqlite') }, useNullAsDefault: true, pool: { min: 1, max: 1 } });
17
+ const document = {
18
+ integrations: { billing: { provider: 'stripe', accountMode: 'shared', authentication: { method: 'api-key', secretRef: 'env:STRIPE_KEY' } } },
19
+ extensions: { payments: {
20
+ version: 1,
21
+ environments: { sandbox: { integrationId: 'billing', providerAccountId: 'acct_fixture', webhookSecretRef: 'env:STRIPE_WEBHOOK_SECRET', returnUrlRef: 'env:BILLING_RETURN_URL' } },
22
+ plans: { pro: { name: 'Pro', amount: 1200, currency: 'USD', interval: 'month', features: ['export'], renewalCredits: 100 } }
23
+ } }
24
+ };
25
+ const scope = { applicationId: 'hand-written-app', integrationId: 'billing', providerAccountId: 'acct_fixture', environment: 'sandbox', subjectId: 'workspace-a' };
26
+ const boot = async () => createPaymentService({
27
+ store: createKnexPaymentStore({ knex: db }),
28
+ configuration: validatePaymentConfiguration(JSON.parse(await readFile(path, 'utf8'))),
29
+ clock: () => 1000
30
+ });
31
+ try {
32
+ await migration.up(db);
33
+ await writeFile(path, JSON.stringify(document));
34
+ const first = await boot();
35
+ await createKnexPaymentStore({ knex: db }).bindCustomer(scope, 'cus_fixture', scope.subjectId);
36
+ await first.reconcileEvent(scope, {
37
+ eventId: 'evt_fixture', customerId: 'cus_fixture',
38
+ load: async () => ({ subscription: { id: 'sub_fixture', status: 'active', planId: 'pro', periodEnd: 10000 }, renewal: { id: 'invoice_fixture', planId: 'pro', periodEnd: 10000 } })
39
+ });
40
+ await first.requireFeature(scope, 'export');
41
+ await first.debitCredits(scope, { reference: 'job-1', units: 7 });
42
+ document.extensions.payments.plans.pro.features = ['reports'];
43
+ await writeFile(path, JSON.stringify(document));
44
+ const restarted = await boot();
45
+ assert.equal((await restarted.inspect(scope)).balance, 93);
46
+ await restarted.requireFeature(scope, 'reports');
47
+ await assert.rejects(restarted.requireFeature(scope, 'export'), { code: 'payment_feature_required' });
48
+ assert.equal((await restarted.debitCredits(scope, { reference: 'job-1', units: 7 })).duplicate, true);
49
+ assert.equal((await restarted.inspect(scope)).balance, 93);
50
+ assert.equal((await restarted.inspect({ ...scope, subjectId: 'workspace-b' })).balance, 0);
51
+ // A runtime instance uses an explicit snapshot, not an editor-managed watcher.
52
+ await first.requireFeature(scope, 'export');
53
+ document.extensions.payments.plans.pro.amount = -1;
54
+ await writeFile(path, JSON.stringify(document));
55
+ await assert.rejects(boot(), { code: 'payment_configuration_invalid' });
56
+ assert.equal((await restarted.inspect(scope)).balance, 93);
57
+ } finally {
58
+ await db.destroy();
59
+ await rm(directory, { recursive: true, force: true });
60
+ }
61
+ });