import { Container, type TenantScope } from "@conusai-kit/core" import { createMemoryDataSource, DATA_SOURCE, SYSTEM_DATA } from "@conusai-kit/data" import { accountData, projects } from "@conusai-kit/tenancy" import { expect, it } from "vitest" import { custodyModule } from "../../custody.js" import { knowledgeAccountData } from "../adapters/account.js" import { autoIndex, KNOWLEDGE_ENTITIES } from "../contract/index.js" import { knowledgeModule } from "../module.js" const subject = "01900000-0000-7000-0000-000000000009" async function fixture() { const data = createMemoryDataSource({ entities: [projects, ...KNOWLEDGE_ENTITIES] }) const container = new Container() container.value(DATA_SOURCE, data) container.value(SYSTEM_DATA, data) const scopes: TenantScope[] = [] for (const n of [1, 2, 3]) { const orgId = `01900000-0000-7000-0000-00000000000${n}` const workspaceId = `01900000-0000-7000-0000-00000000001${n}` const actor = { userId: subject } const project = await data .forScope({ level: "workspace", orgId, workspaceId, actor }) .of(projects) .insert({ slug: "site", name: "Site", moduleKey: "notes", archivedAt: null }) const scope: TenantScope = { level: "project", orgId, workspaceId, projectId: project.id, actor } scopes.push(scope) await data .forScope(scope) .of(autoIndex) .insert({ collectionName: "contracts", enabledBy: n === 3 ? "another-person" : subject, enabledAt: "2026-11-11T00:00:00.000Z", }) } return { data, container, scopes } } it("exports and erases only that administrator’s opt-ins across projects, idempotently", async () => { const { data, container, scopes } = await fixture() const exported = await knowledgeAccountData.export?.({ subject, container }) expect(exported?.automaticIndexing).toHaveLength(2) await knowledgeAccountData.erase?.({ subject, container }) expect(await knowledgeAccountData.export?.({ subject, container })).toEqual({ automaticIndexing: [] }) await knowledgeAccountData.erase?.({ subject, container }) const otherScope = scopes[2] if (!otherScope) throw new Error("Missing unrelated project fixture") expect((await data.forScope(otherScope).of(autoIndex).list()).items).toHaveLength(1) }) it("retains the account contribution when processing is deselected", () => { const retained = custodyModule(knowledgeModule) expect(retained.extends?.find((entry) => entry.point.id === accountData.id)?.values).toEqual([knowledgeAccountData]) }) it("an aborted account operation changes no opt-in", async () => { const { data, container, scopes } = await fixture() const controller = new AbortController() controller.abort() await expect(knowledgeAccountData.erase?.({ subject, container, signal: controller.signal })).rejects.toThrow() for (const scope of scopes) expect((await data.forScope(scope).of(autoIndex).list()).items).toHaveLength(1) })