Fresh history cut from cynarAI/evoo antigravity-handoff/project-timeline (Voice First + project timeline). Brand docs/titles as evOo; keep @rakazo/* packages as tech debt. Excludes node_modules, caches, secrets, voice blobs.
102 lines
4.2 KiB
TypeScript
102 lines
4.2 KiB
TypeScript
import type { Actor } from "@rakazo/contracts";
|
|
import { AI_DISCLOSURE_VERSION } from "@rakazo/contracts";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("./thread-target.js", () => ({
|
|
resolveThreadTarget: async () => ({ kind: "bot", botId: "bot" }),
|
|
}));
|
|
|
|
import { aiConsentStatus, allowAiConsent } from "./ai-consent.js";
|
|
import type { RouterDeps } from "./router.js";
|
|
|
|
function setup() {
|
|
const upsert = vi.fn(async () => ({}));
|
|
const deps = {
|
|
env: { agentRuntime: "scripted" },
|
|
prisma: {
|
|
spaceModelPreference: { findMany: vi.fn(async () => []), findFirst: vi.fn(async () => null) },
|
|
secret: { findMany: vi.fn(async () => []) },
|
|
bot: { findMany: vi.fn(async () => []) },
|
|
spaceVoicePreference: {
|
|
findFirst: vi.fn(async () => ({ credential: { provider: "openai" }, voiceId: "alloy" })),
|
|
findMany: vi.fn(async () => [{ credential: { provider: "openai" } }]),
|
|
},
|
|
spaceMemoryConfig: { findUnique: vi.fn(async () => null) },
|
|
deploymentSettings: { findUnique: vi.fn(async () => null) },
|
|
aiDataConsent: { findMany: vi.fn(async () => []), upsert },
|
|
$transaction: vi.fn(async (queries) => Promise.all(queries)),
|
|
},
|
|
} as unknown as RouterDeps;
|
|
return { deps, upsert, actor: { userId: "user", spaceId: "space" } as Actor };
|
|
}
|
|
describe("consent grants", () => {
|
|
it("includes reachable helper models and cloud services for a targeted send", async () => {
|
|
const { deps, actor } = setup();
|
|
deps.env.agentRuntime = "pi";
|
|
deps.cloudAgent = {
|
|
key: "test",
|
|
spaceId: actor.spaceId,
|
|
provider: { describe: () => ({ id: "cursor", capabilities: { offline: false } }) },
|
|
} as RouterDeps["cloudAgent"];
|
|
vi.mocked(deps.prisma.spaceModelPreference.findMany).mockResolvedValue([
|
|
{
|
|
modelId: "helper-model",
|
|
isDefault: false,
|
|
credential: { provider: "anthropic", secretId: "test-secret" },
|
|
},
|
|
] as never);
|
|
const status = await aiConsentStatus(deps, actor, { botId: "bot", uses: ["model"] });
|
|
expect(status.recipients.map((recipient) => recipient.name)).toEqual(
|
|
expect.arrayContaining(["Anthropic", "Cursor"]),
|
|
);
|
|
expect(deps.prisma.spaceVoicePreference.findMany).not.toHaveBeenCalled();
|
|
expect(deps.prisma.bot.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: expect.objectContaining({ id: { in: ["bot"] } }) }),
|
|
);
|
|
});
|
|
it("discloses only the selected voice provider before a voice action", async () => {
|
|
const { deps, actor } = setup();
|
|
vi.mocked(deps.prisma.spaceVoicePreference.findMany).mockResolvedValue([
|
|
{ credential: { provider: "openai" } },
|
|
{ credential: { provider: "elevenlabs" } },
|
|
] as never);
|
|
const status = await aiConsentStatus(deps, actor, { uses: ["voice"] });
|
|
expect(status.recipients).toHaveLength(1);
|
|
expect(status.recipients[0]?.name).toBe("OpenAI");
|
|
expect(deps.prisma.spaceVoicePreference.findMany).not.toHaveBeenCalled();
|
|
});
|
|
it("returns an operator policy without requiring a hosted provider", async () => {
|
|
const { deps, actor } = setup();
|
|
deps.env.privacyPolicyUrl = "https://example.com/privacy";
|
|
expect((await aiConsentStatus(deps, actor)).privacyUrl).toBe("https://example.com/privacy");
|
|
});
|
|
it("rejects stale disclosures, unknown recipients, and a changed account or Space", async () => {
|
|
const { deps, actor, upsert } = setup();
|
|
const current = await aiConsentStatus(deps, actor);
|
|
const input = {
|
|
scope: current.scope,
|
|
version: current.version,
|
|
keys: [current.recipients[0]!.key],
|
|
};
|
|
for (const invalid of [
|
|
{ ...input, version: "old" },
|
|
{ ...input, scope: "different-account" },
|
|
{ ...input, keys: ["unknown"] },
|
|
]) {
|
|
await expect(allowAiConsent(deps, actor, invalid)).rejects.toThrow("changed");
|
|
}
|
|
expect(upsert).not.toHaveBeenCalled();
|
|
await allowAiConsent(deps, actor, input);
|
|
expect(upsert).toHaveBeenCalledExactlyOnceWith(
|
|
expect.objectContaining({
|
|
create: {
|
|
userId: actor.userId,
|
|
spaceId: actor.spaceId,
|
|
recipientKey: input.keys[0],
|
|
version: AI_DISCLOSURE_VERSION,
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
});
|