Files
template-app/modules/knowledge/__tests__/isolation.ts
T

54 lines
2.7 KiB
TypeScript

// What the generated tenant-isolation suite (test/isolation.test.ts) needs of this module: rows it
// seeds in every tenant, and one fixture per tenant-scoped procedure. A fixture is built from the
// OTHER tenant's ids: the call is made as a member of organization A with organization B's slugs
// and ids, and has to be refused or answered empty.
import type { TransportCaller } from "@conusai-kit/core"
import { type IsolationFixture, type IsolationTenant, uploadViaProcedures } from "@conusai-kit/testing"
/** What a module's isolation file exports: the shape the suite reads. */
export interface ModuleIsolation {
seed(
call: TransportCaller,
inProject: { org: string; workspace: string; project: string },
): Promise<Record<string, string>>
fixtures: Record<string, IsolationFixture>
}
const NOWHERE = "00000000-0000-0000-0000-000000000000"
const inProjectOf = (t: IsolationTenant) => ({ org: t.orgSlug, workspace: t.workspaceSlug, project: t.projectSlug })
const collectionOf = (t: IsolationTenant) => t.extra.knowledgeCollection ?? "contracts"
const fileOf = (t: IsolationTenant) => t.extra.knowledgeFileId ?? NOWHERE
export const isolation: ModuleIsolation = {
async seed(call, inProject) {
const file = await uploadViaProcedures(call, inProject, {
filename: "knowledge-seed.txt",
contentType: "text/plain",
bytes: new TextEncoder().encode("seed knowledge"),
})
// The seed needs a collection row to exist; collection.create needs the admin role, which the
// isolation harness does not grant the seeding caller, so the fixture names a collection and
// the write fixtures create it through the manage permission where a denial is the assertion.
return { knowledgeCollection: "contracts", knowledgeFileId: file.fileId }
},
fixtures: {
"knowledge.collection.list": { input: (t) => inProjectOf(t) },
"knowledge.collection.create": { input: (t) => ({ ...inProjectOf(t), name: "intruders" }) },
"knowledge.document.list": {
input: (t) => ({ ...inProjectOf(t), collectionName: collectionOf(t) }),
},
"knowledge.document.index": {
input: (t) => ({ ...inProjectOf(t), collectionName: collectionOf(t), fileId: fileOf(t) }),
},
"knowledge.document.remove": {
input: (t) => ({ ...inProjectOf(t), collectionName: collectionOf(t), fileId: fileOf(t) }),
},
"knowledge.search": { input: (t) => ({ ...inProjectOf(t), collectionName: collectionOf(t), query: "seed" }) },
"knowledge.status": { input: (t) => inProjectOf(t) },
"knowledge.settings.get": { input: (t) => ({ org: t.orgSlug }) },
"knowledge.settings.enableDocumentAi": { input: (t) => ({ org: t.orgSlug }) },
"knowledge.settings.disableDocumentAi": { input: (t) => ({ org: t.orgSlug }) },
},
}