@cosmicdrift/kumiko-bundled-features 0.320.0 → 0.322.0
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.
- package/package.json +9 -9
- package/src/agent-tools/__tests__/tool-catalog.test.ts +16 -1
- package/src/agent-tools/__tests__/tool-dispatch.integration.test.ts +130 -3
- package/src/agent-tools/agent-manifest.ts +13 -3
- package/src/agent-tools/tool-dispatch.ts +17 -12
- package/src/billing-foundation/__tests__/billing-plans.integration.test.ts +27 -0
- package/src/billing-foundation/__tests__/checkout-core.test.ts +35 -0
- package/src/billing-foundation/__tests__/sync-subscription.integration.test.ts +469 -0
- package/src/billing-foundation/changes.json +14 -0
- package/src/billing-foundation/checkout-core.ts +8 -4
- package/src/billing-foundation/constants.ts +6 -0
- package/src/billing-foundation/feature.ts +37 -2
- package/src/billing-foundation/handlers/process-event.write.ts +112 -106
- package/src/billing-foundation/handlers/switch-plan.write.ts +7 -0
- package/src/billing-foundation/handlers/sync-subscription.write.ts +164 -0
- package/src/billing-foundation/i18n.ts +9 -0
- package/src/billing-foundation/index.ts +1 -0
- package/src/billing-foundation/plan-catalog.ts +22 -5
- package/src/billing-foundation/types.ts +27 -0
- package/src/billing-foundation/web/__tests__/billing-plans-panel.test.tsx +28 -0
- package/src/billing-foundation/web/billing-plans-panel.tsx +8 -1
- package/src/channel-email/__tests__/email-channel.test.ts +92 -0
- package/src/channel-email/changes.json +9 -1
- package/src/channel-email/email-channel.ts +31 -6
- package/src/crypto-shredding/handlers/forget-subject.write.ts +3 -1
- package/src/delivery/__tests__/delivery.integration.test.ts +101 -26
- package/src/delivery/changes.json +7 -0
- package/src/delivery/feature.ts +1 -1
- package/src/delivery/handlers/unsubscribe-address.write.ts +1 -1
- package/src/delivery/handlers/unsubscribe-user.write.ts +1 -1
- package/src/delivery/index.ts +2 -1
- package/src/delivery/public-names.ts +1 -1
- package/src/delivery/unsubscribe.ts +167 -89
- package/src/step-dispatcher/__tests__/feature.boot.test.ts +9 -2
- package/src/step-dispatcher/__tests__/webhook-runner.test.ts +187 -35
- package/src/step-dispatcher/changes.json +7 -0
- package/src/step-dispatcher/feature.ts +48 -15
- package/src/step-dispatcher/index.ts +3 -1
- package/src/step-dispatcher/webhook-runner.ts +59 -19
- package/src/subscription-stripe/__tests__/plugin-methods.test.ts +117 -0
- package/src/subscription-stripe/feature.ts +5 -1
- package/src/subscription-stripe/plugin-methods.ts +53 -0
- package/src/subscription-stripe/verify-webhook.ts +52 -28
- package/src/tenant/__tests__/members-facet-i18n.test.ts +3 -2
- package/src/tenant-handover/__tests__/claim.integration.test.ts +62 -4
- package/src/tenant-handover/changes.json +7 -0
- package/src/tenant-handover/handlers/claim.write.ts +1 -0
- package/src/tenant-handover/move-entity-graph.ts +30 -44
- package/src/tenant-settings/__tests__/settings-hub-i18n.test.ts +6 -10
- package/src/user-data-rights/handlers/run-forget-cleanup.write.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-bundled-features",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.322.0",
|
|
4
4
|
"description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -134,12 +134,12 @@
|
|
|
134
134
|
"./workflow-runner": "./src/workflow-runner/index.ts"
|
|
135
135
|
},
|
|
136
136
|
"dependencies": {
|
|
137
|
-
"@cosmicdrift/kumiko-dispatcher-live": "0.
|
|
138
|
-
"@cosmicdrift/kumiko-framework": "0.
|
|
139
|
-
"@cosmicdrift/kumiko-headless": "0.
|
|
140
|
-
"@cosmicdrift/kumiko-renderer": "0.
|
|
141
|
-
"@cosmicdrift/kumiko-renderer-web": "0.
|
|
142
|
-
"@cosmicdrift/kumiko-types": "0.
|
|
137
|
+
"@cosmicdrift/kumiko-dispatcher-live": "0.322.0",
|
|
138
|
+
"@cosmicdrift/kumiko-framework": "0.322.0",
|
|
139
|
+
"@cosmicdrift/kumiko-headless": "0.322.0",
|
|
140
|
+
"@cosmicdrift/kumiko-renderer": "0.322.0",
|
|
141
|
+
"@cosmicdrift/kumiko-renderer-web": "0.322.0",
|
|
142
|
+
"@cosmicdrift/kumiko-types": "0.322.0",
|
|
143
143
|
"@mollie/api-client": "^4.5.0",
|
|
144
144
|
"@node-rs/argon2": "^2.0.2",
|
|
145
145
|
"@types/mailparser": "^3.4.6",
|
|
@@ -168,8 +168,8 @@
|
|
|
168
168
|
],
|
|
169
169
|
"devDependencies": {
|
|
170
170
|
"@testing-library/user-event": "^14.6.1",
|
|
171
|
-
"@cosmicdrift/kumiko-locale-de": "0.
|
|
172
|
-
"@cosmicdrift/kumiko-locale-es": "0.
|
|
171
|
+
"@cosmicdrift/kumiko-locale-de": "0.322.0",
|
|
172
|
+
"@cosmicdrift/kumiko-locale-es": "0.322.0",
|
|
173
173
|
"jsqr": "^1.4.0"
|
|
174
174
|
}
|
|
175
175
|
}
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
} from "@cosmicdrift/kumiko-framework/engine";
|
|
9
9
|
import * as z from "zod";
|
|
10
10
|
import { buildAgentManifest } from "../agent-manifest";
|
|
11
|
-
import { buildToolCatalog, toolNameForQn } from "../tool-catalog";
|
|
11
|
+
import { buildToolCatalog, OPEN_FORM_TOOL_NAME, toolNameForQn } from "../tool-catalog";
|
|
12
12
|
import type { AgentManifest, AgentToolMode, RegistrySearchView } from "../types";
|
|
13
13
|
|
|
14
14
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
@@ -440,6 +440,21 @@ describe("buildToolCatalog — built-in client tool names vs. a colliding handle
|
|
|
440
440
|
});
|
|
441
441
|
});
|
|
442
442
|
|
|
443
|
+
describe("buildToolCatalog — tools/dispatchTable bijection", () => {
|
|
444
|
+
test("every tool name has exactly one dispatchTable entry, and vice versa, with no duplicates", () => {
|
|
445
|
+
const catalog = buildCatalog(ADMIN);
|
|
446
|
+
const names = catalog.tools.map((t) => t.name);
|
|
447
|
+
expect(names.length).toBeGreaterThan(0);
|
|
448
|
+
expect(new Set(names).size).toBe(names.length);
|
|
449
|
+
// open_form is the one deliberate exception: dispatch-only, claimed via
|
|
450
|
+
// claimDispatchName so the approval layer can trigger it, but never
|
|
451
|
+
// listed for the model to call directly (see addClientTools).
|
|
452
|
+
const dispatchNamesForModel = new Set(catalog.dispatchTable.keys());
|
|
453
|
+
dispatchNamesForModel.delete(OPEN_FORM_TOOL_NAME);
|
|
454
|
+
expect(dispatchNamesForModel).toEqual(new Set(names));
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
443
458
|
describe("toolNameForQn", () => {
|
|
444
459
|
test("drops the verb segment, joins with underscores, sanitizes non-identifier characters", () => {
|
|
445
460
|
expect(toolNameForQn("agent-tools-test-vendor:write:vendor:approve")).toBe(
|
|
@@ -8,15 +8,21 @@
|
|
|
8
8
|
|
|
9
9
|
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
10
10
|
import { asRawClient } from "@cosmicdrift/kumiko-framework/bun-db";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
createEntity,
|
|
13
|
+
createEntityExecutor,
|
|
14
|
+
createTextField,
|
|
15
|
+
defineFeature,
|
|
16
|
+
} from "@cosmicdrift/kumiko-framework/engine";
|
|
12
17
|
import {
|
|
13
18
|
createTestUser,
|
|
14
19
|
setupTestStack,
|
|
15
20
|
type TestStack,
|
|
16
21
|
unsafeCreateEntityTable,
|
|
17
22
|
} from "@cosmicdrift/kumiko-framework/stack";
|
|
23
|
+
import * as z from "zod";
|
|
18
24
|
import { buildAgentManifest } from "../agent-manifest";
|
|
19
|
-
import { buildToolCatalog } from "../tool-catalog";
|
|
25
|
+
import { buildToolCatalog, toolNameForQn } from "../tool-catalog";
|
|
20
26
|
import { dispatchToolCall } from "../tool-dispatch";
|
|
21
27
|
|
|
22
28
|
const vendorEntity = createEntity({
|
|
@@ -47,11 +53,92 @@ const vendorFeature = defineFeature("agent-tools-test-vendor", (r) => {
|
|
|
47
53
|
|
|
48
54
|
const VENDOR_CREATE_QN = "agent-tools-test-vendor:write:vendor:create";
|
|
49
55
|
|
|
56
|
+
// No softDelete — forget-gizmo-high performs a hard, irreversible purge and
|
|
57
|
+
// therefore must resolve agent.risk "high" to pass the executor gate.
|
|
58
|
+
const gizmoEntity = createEntity({
|
|
59
|
+
table: "agent_tools_test_gizmos",
|
|
60
|
+
fields: {
|
|
61
|
+
label: createTextField({ personal: false, reason: "test_fixture", required: true }),
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
const { executor: gizmoExecutor } = createEntityExecutor("gizmo", gizmoEntity);
|
|
65
|
+
|
|
66
|
+
const GIZMO_CREATE_QN = "agent-tools-test-gizmo:write:gizmo:create";
|
|
67
|
+
const GIZMO_FORGET_HIGH_QN = "agent-tools-test-gizmo:write:forget-gizmo-high";
|
|
68
|
+
const GIZMO_DELEGATE_FORGET_MID_QN = "agent-tools-test-gizmo:write:delegate-forget-gizmo-mid";
|
|
69
|
+
|
|
70
|
+
const gizmoFeature = defineFeature("agent-tools-test-gizmo", (r) => {
|
|
71
|
+
r.entity("gizmo", gizmoEntity);
|
|
72
|
+
|
|
73
|
+
r.writeHandler(
|
|
74
|
+
"gizmo:create",
|
|
75
|
+
z.object({ label: z.string() }),
|
|
76
|
+
async (event, ctx) => gizmoExecutor.create(event.payload, event.user, ctx.db),
|
|
77
|
+
{ access: { roles: ["Admin"] } },
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
r.writeHandler(
|
|
81
|
+
"forget-gizmo-high",
|
|
82
|
+
z.object({ id: z.uuid() }),
|
|
83
|
+
async (event, ctx) => gizmoExecutor.forget({ id: event.payload.id }, event.user, ctx.db),
|
|
84
|
+
{
|
|
85
|
+
access: { roles: ["Admin"] },
|
|
86
|
+
description: "Permanently forget a gizmo (GDPR erasure).",
|
|
87
|
+
agent: { risk: "high" },
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
r.writeHandler(
|
|
92
|
+
"delegate-forget-gizmo-mid",
|
|
93
|
+
z.object({ id: z.uuid() }),
|
|
94
|
+
async (event, ctx) =>
|
|
95
|
+
ctx.write("agent-tools-test-gizmo:write:forget-gizmo-high", { id: event.payload.id }),
|
|
96
|
+
{
|
|
97
|
+
access: { roles: ["Admin"] },
|
|
98
|
+
description: "Delegates to forget-gizmo-high via ctx.write.",
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Simulates the enterprise "approve" write handler: an outer, mid-risk
|
|
103
|
+
// handler that itself runs the real dispatchToolCall path against a
|
|
104
|
+
// tool name given as input, the way an AI-agent turn approves a queued
|
|
105
|
+
// tool call.
|
|
106
|
+
r.writeHandler(
|
|
107
|
+
"approve",
|
|
108
|
+
z.object({ toolName: z.string(), id: z.string() }),
|
|
109
|
+
async (event) => {
|
|
110
|
+
const manifest = buildAgentManifest(stack.registry, {
|
|
111
|
+
locale: "en",
|
|
112
|
+
roles: event.user.roles,
|
|
113
|
+
});
|
|
114
|
+
const catalog = buildToolCatalog(stack.registry, manifest, { mode: "edit" });
|
|
115
|
+
// runId/toolCallId feed the write's idempotency key — must be unique
|
|
116
|
+
// per call, or a second approve() reuses the first call's cached
|
|
117
|
+
// WriteResult instead of actually re-dispatching the tool.
|
|
118
|
+
const result = await dispatchToolCall({
|
|
119
|
+
dispatcher: stack.dispatcher,
|
|
120
|
+
user: event.user,
|
|
121
|
+
toolName: event.payload.toolName,
|
|
122
|
+
input: { id: event.payload.id },
|
|
123
|
+
dispatchTable: catalog.dispatchTable,
|
|
124
|
+
runId: `run-approve:${event.payload.toolName}`,
|
|
125
|
+
toolCallId: `call-approve:${event.payload.id}`,
|
|
126
|
+
});
|
|
127
|
+
return {
|
|
128
|
+
isSuccess: true as const,
|
|
129
|
+
data: result.ok ? { ok: true } : { ok: false, error: result.error },
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
{ access: { roles: ["Admin"] } },
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
|
|
50
136
|
let stack: TestStack;
|
|
51
137
|
|
|
52
138
|
beforeAll(async () => {
|
|
53
|
-
stack = await setupTestStack({ features: [vendorFeature] });
|
|
139
|
+
stack = await setupTestStack({ features: [vendorFeature, gizmoFeature] });
|
|
54
140
|
await unsafeCreateEntityTable(stack.db, vendorEntity);
|
|
141
|
+
await unsafeCreateEntityTable(stack.db, gizmoEntity, "gizmo");
|
|
55
142
|
}, 20000);
|
|
56
143
|
|
|
57
144
|
afterAll(async () => {
|
|
@@ -61,6 +148,7 @@ afterAll(async () => {
|
|
|
61
148
|
beforeEach(async () => {
|
|
62
149
|
await asRawClient(stack.db).unsafe("DELETE FROM kumiko_events");
|
|
63
150
|
await asRawClient(stack.db).unsafe("DELETE FROM agent_tools_test_vendors");
|
|
151
|
+
await asRawClient(stack.db).unsafe("DELETE FROM agent_tools_test_gizmos");
|
|
64
152
|
});
|
|
65
153
|
|
|
66
154
|
const TENANT_B = "00000000-0000-4000-8000-0000000000bb";
|
|
@@ -119,3 +207,42 @@ describe("dispatchToolCall — real <entity>:list pipeline", () => {
|
|
|
119
207
|
expect(result.error).toContain("access denied");
|
|
120
208
|
});
|
|
121
209
|
});
|
|
210
|
+
|
|
211
|
+
describe("dispatchToolCall — the invoked tool is the entry handler, not the surrounding write handler", () => {
|
|
212
|
+
async function createGizmo(label: string): Promise<string> {
|
|
213
|
+
const { id } = await stack.http.writeOk<{ id: string }>(GIZMO_CREATE_QN, { label }, adminA);
|
|
214
|
+
return id;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function gizmoRowExists(id: string): Promise<boolean> {
|
|
218
|
+
const rows = (await asRawClient(stack.db).unsafe(
|
|
219
|
+
"SELECT id FROM agent_tools_test_gizmos WHERE id = $1",
|
|
220
|
+
[id],
|
|
221
|
+
)) as readonly Record<string, unknown>[];
|
|
222
|
+
return rows.length > 0;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
test("approve (mid) invoking a high-risk forget tool succeeds — the tool is the entry, not approve", async () => {
|
|
226
|
+
const id = await createGizmo("gone-after-tool-forget");
|
|
227
|
+
const { ok } = await stack.http.writeOk<{ ok: boolean }>(
|
|
228
|
+
"agent-tools-test-gizmo:write:approve",
|
|
229
|
+
{ toolName: toolNameForQn(GIZMO_FORGET_HIGH_QN), id },
|
|
230
|
+
adminA,
|
|
231
|
+
);
|
|
232
|
+
expect(ok).toBe(true);
|
|
233
|
+
expect(await gizmoRowExists(id)).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("approve (mid) invoking a mid-risk tool that delegates via ctx.write to the high forget handler is denied", async () => {
|
|
237
|
+
const id = await createGizmo("survives-delegated-forget");
|
|
238
|
+
const { ok, error } = await stack.http.writeOk<{ ok: boolean; error?: string }>(
|
|
239
|
+
"agent-tools-test-gizmo:write:approve",
|
|
240
|
+
{ toolName: toolNameForQn(GIZMO_DELEGATE_FORGET_MID_QN), id },
|
|
241
|
+
adminA,
|
|
242
|
+
);
|
|
243
|
+
expect(ok).toBe(false);
|
|
244
|
+
expect(error).toContain(GIZMO_DELEGATE_FORGET_MID_QN);
|
|
245
|
+
expect(error).toContain("irreversible");
|
|
246
|
+
expect(await gizmoRowExists(id)).toBe(true);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
NavDefinition,
|
|
7
7
|
QueryHandlerDef,
|
|
8
8
|
ScreenDefinition,
|
|
9
|
+
TranslationEntry,
|
|
9
10
|
TranslationKeys,
|
|
10
11
|
WorkspaceDefinition,
|
|
11
12
|
WriteHandlerDef,
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
isAgentVisibleScreen,
|
|
16
17
|
resolveAgentExposure,
|
|
17
18
|
} from "@cosmicdrift/kumiko-framework/engine";
|
|
19
|
+
import { translationValueOtherText } from "@cosmicdrift/kumiko-framework/ui-types";
|
|
18
20
|
import * as z from "zod";
|
|
19
21
|
import type {
|
|
20
22
|
AgentManifest,
|
|
@@ -29,6 +31,14 @@ import type {
|
|
|
29
31
|
RegistryManifestView,
|
|
30
32
|
} from "./types";
|
|
31
33
|
|
|
34
|
+
/** The manifest surfaces one string per locale — a plural entry reads as its
|
|
35
|
+
* CLDR `other` form since the manifest has no `count` to select a category. */
|
|
36
|
+
function entryAsLabels(entry: TranslationEntry): Readonly<Record<string, string>> {
|
|
37
|
+
return Object.fromEntries(
|
|
38
|
+
Object.entries(entry).map(([locale, value]) => [locale, translationValueOtherText(value)]),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
32
42
|
/** Translation keys carry a feature prefix the manifest can't reconstruct
|
|
33
43
|
* (`showcase:entity:item:field:title`), so match on the suffix instead. */
|
|
34
44
|
function labelsForSuffix(
|
|
@@ -36,7 +46,7 @@ function labelsForSuffix(
|
|
|
36
46
|
suffix: string,
|
|
37
47
|
): Readonly<Record<string, string>> {
|
|
38
48
|
for (const [key, labels] of Object.entries(translations)) {
|
|
39
|
-
if (key === suffix || key.endsWith(suffix)) return labels;
|
|
49
|
+
if (key === suffix || key.endsWith(suffix)) return entryAsLabels(labels);
|
|
40
50
|
}
|
|
41
51
|
return {};
|
|
42
52
|
}
|
|
@@ -226,7 +236,7 @@ function buildNavs(
|
|
|
226
236
|
|
|
227
237
|
result.push({
|
|
228
238
|
id: nav.id,
|
|
229
|
-
labels: translations[nav.label] ?? {},
|
|
239
|
+
labels: entryAsLabels(translations[nav.label] ?? {}),
|
|
230
240
|
...(nav.screen !== undefined && { screen: nav.screen }),
|
|
231
241
|
...(nav.parent !== undefined && { parent: nav.parent }),
|
|
232
242
|
workspaces: visibleWorkspaces,
|
|
@@ -304,7 +314,7 @@ function buildWorkspaces(
|
|
|
304
314
|
if (!uiVisible(workspace.access, roles)) continue;
|
|
305
315
|
result.push({
|
|
306
316
|
id: workspace.id,
|
|
307
|
-
labels: translations[workspace.label] ?? {},
|
|
317
|
+
labels: entryAsLabels(translations[workspace.label] ?? {}),
|
|
308
318
|
...(workspace.default === true && { default: true as const }),
|
|
309
319
|
});
|
|
310
320
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { runAsDirectCallEntry } from "@cosmicdrift/kumiko-framework/api";
|
|
1
2
|
import type { SessionUser, WriteResult } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
3
|
import type { ToolDispatchDescriptor } from "./types";
|
|
3
4
|
|
|
@@ -355,18 +356,22 @@ export async function dispatchToolCall(request: ToolCallRequest): Promise<ToolCa
|
|
|
355
356
|
}
|
|
356
357
|
|
|
357
358
|
try {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
359
|
+
// The invoked tool, not the surrounding turn/approve write handler, must
|
|
360
|
+
// be what the irreversible-operation gate sees as the entry handler.
|
|
361
|
+
return await runAsDirectCallEntry(async () => {
|
|
362
|
+
switch (descriptor.kind) {
|
|
363
|
+
case "search":
|
|
364
|
+
return await dispatchSearch(descriptor, dispatcher, user, toolName, input);
|
|
365
|
+
case "findBy":
|
|
366
|
+
return await dispatchFindBy(descriptor, dispatcher, user, toolName, input);
|
|
367
|
+
case "client":
|
|
368
|
+
return dispatchClient(descriptor, input);
|
|
369
|
+
case "server":
|
|
370
|
+
return descriptor.op === "query"
|
|
371
|
+
? await dispatchServerQuery(descriptor, dispatcher, user, input)
|
|
372
|
+
: await dispatchServerWrite(descriptor, dispatcher, user, input, runId, toolCallId);
|
|
373
|
+
}
|
|
374
|
+
});
|
|
370
375
|
} catch (err) {
|
|
371
376
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
372
377
|
}
|
|
@@ -232,6 +232,7 @@ async function createSubscription(
|
|
|
232
232
|
tier: string;
|
|
233
233
|
providerSubscriptionId: string;
|
|
234
234
|
providerCustomerId: string;
|
|
235
|
+
cancelAtIso: string | null;
|
|
235
236
|
}> = {},
|
|
236
237
|
onStack: TestStack = stack,
|
|
237
238
|
) {
|
|
@@ -247,6 +248,7 @@ async function createSubscription(
|
|
|
247
248
|
status: overrides.status ?? SubscriptionStatuses.active,
|
|
248
249
|
tier: overrides.tier ?? "starter",
|
|
249
250
|
currentPeriodEndIso: "2026-06-01T00:00:00Z",
|
|
251
|
+
...(overrides.cancelAtIso !== undefined && { cancelAtIso: overrides.cancelAtIso }),
|
|
250
252
|
rawPayload: '{"raw":"payload"}',
|
|
251
253
|
},
|
|
252
254
|
admin,
|
|
@@ -503,6 +505,31 @@ describe("switch-plan", () => {
|
|
|
503
505
|
expect(error.httpStatus).toBe(409);
|
|
504
506
|
});
|
|
505
507
|
|
|
508
|
+
test("switching a subscription that is scheduled to cancel is a conflict, and the query offers unavailable instead of switch", async () => {
|
|
509
|
+
const admin = adminFor(7034);
|
|
510
|
+
await createSubscription(admin.tenantId, {
|
|
511
|
+
tier: "starter",
|
|
512
|
+
providerSubscriptionId: "sub_switch_7034",
|
|
513
|
+
cancelAtIso: "2026-07-01T00:00:00Z",
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
const result = (await stack.http.queryOk(
|
|
517
|
+
"billing-foundation:query:billing-plans",
|
|
518
|
+
{},
|
|
519
|
+
admin,
|
|
520
|
+
)) as { plans: Array<{ tier: string; action: string }> };
|
|
521
|
+
expect(result.plans.find((p) => p.tier === "pro")?.action).toBe("unavailable");
|
|
522
|
+
|
|
523
|
+
const error = await stack.http.writeErr(
|
|
524
|
+
SubscriptionFoundationHandlers.switchPlan,
|
|
525
|
+
{ tier: "pro" },
|
|
526
|
+
admin,
|
|
527
|
+
);
|
|
528
|
+
expect(error.httpStatus).toBe(409);
|
|
529
|
+
expect(error.i18nKey).toBe("billing-foundation.errors.cancellationScheduled");
|
|
530
|
+
expect(switchCalls).toHaveLength(0);
|
|
531
|
+
});
|
|
532
|
+
|
|
506
533
|
test("the provider rejecting the switch because two tiers share one Stripe product surfaces as a 422, not a 500", async () => {
|
|
507
534
|
const admin = adminFor(7024);
|
|
508
535
|
await createSubscription(admin.tenantId, {
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { HandlerContext } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
3
|
import { UnconfiguredError, UnprocessableError } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
4
|
// Aliased — an un-aliased `Temporal` would shadow the ambient global
|
|
4
5
|
// `Temporal` TYPE `SubscriptionView.lastChangedAt` resolves against.
|
|
5
6
|
import { Temporal as TemporalPolyfill } from "temporal-polyfill";
|
|
6
7
|
import {
|
|
7
8
|
assertRedirectOrigins,
|
|
9
|
+
isBillingEnabled,
|
|
8
10
|
isNonEmptyStringArray,
|
|
9
11
|
isOwnProviderCustomer,
|
|
10
12
|
joinBaseUrl,
|
|
11
13
|
} from "../checkout-core";
|
|
12
14
|
import type { SubscriptionView } from "../get-subscription-for-tenant";
|
|
15
|
+
import type { SubscriptionProviderPlugin } from "../types";
|
|
13
16
|
|
|
14
17
|
function subscriptionView(overrides: Partial<SubscriptionView> = {}): SubscriptionView {
|
|
15
18
|
return {
|
|
@@ -151,3 +154,35 @@ describe("isOwnProviderCustomer", () => {
|
|
|
151
154
|
expect(isOwnProviderCustomer(subscriptionView(), "mock", "cus_own")).toBe(true);
|
|
152
155
|
});
|
|
153
156
|
});
|
|
157
|
+
|
|
158
|
+
function fakeCtxWithProvider(
|
|
159
|
+
entries: ReadonlyArray<{ readonly entityName: string; readonly options: unknown }>,
|
|
160
|
+
): HandlerContext {
|
|
161
|
+
return {
|
|
162
|
+
registry: {
|
|
163
|
+
getExtensionUsages: () => entries,
|
|
164
|
+
},
|
|
165
|
+
} as unknown as HandlerContext; // @cast-boundary test-fixture — only registry is exercised
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
describe("isBillingEnabled", () => {
|
|
169
|
+
test("false when no provider with that name is registered", async () => {
|
|
170
|
+
const ctx = fakeCtxWithProvider([]);
|
|
171
|
+
expect(await isBillingEnabled(ctx, "stripe")).toBe(false);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("false when the registered plugin's own isBillingEnabled reports false", async () => {
|
|
175
|
+
const plugin: SubscriptionProviderPlugin = {
|
|
176
|
+
verifyAndParseWebhook: async () => null,
|
|
177
|
+
isBillingEnabled: async () => false,
|
|
178
|
+
};
|
|
179
|
+
const ctx = fakeCtxWithProvider([{ entityName: "stripe", options: plugin }]);
|
|
180
|
+
expect(await isBillingEnabled(ctx, "stripe")).toBe(false);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("true when the registered plugin has no isBillingEnabled (defaults to enabled)", async () => {
|
|
184
|
+
const plugin: SubscriptionProviderPlugin = { verifyAndParseWebhook: async () => null };
|
|
185
|
+
const ctx = fakeCtxWithProvider([{ entityName: "stripe", options: plugin }]);
|
|
186
|
+
expect(await isBillingEnabled(ctx, "stripe")).toBe(true);
|
|
187
|
+
});
|
|
188
|
+
});
|