create-ailk 0.15.0 → 0.17.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/README.md +124 -0
- package/dist/add-module.js +52 -7
- package/dist/add-route.js +8 -0
- package/dist/adopt.d.ts +74 -0
- package/dist/adopt.js +314 -0
- package/dist/cli.js +82 -2
- package/dist/derive-architecture.d.ts +22 -0
- package/dist/derive-architecture.js +58 -1
- package/dist/lib/declared-env.d.ts +142 -0
- package/dist/lib/declared-env.js +985 -0
- package/dist/lib/env-catalog.d.ts +17 -0
- package/dist/lib/env-catalog.js +234 -0
- package/dist/lib/extract-module-bundle.d.ts +9 -0
- package/dist/lib/extract-module-bundle.js +18 -1
- package/dist/lib/module-tier.d.ts +2 -0
- package/dist/lib/module-tier.js +4 -0
- package/dist/module-architecture.js +76 -0
- package/dist/parse-args.d.ts +5 -0
- package/dist/parse-args.js +17 -1
- package/dist/programmatic.d.ts +8 -0
- package/dist/programmatic.js +78 -9
- package/dist/seed-flows.d.ts +65 -0
- package/dist/seed-flows.js +350 -0
- package/dist/site-manifest.d.ts +43 -0
- package/dist/site-manifest.js +98 -3
- package/dist/site-roster.d.ts +13 -0
- package/dist/site-roster.js +22 -8
- package/dist/spoke-checkout.d.ts +67 -0
- package/dist/spoke-checkout.js +562 -0
- package/dist/surfaces.js +7 -0
- package/dist/sync-routes.js +23 -8
- package/dist/topology.d.ts +124 -9
- package/dist/topology.js +451 -40
- package/package.json +3 -3
- package/templates/.env.example +10 -0
- package/templates/apps/api/CLAUDE.md +19 -4
- package/templates/apps/api/src/__tests__/factories/in-memory-prisma.ts +95 -16
- package/templates/apps/api/src/__tests__/openapi-keyless-hub.test.ts +122 -0
- package/templates/apps/api/src/__tests__/support/hub-sign-in-fake-db.ts +362 -0
- package/templates/apps/api/src/bin/spokes.ts +62 -1
- package/templates/apps/api/src/config/__tests__/hub-require-spoke-key.test.ts +75 -0
- package/templates/apps/api/src/config/index.ts +35 -0
- package/templates/apps/api/src/lib/__mocks__/prisma.ts +89 -0
- package/templates/apps/api/src/lib/__tests__/deliverable-asset-store.test.ts +66 -5
- package/templates/apps/api/src/lib/__tests__/hub-sign-in-store.test.ts +209 -0
- package/templates/apps/api/src/lib/__tests__/spoke-store.test.ts +20 -0
- package/templates/apps/api/src/lib/deliverable-asset-store.ts +24 -5
- package/templates/apps/api/src/lib/hub-sign-in-store.ts +419 -0
- package/templates/apps/api/src/lib/spoke-store.ts +25 -3
- package/templates/apps/api/src/middleware/__tests__/auth.hub-session-path.test.ts +310 -0
- package/templates/apps/api/src/middleware/auth.ts +126 -0
- package/templates/apps/api/src/routes/auth/__tests__/hub-sign-in.test.ts +483 -0
- package/templates/apps/api/src/routes/auth/hub-sign-in.ts +201 -0
- package/templates/apps/api/src/routes/leads/__tests__/index.test.ts +7 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-flow-checkout.test.ts +105 -0
- package/templates/apps/api/src/routes/webhooks/stripe.ts +72 -1
- package/templates/apps/api/src/server.ts +105 -0
- package/templates/apps/api/src/services/__tests__/deliverable-fulfillment.test.ts +4 -4
- package/templates/apps/api/src/services/__tests__/hub-sign-in.test.ts +854 -0
- package/templates/apps/api/src/services/__tests__/lead-routing.test.ts +293 -0
- package/templates/apps/api/src/services/__tests__/person.test.ts +553 -0
- package/templates/apps/api/src/services/hub-sign-in.ts +471 -0
- package/templates/apps/api/src/services/lead-routing.ts +131 -2
- package/templates/apps/api/src/services/person.ts +541 -0
- package/templates/apps/web/.env.example +58 -5
- package/templates/apps/web/app/[locale]/(auth)/sign-in/next-path.ts +0 -0
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/hub-mode.test.tsx +206 -0
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/page.test.tsx +256 -3
- package/templates/apps/web/app/[locale]/(authed)/account/account-data.ts +5 -8
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/message/[id]/file/__tests__/route.test.ts +205 -0
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/message/[id]/file/route.ts +82 -0
- package/templates/apps/web/app/[locale]/(authed)/account/settings/page.tsx +98 -29
- package/templates/apps/web/app/[locale]/(authed)/account/support-actions.ts +6 -3
- package/templates/apps/web/app/[locale]/(authed)/layout.tsx +19 -101
- package/templates/apps/web/app/[locale]/(authed)/signed-out-gate.generated.tsx +90 -0
- package/templates/apps/web/app/[locale]/layout.tsx +28 -15
- package/templates/apps/web/app/api/auth/hub/authorize/__tests__/route.test.ts +295 -0
- package/templates/apps/web/app/api/auth/hub/authorize/route.ts +156 -0
- package/templates/apps/web/app/api/hub-session/__tests__/routes.test.ts +341 -0
- package/templates/apps/web/app/api/hub-session/callback/route.ts +168 -0
- package/templates/apps/web/app/api/hub-session/sign-in/route.ts +76 -0
- package/templates/apps/web/app/api/hub-session/sign-out/route.ts +63 -0
- package/templates/apps/web/app/openapi.json/__tests__/route.test.ts +57 -24
- package/templates/apps/web/app/openapi.json/route.ts +3 -6
- package/templates/apps/web/auth.ts +29 -2
- package/templates/apps/web/components/sign-out-trigger.tsx +20 -1
- package/templates/apps/web/components/signed-in-nav.tsx +17 -3
- package/templates/apps/web/lib/__tests__/account-config.test.ts +5 -2
- package/templates/apps/web/lib/__tests__/listings-config.test.ts +83 -0
- package/templates/apps/web/lib/__tests__/listings.test.tsx +49 -0
- package/templates/apps/web/lib/__tests__/meeting-slugs.test.ts +49 -1
- package/templates/apps/web/lib/__tests__/relay.test.ts +33 -0
- package/templates/apps/web/lib/__tests__/server-api.test.ts +37 -28
- package/templates/apps/web/lib/account-config.ts +130 -1
- package/templates/apps/web/lib/api-origin.generated.ts +11 -0
- package/templates/apps/web/lib/hub-session/__tests__/hub-session.test.ts +262 -0
- package/templates/apps/web/lib/hub-session/config.ts +104 -0
- package/templates/apps/web/lib/hub-session/index.ts +23 -0
- package/templates/apps/web/lib/hub-session/seal.ts +82 -0
- package/templates/apps/web/lib/hub-session/session.ts +180 -0
- package/templates/apps/web/lib/hub-session/sign-in-gate.tsx +79 -0
- package/templates/apps/web/lib/listings-config.ts +31 -0
- package/templates/apps/web/lib/listings.tsx +7 -3
- package/templates/apps/web/lib/meeting-slugs.ts +11 -1
- package/templates/apps/web/lib/next-path.ts +35 -0
- package/templates/apps/web/lib/relay.ts +3 -1
- package/templates/apps/web/lib/server-api.ts +11 -1
- package/templates/apps/web/lib/topology.generated.ts +15 -5
- package/templates/apps/web/messages/en.json +32 -3
- package/templates/database/CHANGELOG.md +26 -0
- package/templates/database/__tests__/agents.db.test.ts +25 -5
- package/templates/database/__tests__/fixtures/person-backfill.json +264 -0
- package/templates/database/__tests__/member-area.db.test.ts +245 -0
- package/templates/database/__tests__/person.db.test.ts +584 -0
- package/templates/database/auth/schema.prisma +3 -2
- package/templates/database/config/schema.prisma +13 -0
- package/templates/database/inbox/schema.prisma +98 -0
- package/templates/database/migrations/20261010120000_person/migration.sql +106 -0
- package/templates/database/migrations/20261010121000_person_backfill/migration.sql +132 -0
- package/templates/database/migrations/20261010130000_agent_grants_people_read/migration.sql +20 -0
- package/templates/database/migrations/20261011120000_member_area/migration.sql +158 -0
- package/templates/database/migrations/20261011130000_agent_grants_member_pieces_manage/migration.sql +21 -0
- package/templates/database/package.json +1 -1
- package/templates/database/scripts/person-backfill.sql +132 -0
- package/templates/database/tenancy.ts +53 -0
- package/templates/tools/check-declared-env.ts +37 -0
- package/templates/tools/declared-env.ts +1163 -0
- package/versions.json +35 -35
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file person.test.ts
|
|
3
|
+
* @description `findOrCreatePerson`, `stampPersonUser` and
|
|
4
|
+
* `recordCheckoutPerson` (#852, spec
|
|
5
|
+
* docs/specs/852-person-the-per-workspace-client-spec.md D3, D5, D6;
|
|
6
|
+
* AC-2, AC-4's API half, AC-5), and the member area's two additions,
|
|
7
|
+
* `resolveMemberPersonId` and `setPersonPlanOffering` (#7877 D8; AC-3, AC-5),
|
|
8
|
+
* and the hub's buyer read, `resolveCallerBillingCustomerId`, with the
|
|
9
|
+
* stamped-Person guard on the customer-id write (#7961, spec
|
|
10
|
+
* docs/specs/7894-the-hub-signs-in-its-spec.md D13, SEC-7894-6; AC-8).
|
|
11
|
+
*
|
|
12
|
+
* Runs over `createInMemoryPrisma()`, whose `person` delegate enforces the
|
|
13
|
+
* declared `(workspaceId, email)` and `(workspaceId, userId)` keys the way
|
|
14
|
+
* Postgres does: `createMany({ skipDuplicates })` skips a conflict, and an
|
|
15
|
+
* `updateMany` that would put one user on two Persons of a workspace throws
|
|
16
|
+
* `P2002`. The database half of each claim — the CHECKs, the real ON
|
|
17
|
+
* CONFLICT race, the backfill SQL — is `database/__tests__/person.db.test.ts`.
|
|
18
|
+
*
|
|
19
|
+
* External dependency boundary mocked:
|
|
20
|
+
* - apps/api/src/lib/prisma — the in-memory, where-clause-honouring double.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// eslint-disable-next-line no-restricted-syntax -- lib/prisma.ts wraps the external DB boundary (PrismaClient requires a live connection); the in-memory double is the repo-standard where-honouring test double for it.
|
|
24
|
+
jest.mock("../../lib/prisma.js", () =>
|
|
25
|
+
jest
|
|
26
|
+
.requireActual<typeof import("../../__tests__/factories/in-memory-prisma")>(
|
|
27
|
+
"../../__tests__/factories/in-memory-prisma",
|
|
28
|
+
)
|
|
29
|
+
.createInMemoryPrisma(),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
import { readFileSync } from "node:fs";
|
|
33
|
+
import { join } from "node:path";
|
|
34
|
+
|
|
35
|
+
import { prisma } from "../../lib/prisma.js";
|
|
36
|
+
import {
|
|
37
|
+
findOrCreatePerson,
|
|
38
|
+
isStorablePersonEmail,
|
|
39
|
+
normalizePersonEmail,
|
|
40
|
+
recordCheckoutPerson,
|
|
41
|
+
resolveCallerBillingCustomerId,
|
|
42
|
+
resolveMemberPersonId,
|
|
43
|
+
setPersonPlanOffering,
|
|
44
|
+
stampPersonUser,
|
|
45
|
+
} from "../person";
|
|
46
|
+
|
|
47
|
+
type Row = Record<string, unknown>;
|
|
48
|
+
// The in-memory double's own surface (`__db`, `__calls`) is not on the
|
|
49
|
+
// PrismaClient type the import carries, so it is named here once.
|
|
50
|
+
// eslint-disable-next-line no-restricted-syntax -- justified above
|
|
51
|
+
const store = prisma as unknown as {
|
|
52
|
+
__db: Record<string, Row[]>;
|
|
53
|
+
__calls: Array<{ model: string; op: string }>;
|
|
54
|
+
person: Record<string, jest.Mock>;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const WS_A1 = "wksp_00000000-0000-7000-8000-0000000000a1";
|
|
58
|
+
const WS_A2 = "wksp_00000000-0000-7000-8000-0000000000a2";
|
|
59
|
+
|
|
60
|
+
/** A 255-character address — one past the 254 bound. */
|
|
61
|
+
const LONG_255 = `${"a".repeat(244)}@example.io`;
|
|
62
|
+
|
|
63
|
+
const people = () => (store.__db["person"] ??= []);
|
|
64
|
+
const personCalls = () => store.__calls.filter((c) => c.model === "person");
|
|
65
|
+
|
|
66
|
+
function reset(): void {
|
|
67
|
+
for (const key of Object.keys(store.__db)) delete store.__db[key];
|
|
68
|
+
store.__calls.length = 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function seedPerson(row: Row): Row {
|
|
72
|
+
const full: Row = {
|
|
73
|
+
id: `person_00000000-0000-7000-8000-${String(people().length + 900).padStart(12, "0")}`,
|
|
74
|
+
workspaceId: WS_A1,
|
|
75
|
+
name: null,
|
|
76
|
+
userId: null,
|
|
77
|
+
billingProvider: null,
|
|
78
|
+
billingCustomerId: null,
|
|
79
|
+
planOffering: null,
|
|
80
|
+
createdAt: new Date(Date.UTC(2026, 0, 1 + people().length)),
|
|
81
|
+
updatedAt: new Date(),
|
|
82
|
+
...row,
|
|
83
|
+
};
|
|
84
|
+
people().push(full);
|
|
85
|
+
return full;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
beforeEach(() => {
|
|
89
|
+
reset();
|
|
90
|
+
jest.restoreAllMocks();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("findOrCreatePerson (#852 D3, AC-2)", () => {
|
|
94
|
+
it("normalizes the email: a padded, mixed-case address and its plain form are one Person per workspace", async () => {
|
|
95
|
+
const first = await findOrCreatePerson(WS_A1, { email: " Ada@Example.COM\t" });
|
|
96
|
+
const second = await findOrCreatePerson(WS_A1, { email: "ada@example.com" });
|
|
97
|
+
expect(first).toEqual({ ok: true, value: expect.objectContaining({ created: true }) });
|
|
98
|
+
expect(second.ok && second.value).toEqual(
|
|
99
|
+
expect.objectContaining({ created: false, personId: first.ok && first.value.personId }),
|
|
100
|
+
);
|
|
101
|
+
expect(people()).toHaveLength(1);
|
|
102
|
+
expect(people()[0]!["email"]).toBe("ada@example.com");
|
|
103
|
+
|
|
104
|
+
const other = await findOrCreatePerson(WS_A2, { email: "ada@example.com" });
|
|
105
|
+
expect(other.ok && other.value.created).toBe(true);
|
|
106
|
+
expect(people()).toHaveLength(2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("a customer-id match returns that Person, creates nothing and never rewrites its email", async () => {
|
|
110
|
+
const p = seedPerson({ email: "a@x.io", billingProvider: "stripe", billingCustomerId: "cus_1" });
|
|
111
|
+
const result = await findOrCreatePerson(WS_A1, { email: "b@x.io", billingCustomerId: "cus_1" });
|
|
112
|
+
expect(result).toEqual({
|
|
113
|
+
ok: true,
|
|
114
|
+
value: { personId: p["id"], created: false, billing: "unchanged" },
|
|
115
|
+
});
|
|
116
|
+
expect(people()).toHaveLength(1);
|
|
117
|
+
expect(people()[0]!["email"]).toBe("a@x.io");
|
|
118
|
+
expect(personCalls().map((c) => c.op)).toEqual(["findFirst"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("an email match writes the customer id only when the Person has none; the first writer is kept", async () => {
|
|
122
|
+
seedPerson({ email: "c@x.io" });
|
|
123
|
+
const written = await findOrCreatePerson(WS_A1, { email: "c@x.io", billingCustomerId: "cus_2" });
|
|
124
|
+
expect(written.ok && written.value.billing).toBe("written");
|
|
125
|
+
const kept = await findOrCreatePerson(WS_A1, { email: "c@x.io", billingCustomerId: "cus_3" });
|
|
126
|
+
expect(kept.ok && kept.value.billing).toBe("kept");
|
|
127
|
+
expect(people()[0]).toEqual(
|
|
128
|
+
expect.objectContaining({ billingProvider: "stripe", billingCustomerId: "cus_2" }),
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("an email match never writes a customer id onto a Person already linked to a user (SEC-7894-6)", async () => {
|
|
133
|
+
seedPerson({ email: "d@x.io", userId: "user_00000000-0000-7000-8000-000000000021" });
|
|
134
|
+
const result = await findOrCreatePerson(WS_A1, { email: "D@x.io", billingCustomerId: "cus_payer" });
|
|
135
|
+
expect(result.ok && result.value).toEqual(expect.objectContaining({ created: false, billing: "kept" }));
|
|
136
|
+
expect(people()[0]).toEqual(
|
|
137
|
+
expect.objectContaining({ billingProvider: null, billingCustomerId: null }),
|
|
138
|
+
);
|
|
139
|
+
expect(personCalls().filter((c) => c.op === "updateMany")).toHaveLength(0);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("a Person linked between the match and the write keeps no customer id (the write is guarded on userId)", async () => {
|
|
143
|
+
seedPerson({ email: "e@x.io", userId: "user_00000000-0000-7000-8000-000000000022" });
|
|
144
|
+
// Step 2's customer-id read finds nothing; the email match read saw the
|
|
145
|
+
// Person before the stamp landed.
|
|
146
|
+
store.person["findFirst"]!.mockResolvedValueOnce(null).mockResolvedValueOnce({
|
|
147
|
+
id: people()[0]!["id"],
|
|
148
|
+
name: null,
|
|
149
|
+
userId: null,
|
|
150
|
+
billingCustomerId: null,
|
|
151
|
+
});
|
|
152
|
+
const result = await findOrCreatePerson(WS_A1, { email: "e@x.io", billingCustomerId: "cus_payer" });
|
|
153
|
+
expect(result.ok && result.value.billing).toBe("kept");
|
|
154
|
+
expect(people()[0]!["billingCustomerId"]).toBeNull();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("a new Person given a customer id is created with it", async () => {
|
|
158
|
+
const result = await findOrCreatePerson(WS_A1, { email: "new@x.io", billingCustomerId: "cus_9" });
|
|
159
|
+
expect(result.ok && result.value).toEqual(expect.objectContaining({ created: true, billing: "written" }));
|
|
160
|
+
expect(people()[0]).toEqual(
|
|
161
|
+
expect.objectContaining({ billingProvider: "stripe", billingCustomerId: "cus_9" }),
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("an email match gains a name only when it has none", async () => {
|
|
166
|
+
seedPerson({ email: "named@x.io", name: "First" });
|
|
167
|
+
seedPerson({ email: "nameless@x.io" });
|
|
168
|
+
await findOrCreatePerson(WS_A1, { email: "named@x.io", name: "Second" });
|
|
169
|
+
await findOrCreatePerson(WS_A1, { email: "nameless@x.io", name: "Given" });
|
|
170
|
+
expect(people().map((p) => p["name"])).toEqual(["First", "Given"]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it.each([
|
|
174
|
+
["not-an-email"],
|
|
175
|
+
[""],
|
|
176
|
+
["a@b@c.io"],
|
|
177
|
+
["zoë@x.io"],
|
|
178
|
+
["zero\u200Bwidth@x.io"],
|
|
179
|
+
[LONG_255],
|
|
180
|
+
])("refuses %p as invalid_email with zero person delegate calls", async (email) => {
|
|
181
|
+
const result = await findOrCreatePerson(WS_A1, { email });
|
|
182
|
+
expect(result).toEqual({ ok: false, error: { kind: "invalid_email" } });
|
|
183
|
+
expect(personCalls()).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("the long case is exactly 255 characters", () => {
|
|
187
|
+
expect(LONG_255).toHaveLength(255);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("strips control and bidi characters from a name, trims it and cuts it to 200 code points", async () => {
|
|
191
|
+
await findOrCreatePerson(WS_A1, {
|
|
192
|
+
email: "n@x.io",
|
|
193
|
+
name: ` Ad\u202Ea\u0007 ${"😀".repeat(250)}`,
|
|
194
|
+
});
|
|
195
|
+
const name = people()[0]!["name"] as string;
|
|
196
|
+
expect(name.startsWith("Ada ")).toBe(true);
|
|
197
|
+
const codePoints = Array.from(name, (c) => c.codePointAt(0)!);
|
|
198
|
+
expect(codePoints.filter((cp) => cp < 0x20 || cp === 0x202e)).toEqual([]);
|
|
199
|
+
expect(Array.from(name)).toHaveLength(200);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("an empty-after-cleaning name is stored as null", async () => {
|
|
203
|
+
await findOrCreatePerson(WS_A1, { email: "blank@x.io", name: " \u200E\u0001 " });
|
|
204
|
+
expect(people()[0]!["name"]).toBeNull();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("a createMany that loses a concurrent race (count 0) answers the re-read's Person with created: false", async () => {
|
|
208
|
+
store.person["createMany"]!.mockImplementationOnce(async () => {
|
|
209
|
+
seedPerson({ email: "race@x.io" });
|
|
210
|
+
return { count: 0 };
|
|
211
|
+
});
|
|
212
|
+
const result = await findOrCreatePerson(WS_A1, { email: "race@x.io" });
|
|
213
|
+
expect(result).toEqual({
|
|
214
|
+
ok: true,
|
|
215
|
+
value: { personId: people()[0]!["id"], created: false, billing: "unchanged" },
|
|
216
|
+
});
|
|
217
|
+
expect(people()).toHaveLength(1);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("uses createMany with skipDuplicates (ON CONFLICT DO NOTHING), never a caught P2002", async () => {
|
|
221
|
+
await findOrCreatePerson(WS_A1, { email: "skip@x.io" });
|
|
222
|
+
expect(store.person["createMany"]).toHaveBeenCalledWith(
|
|
223
|
+
expect.objectContaining({ skipDuplicates: true }),
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("a delegate that throws answers internal_error with no message", async () => {
|
|
228
|
+
store.person["findFirst"]!.mockRejectedValueOnce(new Error("relation people does not exist: secret detail"));
|
|
229
|
+
const result = await findOrCreatePerson(WS_A1, { email: "boom@x.io" });
|
|
230
|
+
expect(result).toEqual({ ok: false, error: { kind: "internal_error" } });
|
|
231
|
+
expect(JSON.stringify(result)).not.toContain("secret");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("runs on the client it is handed (the appointment path's transaction)", async () => {
|
|
235
|
+
const tx = { person: { ...store.person } };
|
|
236
|
+
const spy = jest.fn(store.person["findFirst"]!);
|
|
237
|
+
tx.person["findFirst"] = spy;
|
|
238
|
+
await findOrCreatePerson(WS_A1, { email: "tx@x.io" }, tx as never);
|
|
239
|
+
expect(spy).toHaveBeenCalled();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("never reads a User: a Person is created with no verified email at all", async () => {
|
|
243
|
+
await findOrCreatePerson(WS_A1, { email: "nouser@x.io" });
|
|
244
|
+
expect(store.__calls.filter((c) => c.model === "user")).toEqual([]);
|
|
245
|
+
expect(people()[0]!["userId"] ?? null).toBeNull();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("imports none of the paid closure or tenant-db", () => {
|
|
249
|
+
const source = readFileSync(join(__dirname, "..", "person.ts"), "utf8");
|
|
250
|
+
expect(source).not.toMatch(
|
|
251
|
+
/from ['"](\.\/lead-promotion|\.\/owner-workspaces|\.\/lead-accept-user|\.\.\/lib\/tenant-db)/,
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe("the backfill fixture, API half (#852 AC-4)", () => {
|
|
257
|
+
type Fixture = {
|
|
258
|
+
workspaces: Record<string, string>;
|
|
259
|
+
input: {
|
|
260
|
+
leads: Array<{ key: string; workspace: string; email: string }>;
|
|
261
|
+
flowCheckouts: Array<{ workspace: string; lead: string }>;
|
|
262
|
+
appointments: Array<{ workspace: string; attendeeEmail: string }>;
|
|
263
|
+
};
|
|
264
|
+
emails: Array<{ raw: string; normalized: string; storable: boolean }>;
|
|
265
|
+
expected: {
|
|
266
|
+
people: Array<{ workspace: string; email: string; name: string | null; billingCustomerId: string | null }>;
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
const fixture = JSON.parse(
|
|
270
|
+
readFileSync(
|
|
271
|
+
join(__dirname, "..", "..", "..", "..", "..", "database", "__tests__", "fixtures", "person-backfill.json"),
|
|
272
|
+
"utf8",
|
|
273
|
+
),
|
|
274
|
+
) as Fixture;
|
|
275
|
+
|
|
276
|
+
it("normalizePersonEmail and isStorablePersonEmail agree with every pair the fixture lists (the SQL half is person.db.test.ts)", () => {
|
|
277
|
+
expect(fixture.emails.map((e) => e.raw)).toEqual(
|
|
278
|
+
expect.arrayContaining(["vic@x.dev", "\u000Bdana@x.io\u000B", " Eve@X.IO\t"]),
|
|
279
|
+
);
|
|
280
|
+
for (const e of fixture.emails) {
|
|
281
|
+
const n = normalizePersonEmail(e.raw);
|
|
282
|
+
expect({ raw: e.raw, n, storable: isStorablePersonEmail(n) }).toEqual({
|
|
283
|
+
raw: e.raw,
|
|
284
|
+
n: e.normalized,
|
|
285
|
+
storable: e.storable,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("findOrCreatePerson on every raw input email over the backfilled Persons creates zero rows", async () => {
|
|
291
|
+
for (const p of fixture.expected.people) {
|
|
292
|
+
seedPerson({
|
|
293
|
+
workspaceId: fixture.workspaces[p.workspace],
|
|
294
|
+
email: p.email,
|
|
295
|
+
name: p.name,
|
|
296
|
+
billingProvider: p.billingCustomerId === null ? null : "stripe",
|
|
297
|
+
billingCustomerId: p.billingCustomerId,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
const before = people().length;
|
|
301
|
+
const leadsByKey = new Map(fixture.input.leads.map((l) => [l.key, l]));
|
|
302
|
+
const calls: Array<[string, string]> = [
|
|
303
|
+
...fixture.input.leads.map((l): [string, string] => [l.workspace, l.email]),
|
|
304
|
+
...fixture.input.flowCheckouts.map((c): [string, string] => [c.workspace, leadsByKey.get(c.lead)!.email]),
|
|
305
|
+
...fixture.input.appointments.map((a): [string, string] => [a.workspace, a.attendeeEmail]),
|
|
306
|
+
];
|
|
307
|
+
const created: string[] = [];
|
|
308
|
+
for (const [workspace, email] of calls) {
|
|
309
|
+
const result = await findOrCreatePerson(fixture.workspaces[workspace]!, { email });
|
|
310
|
+
if (result.ok && result.value.created) created.push(email);
|
|
311
|
+
}
|
|
312
|
+
expect(created).toEqual([]);
|
|
313
|
+
expect(people()).toHaveLength(before);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe("stampPersonUser (#852 D5, AC-5)", () => {
|
|
318
|
+
const users = () => (store.__db["user"] ??= []);
|
|
319
|
+
const seedUser = (id: string, email: string, emailVerified: boolean) =>
|
|
320
|
+
users().push({ id, email, emailVerified });
|
|
321
|
+
const U = "user_00000000-0000-7000-8000-000000000001";
|
|
322
|
+
const V = "user_00000000-0000-7000-8000-000000000002";
|
|
323
|
+
|
|
324
|
+
it("a verified user whose email normalizes to the Person's is stamped; a second call is already_linked", async () => {
|
|
325
|
+
const p = seedPerson({ email: "ada@example.com" });
|
|
326
|
+
seedUser(U, " ADA@example.com", true);
|
|
327
|
+
expect(await stampPersonUser(WS_A1, p["id"] as string, U)).toBe("stamped");
|
|
328
|
+
expect(people()[0]!["userId"]).toBe(U);
|
|
329
|
+
expect(await stampPersonUser(WS_A1, p["id"] as string, U)).toBe("already_linked");
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("an unverified user, and a verified user with another email, write nothing", async () => {
|
|
333
|
+
const p = seedPerson({ email: "ada@example.com" });
|
|
334
|
+
seedUser(U, "ada@example.com", false);
|
|
335
|
+
seedUser(V, "someone@example.com", true);
|
|
336
|
+
expect(await stampPersonUser(WS_A1, p["id"] as string, U)).toBe("user_unverified");
|
|
337
|
+
expect(await stampPersonUser(WS_A1, p["id"] as string, V)).toBe("email_mismatch");
|
|
338
|
+
expect(people()[0]!["userId"]).toBeNull();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("a Person already linked to U, stamped with V, stays U", async () => {
|
|
342
|
+
const p = seedPerson({ email: "ada@example.com", userId: U });
|
|
343
|
+
seedUser(V, "ada@example.com", true);
|
|
344
|
+
expect(await stampPersonUser(WS_A1, p["id"] as string, V)).toBe("linked_to_other_user");
|
|
345
|
+
expect(people()[0]!["userId"]).toBe(U);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("U already on P1 cannot be stamped onto P2 of the same workspace (P2002) — P2 unchanged", async () => {
|
|
349
|
+
seedPerson({ email: "first@example.com", userId: U });
|
|
350
|
+
const p2 = seedPerson({ email: "ada@example.com" });
|
|
351
|
+
seedUser(U, "ada@example.com", true);
|
|
352
|
+
expect(await stampPersonUser(WS_A1, p2["id"] as string, U)).toBe("user_on_other_person");
|
|
353
|
+
expect(people()[1]!["userId"]).toBeNull();
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("a Person of another workspace is not_found", async () => {
|
|
357
|
+
const p = seedPerson({ workspaceId: WS_A2, email: "ada@example.com" });
|
|
358
|
+
seedUser(U, "ada@example.com", true);
|
|
359
|
+
expect(await stampPersonUser(WS_A1, p["id"] as string, U)).toBe("not_found");
|
|
360
|
+
expect(people()[0]!["userId"]).toBeNull();
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("a store error is internal_error", async () => {
|
|
364
|
+
store.person["findFirst"]!.mockRejectedValueOnce(new Error("down"));
|
|
365
|
+
expect(await stampPersonUser(WS_A1, "person_x", U)).toBe("internal_error");
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
describe("recordCheckoutPerson (#852 D6)", () => {
|
|
370
|
+
const SESSION = "cs_live_852";
|
|
371
|
+
const CHECKOUT = "flchk_00000000-0000-7000-8000-000000000001";
|
|
372
|
+
const LEAD = "lead_00000000-0000-7000-8000-000000000001";
|
|
373
|
+
|
|
374
|
+
function seedCheckout(overrides: Row = {}): void {
|
|
375
|
+
(store.__db["flowCheckout"] ??= []).push({
|
|
376
|
+
id: CHECKOUT,
|
|
377
|
+
leadId: LEAD,
|
|
378
|
+
workspaceId: WS_A1,
|
|
379
|
+
stripeSessionId: SESSION,
|
|
380
|
+
status: "completed",
|
|
381
|
+
...overrides,
|
|
382
|
+
});
|
|
383
|
+
(store.__db["lead"] ??= []).push({ id: LEAD, workspaceId: WS_A1, email: " Buyer@X.io" });
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
it("live mode: the Person of the checkout's lead, with session.customer", async () => {
|
|
387
|
+
seedCheckout();
|
|
388
|
+
expect(
|
|
389
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: SESSION, customer: "cus_9", livemode: true }),
|
|
390
|
+
).toBe("person_written");
|
|
391
|
+
expect(people()[0]).toEqual(
|
|
392
|
+
expect.objectContaining({ workspaceId: WS_A1, email: "buyer@x.io", billingCustomerId: "cus_9" }),
|
|
393
|
+
);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it("live mode onto a Person already linked to a user: the checkout completes and the Person keeps no customer id", async () => {
|
|
397
|
+
seedCheckout();
|
|
398
|
+
seedPerson({ email: "buyer@x.io", userId: "user_00000000-0000-7000-8000-000000000023" });
|
|
399
|
+
expect(
|
|
400
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: SESSION, customer: "cus_9", livemode: true }),
|
|
401
|
+
).toBe("person_kept");
|
|
402
|
+
expect(people()).toHaveLength(1);
|
|
403
|
+
expect(people()[0]!["billingCustomerId"]).toBeNull();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("test mode: the Person, with no customer id", async () => {
|
|
407
|
+
seedCheckout();
|
|
408
|
+
expect(
|
|
409
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: SESSION, customer: "cus_9", livemode: false }),
|
|
410
|
+
).toBe("person_unchanged");
|
|
411
|
+
expect(people()[0]!["billingCustomerId"] ?? null).toBeNull();
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("a session naming a checkout another session completed matches nothing", async () => {
|
|
415
|
+
seedCheckout({ stripeSessionId: "cs_other" });
|
|
416
|
+
expect(
|
|
417
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: SESSION, customer: "cus_9", livemode: true }),
|
|
418
|
+
).toBe("person_skipped");
|
|
419
|
+
expect(people()).toEqual([]);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("a checkout not yet completed matches nothing", async () => {
|
|
423
|
+
seedCheckout({ status: "created" });
|
|
424
|
+
expect(
|
|
425
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: SESSION, customer: "cus_9", livemode: true }),
|
|
426
|
+
).toBe("person_skipped");
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it("a missing session id is skipped before any read; a store error is person_failed", async () => {
|
|
430
|
+
expect(
|
|
431
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: undefined, customer: null, livemode: true }),
|
|
432
|
+
).toBe("person_skipped");
|
|
433
|
+
expect(store.__calls).toEqual([]);
|
|
434
|
+
seedCheckout();
|
|
435
|
+
store.person["findFirst"]!.mockRejectedValueOnce(new Error("down"));
|
|
436
|
+
expect(
|
|
437
|
+
await recordCheckoutPerson({ flowCheckoutId: CHECKOUT, stripeSessionId: SESSION, customer: "cus_9", livemode: true }),
|
|
438
|
+
).toBe("person_failed");
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
describe("resolveMemberPersonId (#7877 D8, AC-3)", () => {
|
|
443
|
+
const users = () => (store.__db["user"] ??= []);
|
|
444
|
+
const U = "user_00000000-0000-7000-8000-000000000011";
|
|
445
|
+
const V = "user_00000000-0000-7000-8000-000000000012";
|
|
446
|
+
const log = { warn: jest.fn() };
|
|
447
|
+
|
|
448
|
+
beforeEach(() => log.warn.mockClear());
|
|
449
|
+
|
|
450
|
+
it("answers the Person already linked to the caller, with no stamp", async () => {
|
|
451
|
+
const p = seedPerson({ email: "cleo@x.io", userId: U });
|
|
452
|
+
expect(await resolveMemberPersonId(WS_A1, U, "cleo@x.io", log)).toBe(p["id"]);
|
|
453
|
+
expect(personCalls().filter((c) => c.op === "updateMany")).toHaveLength(0);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it("stamps an unlinked Person whose email is the caller's verified email (case and padding differ)", async () => {
|
|
457
|
+
const p = seedPerson({ email: "cleo@x.io" });
|
|
458
|
+
users().push({ id: U, email: " CLEO@x.io ", emailVerified: true });
|
|
459
|
+
expect(await resolveMemberPersonId(WS_A1, U, " CLEO@x.io ", log)).toBe(p["id"]);
|
|
460
|
+
expect(people()[0]!["userId"]).toBe(U);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("a Person linked to another user is not stamped and not served; the refusal is logged by kind", async () => {
|
|
464
|
+
seedPerson({ email: "cleo@x.io", userId: V });
|
|
465
|
+
users().push({ id: U, email: "cleo@x.io", emailVerified: true });
|
|
466
|
+
expect(await resolveMemberPersonId(WS_A1, U, "cleo@x.io", log)).toBeNull();
|
|
467
|
+
expect(people()[0]!["userId"]).toBe(V);
|
|
468
|
+
expect(log.warn).toHaveBeenCalledWith(
|
|
469
|
+
{ event: "member-person-stamp", outcome: "linked_to_other_user" },
|
|
470
|
+
expect.any(String),
|
|
471
|
+
);
|
|
472
|
+
expect(JSON.stringify(log.warn.mock.calls)).not.toContain("cleo@x.io");
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it("no Person in this workspace is null — another workspace's Person is never answered", async () => {
|
|
476
|
+
seedPerson({ workspaceId: WS_A2, email: "cleo@x.io", userId: U });
|
|
477
|
+
expect(await resolveMemberPersonId(WS_A1, U, "cleo@x.io", log)).toBeNull();
|
|
478
|
+
});
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
describe("resolveCallerBillingCustomerId (#7961, spec 7894 D13, AC-8)", () => {
|
|
482
|
+
const users = () => (store.__db["user"] ??= []);
|
|
483
|
+
const U = "user_00000000-0000-7000-8000-000000000031";
|
|
484
|
+
const V = "user_00000000-0000-7000-8000-000000000032";
|
|
485
|
+
const log = { warn: jest.fn() };
|
|
486
|
+
|
|
487
|
+
beforeEach(() => log.warn.mockClear());
|
|
488
|
+
|
|
489
|
+
it("answers the customer id of the Person linked to the caller", async () => {
|
|
490
|
+
seedPerson({ email: "fay@x.io", userId: U, billingProvider: "stripe", billingCustomerId: "cus_fay" });
|
|
491
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBe("cus_fay");
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("links an unlinked Person whose email is the caller's verified email, then answers its id", async () => {
|
|
495
|
+
seedPerson({ email: "fay@x.io", billingProvider: "stripe", billingCustomerId: "cus_fay" });
|
|
496
|
+
users().push({ id: U, email: "Fay@x.io", emailVerified: true });
|
|
497
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "Fay@x.io", log)).toBe("cus_fay");
|
|
498
|
+
expect(people()[0]!["userId"]).toBe(U);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it("a Person matched by email alone is never answered: an unverified caller, and a Person linked to another user, are null", async () => {
|
|
502
|
+
seedPerson({ email: "fay@x.io", billingProvider: "stripe", billingCustomerId: "cus_fay" });
|
|
503
|
+
users().push({ id: U, email: "fay@x.io", emailVerified: false });
|
|
504
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBeNull();
|
|
505
|
+
expect(people()[0]!["userId"]).toBeNull();
|
|
506
|
+
|
|
507
|
+
reset();
|
|
508
|
+
seedPerson({ email: "fay@x.io", userId: V, billingProvider: "stripe", billingCustomerId: "cus_fay" });
|
|
509
|
+
users().push({ id: U, email: "fay@x.io", emailVerified: true });
|
|
510
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBeNull();
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it("a linked Person with no customer id, no Person, and another workspace's Person are all null", async () => {
|
|
514
|
+
seedPerson({ email: "fay@x.io", userId: U });
|
|
515
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBeNull();
|
|
516
|
+
|
|
517
|
+
reset();
|
|
518
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBeNull();
|
|
519
|
+
|
|
520
|
+
seedPerson({ workspaceId: WS_A2, email: "fay@x.io", userId: U, billingCustomerId: "cus_fay" });
|
|
521
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBeNull();
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
it("a store error is null, never a throw, and logs no id or email", async () => {
|
|
525
|
+
seedPerson({ email: "fay@x.io", userId: U, billingCustomerId: "cus_fay" });
|
|
526
|
+
store.person["findFirst"]!
|
|
527
|
+
.mockResolvedValueOnce({ id: people()[0]!["id"] })
|
|
528
|
+
.mockRejectedValueOnce(new Error("down"));
|
|
529
|
+
expect(await resolveCallerBillingCustomerId(WS_A1, U, "fay@x.io", log)).toBeNull();
|
|
530
|
+
expect(JSON.stringify(log.warn.mock.calls)).not.toMatch(/cus_fay|fay@x\.io|user_/);
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
describe("setPersonPlanOffering (#7877 D8, AC-4, AC-5)", () => {
|
|
535
|
+
it("sets and clears the plan, scoped to the workspace", async () => {
|
|
536
|
+
const p = seedPerson({ email: "cleo@x.io" });
|
|
537
|
+
expect(await setPersonPlanOffering(WS_A1, p["id"] as string, "retainer")).toBe("set");
|
|
538
|
+
expect(people()[0]!["planOffering"]).toBe("retainer");
|
|
539
|
+
expect(await setPersonPlanOffering(WS_A1, p["id"] as string, null)).toBe("set");
|
|
540
|
+
expect(people()[0]!["planOffering"]).toBeNull();
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it("another workspace's Person is not_found and unchanged", async () => {
|
|
544
|
+
const p = seedPerson({ workspaceId: WS_A2, email: "cleo@x.io" });
|
|
545
|
+
expect(await setPersonPlanOffering(WS_A1, p["id"] as string, "retainer")).toBe("not_found");
|
|
546
|
+
expect(people()[0]!["planOffering"]).toBeNull();
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it("a store error is internal_error, never a throw", async () => {
|
|
550
|
+
store.person["updateMany"]!.mockRejectedValueOnce(new Error("down"));
|
|
551
|
+
expect(await setPersonPlanOffering(WS_A1, "person_x", "retainer")).toBe("internal_error");
|
|
552
|
+
});
|
|
553
|
+
});
|