Files
template-app/test/dialect/profile-registry.test.ts
T
2026-11-14 09:56:45 +00:00

68 lines
4.0 KiB
TypeScript

// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0
import { type AppEnv, loadAppEnv, profileRegistry, type ServerObservability } from "@app/config"
import { appModules, moduleInclusion } from "@app/modules"
import { buildRegistry } from "@conusai-kit/core"
import { createMemoryDataSource, entitiesOf } from "@conusai-kit/data"
import { users } from "@conusai-kit/tenancy"
import { afterEach, describe, expect, it, vi } from "vitest"
import { buildDialectWorkerRuntime, engineFor, migrateDialect } from "../../apps/worker/src/dialect/runtime.js"
import { makeApp } from "../../modules/account/__tests__/harness.js"
import type { AccountExport, DeletionView } from "../../modules/account/index.js"
import { freshDialectModules } from "./fixture.js"
afterEach(() => vi.restoreAllMocks())
describe.each(["mariadb", "mongodb"] as const)("%s module/profile composition", (profile) => {
it("names unsupported adopted modules and preserves their custody registry", () => {
const before = JSON.stringify(moduleInclusion)
expect(() => profileRegistry(appModules, profile)).toThrow(/agent, assistant/)
expect(JSON.stringify(moduleInclusion)).toBe(before)
expect(buildRegistry([...appModules]).modules.map(({ id }) => id)).toContain("assistant")
})
it("uses the same compatible fresh registry for data and account rights without changing starter history", async () => {
const before = JSON.stringify(moduleInclusion)
const modules = freshDialectModules(profile)
const registry = profileRegistry(modules, profile)
const data = createMemoryDataSource({ entities: entitiesOf(registry, profile) })
const app = makeApp({ data, modules })
expect(app.registry.modules.map(({ id }) => id)).toEqual(registry.modules.map(({ id }) => id))
const ada = await app.signUp("profile-ada@example.test")
const grace = await app.signUp("profile-grace@example.test")
await ada.call().callByName("tenancy.org.create", { slug: "profile-ada", name: "Ada fixture" })
await grace.call().callByName("tenancy.org.create", { slug: "profile-grace", name: "Grace fixture" })
const exported = (await ada.call().callByName("account.data.export", {})) as AccountExport
expect(exported).toMatchObject({ subject: ada.actor.userId, complete: true, failed: [] })
expect(JSON.stringify(exported)).not.toContain(grace.email)
const deleted = (await ada.call().callByName("account.deletion.request", { confirm: ada.email })) as DeletionView
expect(deleted).toMatchObject({ state: "completed", pending: [], abandoned: [] })
expect(await data.global(users).get(ada.actor.userId)).toBeNull()
expect((await data.global(users).get(grace.actor.userId))?.email).toBe(grace.email)
expect(JSON.stringify(moduleInclusion)).toBe(before)
})
it("refuses the actual worker and migrate paths before connecting or applying migrations", async () => {
const databaseUrl = `${profile === "mariadb" ? "mysql" : "mongodb"}://127.0.0.1:1/fixture`
const raw: Record<string, string> = {
NODE_ENV: "test",
CONUSAI_API_URL: "https://fixture.example.test/api",
CONUSAI_API_TOKEN: "synthetic-token",
CONUSAI_PROJECT_ID: "profile-refusal",
REDIS_URL: "redis://127.0.0.1:1",
[profile === "mariadb" ? "MARIADB_URL" : "MONGODB_URL"]:
`${profile === "mariadb" ? "mysql" : "mongodb"}://127.0.0.1:1/fixture`,
}
if (profile === "mariadb") raw.DATABASE_URL = databaseUrl
const env: AppEnv = loadAppEnv("worker", raw, profile)
const engine = engineFor(profile)
const connect = vi
.spyOn(engine, "connect")
.mockRejectedValue(new Error("database access forbidden in refusal control"))
const migrate = vi.spyOn(engine, "migrate").mockRejectedValue(new Error("migration forbidden in refusal control"))
await expect(buildDialectWorkerRuntime(env, {} as ServerObservability)).rejects.toThrow(/agent, assistant/)
expect(() => migrateDialect(env)).toThrow(/agent, assistant/)
expect(connect).not.toHaveBeenCalled()
expect(migrate).not.toHaveBeenCalled()
})
})