136 lines
5.5 KiB
TypeScript
136 lines
5.5 KiB
TypeScript
import { simulatedBusinessAdmission } from "../../../test/helpers/business-admission.js"
|
|
// Simulated adapter dispatch; these fixtures do not prove gateway or partition isolation.
|
|
// The module on its own, wired the way apps/web wires a request -- tenancy on the in-memory data
|
|
// source, the archive module, and the Archive port bound to the real generated client over the
|
|
// business-services fake of @conusai-kit/testing -- so its procedures are tested whether or not this
|
|
// app turned the module on.
|
|
|
|
import { businessClient, type Client } from "@conusai-kit/business"
|
|
import { type Actor, buildRegistry, Container, createCaller, type TransportCaller } from "@conusai-kit/core"
|
|
import { createMemoryDataSource, DATA_SOURCE, entitiesOf, OBJECT_STORE, SYSTEM_DATA } from "@conusai-kit/data"
|
|
import { files, filesModule } from "@conusai-kit/files"
|
|
import { createTenancyHooks, TENANCY, tenancyModule, users } from "@conusai-kit/tenancy"
|
|
import { type FakeBusinessServices, fakeBusinessServices, fakeObjectStore } from "@conusai-kit/testing"
|
|
import { createClient } from "@conusai-sdk/archive/client"
|
|
import { sdkArchiveService } from "../adapters/service.js"
|
|
import { ARCHIVE_SERVICE } from "../core/ports.js"
|
|
import { archiveModule } from "../module.js"
|
|
|
|
export interface Tenant {
|
|
readonly owner: Actor
|
|
readonly call: TransportCaller
|
|
readonly orgSlug: string
|
|
readonly orgId: string
|
|
readonly workspaceSlug: string
|
|
readonly workspaceId: string
|
|
readonly projectSlug: string
|
|
readonly projectId: string
|
|
readonly slugs: { org: string; workspace: string; project: string }
|
|
}
|
|
|
|
export function archiveApp(options: Parameters<typeof fakeBusinessServices>[0] = {}) {
|
|
const fake: FakeBusinessServices = fakeBusinessServices(options)
|
|
const client: Client = businessClient({ apiUrl: fake.apiUrl, token: fake.token, orgSlug: fake.orgSlug }, createClient)
|
|
client.setConfig({ fetch: fake.fetch as typeof fetch })
|
|
// The files module for its table alone: its procedures have their fixtures in test/isolation.test.ts.
|
|
const registry = buildRegistry([tenancyModule, { ...filesModule, procedures: [] }, archiveModule])
|
|
const entities = entitiesOf(registry, "postgres")
|
|
const data = createMemoryDataSource({ entities })
|
|
const root = new Container()
|
|
root.value(DATA_SOURCE, data)
|
|
root.value(SYSTEM_DATA, data)
|
|
root.value(OBJECT_STORE, fakeObjectStore())
|
|
root.value(ARCHIVE_SERVICE, sdkArchiveService({ client, orgSlug: fake.orgSlug }))
|
|
|
|
const caller = (actor: Actor): TransportCaller => {
|
|
const tenancy = createTenancyHooks({ data, registry })
|
|
const container = root.child()
|
|
container.value(TENANCY, tenancy)
|
|
return createCaller({ registry, container, tenancy, serviceReadiness: simulatedBusinessAdmission }, { actor })
|
|
}
|
|
|
|
const signUp = async (email: string): Promise<Actor> => {
|
|
const user = await data.global(users).insert({
|
|
issuer: "https://id.example.test",
|
|
subject: email,
|
|
email,
|
|
displayName: email.split("@")[0] ?? email,
|
|
locale: "en",
|
|
})
|
|
return { userId: user.id }
|
|
}
|
|
|
|
/** An organization with one workspace and one project, owned by a new user. */
|
|
const tenant = async (slug: string): Promise<Tenant> => {
|
|
const owner = await signUp(`owner@${slug}.test`)
|
|
const call = caller(owner)
|
|
const org = (await call.callByName("tenancy.org.create", { slug, name: slug })) as { orgId: string; slug: string }
|
|
const workspace = (await call.callByName("tenancy.workspace.create", {
|
|
org: slug,
|
|
slug: `ws-${slug}`,
|
|
name: `Workspace ${slug}`,
|
|
visibility: "org",
|
|
})) as { workspaceId: string; slug: string }
|
|
const project = (await call.callByName("tenancy.project.create", {
|
|
org: slug,
|
|
workspace: workspace.slug,
|
|
slug: `p-${slug}`,
|
|
name: `Project ${slug}`,
|
|
moduleKey: "archive",
|
|
})) as { projectId: string; slug: string }
|
|
return {
|
|
owner,
|
|
call,
|
|
orgSlug: org.slug,
|
|
orgId: org.orgId,
|
|
workspaceSlug: workspace.slug,
|
|
workspaceId: workspace.workspaceId,
|
|
projectSlug: project.slug,
|
|
projectId: project.projectId,
|
|
slugs: { org: org.slug, workspace: workspace.slug, project: project.slug },
|
|
}
|
|
}
|
|
|
|
/** A second user who joins `t`'s organization with the org role `role`. */
|
|
const member = async (t: Tenant, email: string, role: "viewer" | "member"): Promise<TransportCaller> => {
|
|
const invite = (await t.call.callByName("tenancy.member.invite", { org: t.orgSlug, email, role })) as {
|
|
token: string
|
|
}
|
|
const actor = await signUp(email)
|
|
const call = caller(actor)
|
|
await call.callByName("tenancy.invitation.accept", { org: t.orgSlug, token: invite.token })
|
|
return call
|
|
}
|
|
|
|
/** A file stored in `t`'s project, as the files module leaves an upload it has finished. */
|
|
const storedFile = async (t: Tenant, sha256 = "a".repeat(64), size = 1234): Promise<string> => {
|
|
const scope = {
|
|
level: "project" as const,
|
|
actor: t.owner,
|
|
orgId: t.orgId,
|
|
workspaceId: t.workspaceId,
|
|
projectId: t.projectId,
|
|
}
|
|
const row = await data
|
|
.forScope(scope)
|
|
.of(files)
|
|
.insert({
|
|
status: "stored",
|
|
storageKey: `files/${t.workspaceId}/${t.projectId}/${crypto.randomUUID()}`,
|
|
filename: "record.pdf",
|
|
contentType: "application/pdf",
|
|
size,
|
|
sha256,
|
|
uploadedBy: t.owner.userId,
|
|
storedAt: "2026-11-02T10:00:00.000Z",
|
|
uploadId: null,
|
|
declaredSize: null,
|
|
partSize: null,
|
|
deletingAt: null,
|
|
})
|
|
return row.id
|
|
}
|
|
|
|
return { fake, client, registry, data, caller, signUp, tenant, member, storedFile }
|
|
}
|