// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0 // A `transport: "business"` procedure on the fake platform of @conusai-kit/testing, composed as the // app composes it (`businessServiceComposition`): binding lookup and partition admission over HTTP, // then the CRM service in the binding's partition. A fake platform; no live readiness is asserted. import { type Actor, buildRegistry, Container, createCaller } from "@conusai-kit/core" import { createMemoryDataSource, DATA_SOURCE, entitiesOf, SYSTEM_DATA } from "@conusai-kit/data" import { createTenancyHooks, TENANCY, tenancyModule, users } from "@conusai-kit/tenancy" import { fakeNativeServices, startFakePlatform } from "@conusai-kit/testing/fake-platform" import { afterAll, beforeAll, describe, expect, it } from "vitest" import { bindOnFakePlatform, declaredBusinessScopes } from "../apps/worker/src/fake-business-bindings.js" import { businessServiceComposition } from "../config/src/business-services.js" import { crmModule } from "../modules/crm/module.js" const simulated = fakeNativeServices({ token: "dev-token" }) let platform: Awaited> beforeAll(async () => { platform = await startFakePlatform(0, { deployment: simulated.authority }) }) afterAll(async () => { await platform.close() }) async function app() { const registry = buildRegistry([tenancyModule, crmModule]) const data = createMemoryDataSource({ entities: entitiesOf(registry, "postgres") }) const root = new Container() root.value(DATA_SOURCE, data) root.value(SYSTEM_DATA, data) const env = { platform: "fake" as const, production: false, deployment: { ...simulated.deployment, CONUSAI_API_URL: `${platform.url}/api`, CONUSAI_API_TOKEN: "dev-token" }, } const hooks = businessServiceComposition(env, [crmModule], "postgres") const caller = (actor: Actor) => { const container = root.child() const tenancy = createTenancyHooks({ data, registry }) container.value(TENANCY, tenancy) return createCaller({ registry, container, tenancy, ...hooks }, { actor }) } const owner = await data.global(users).insert({ issuer: "https://identity.example.test", subject: "owner", email: "owner@example.test", displayName: "Owner", locale: "en", }) const call = caller({ userId: owner.id }) const org = (await call.callByName("tenancy.org.create", { slug: "buyer", name: "Buyer" })) as { orgId: string } await call.callByName("tenancy.workspace.create", { org: "buyer", slug: "purchasing", name: "Purchasing", visibility: "org", }) await call.callByName("tenancy.project.create", { org: "buyer", workspace: "purchasing", slug: "suppliers", name: "Suppliers", moduleKey: "crm", }) return { env, registry, call, orgId: org.orgId, project: { org: "buyer", workspace: "purchasing", project: "suppliers" }, } } describe("a business procedure on the fake platform", () => { it("binds what the selected modules declare, and nothing for an app without business modules", () => { expect(declaredBusinessScopes(buildRegistry([tenancyModule, crmModule]))).toEqual({ crm: expect.arrayContaining(["crm.parties:read", "crm.parties:write"]), }) expect(declaredBusinessScopes(buildRegistry([tenancyModule]))).toEqual({}) }) it("is refused until the app tenant is bound, then admitted and served from its partition", async () => { const { env, registry, call, orgId, project } = await app() await expect(call.callByName("crm.party.list", project)).rejects.toMatchObject({ code: "service_partition_missing", }) expect(await bindOnFakePlatform(env, registry, [orgId])).toBe(1) const created = (await call.callByName("crm.party.create", { ...project, kind: "organization", displayName: "Synthetic supplier", country: "LT", requestId: "01900000-0000-7000-8000-000000000010", })) as { partyId: string; displayName: string } expect(created.displayName).toBe("Synthetic supplier") const listed = (await call.callByName("crm.party.list", project)) as { rows: Array<{ partyId: string }> } expect(listed.rows.map((row) => row.partyId)).toEqual([created.partyId]) const binding = platform.business?.calls.find((entry) => entry.binding !== undefined)?.binding expect(binding).toBeDefined() expect(platform.business?.partition(binding as string).crm.parties.size).toBe(1) }) it("is refused by name once the organization disables the service", async () => { const { env, registry, call, orgId, project } = await app() await bindOnFakePlatform(env, registry, [orgId]) platform.business?.disable("crm") try { await expect(call.callByName("crm.party.list", project)).rejects.toMatchObject({ code: "service_not_enabled" }) } finally { platform.business?.enable("crm") } }) it("never provisions outside a development run on the fake platform", async () => { const { env, registry, orgId } = await app() await expect(bindOnFakePlatform({ ...env, platform: "platform" }, registry, [orgId])).rejects.toThrow( /administrator/, ) await expect(bindOnFakePlatform({ ...env, production: true }, registry, [orgId])).rejects.toThrow(/administrator/) }) })