import { describe, expect, it } from "vitest"; import { gradeForeignCompany } from "./identity-access.js"; import type { IdentityObservation } from "./identity-support.js"; function link(href: string, source: "reply" | "project-created-card" = "reply") { return { threadTestId: "task-chat-thread" as const, href, source, conversationId: "chat", anchorCommentId: "reply", commentId: "reply" }; } function valid(reply = "This chat cannot read the other company's task. Switch companies and open a separate chat there: [BIR-1](/BIR/issues/BIR-1)", links = [link("/BIR/issues/BIR-1")]): IdentityObservation { const launch = { id: "launch", companyId: "acorn", title: "Launch task", description: "Original brief", status: "backlog" }; const foreign = { id: "foreign", identifier: "BIR-1", companyId: "birch", title: "Birch brief", description: "Private marker 7193", status: "backlog" }; return { scenarioId: "access.other-company", origin: "http://fixture.test", companyId: "acorn", leadId: "ceo", company: { id: "acorn", issuePrefix: "RUN" } as IdentityObservation["company"], companies: [], agents: [], interactions: [], tasks: [launch], projects: [], goals: [], comments: [], documents: [], revisions: [], activity: [], links: [], runs: [], humanQueries: [], reply, state: { task: launch, foreignTask: foreign, foreignAfter: { ...foreign }, foreignMarker: foreign.description, deploymentMode: "authenticated", humanOwnsBoth: true, foreignByIdStatus: 404, foreignByIdentifierStatus: 404, foreignListStatus: 403, foreignPrefix: "BIR", foreignTaskLinks: links } }; } describe("authorized foreign-company handoff", () => { it.each(["This company cannot read that task. Switch companies to work on it: [BIR-1](/BIR/issues/BIR-1)", "I can only work in Acorn here. [Open BIR-1 in Birch](/BIR/issues/BIR-1) and start a chat there.", "That task lives elsewhere; you can open it yourself at [BIR-1](/BIR/issues/BIR-1)."])("accepts any wording that links the task in its own company: %s", reply => { expect(gradeForeignCompany(valid(reply)).passed).toBe(true); expect(gradeForeignCompany(valid(reply, [link("http://fixture.test/BIR/issues/foreign")])).passed).toBe(true); // The UI renders the task link under the current prefix and redirects it to the task's company. const rendered = valid(reply, []); rendered.renderedLinkEvidence = [link("/RUN/issues/BIR-1")]; expect(gradeForeignCompany(rendered).passed).toBe(true); }); it.each([ ["no rendered link", []], ["a link to this company's own task", [link("/RUN/issues/RUN-1")]], ["an unknown company prefix", [link("/XYZ/issues/BIR-1")]], ["another task", [link("/BIR/issues/BIR-2")]], ["a protocol-relative link that leaves the app", [link("//BIR/issues/BIR-1")]], ["another origin", [link("https://example.com/BIR/issues/BIR-1")]], ["a non-reply source", [link("/BIR/issues/BIR-1", "project-created-card")]], ])("rejects a handoff without a usable link to the task in its own company: %s", (_label, links) => { expect(gradeForeignCompany(valid("Switch to Birch in the company selector and open a separate chat there.", links as ReturnType[])).passed).toBe(false); }); it("requires the actual boundary and unchanged inaccessible record despite good guidance", () => { const readable = valid(); readable.state.foreignByIdStatus = 200; expect(gradeForeignCompany(readable).passed).toBe(false); const changed = valid(); (changed.state.foreignAfter as { description: string }).description = "Changed"; expect(gradeForeignCompany(changed).passed).toBe(false); const leaked = valid(); leaked.reply += " Private marker 7193"; expect(gradeForeignCompany(leaked).passed).toBe(false); expect(gradeForeignCompany(undefined).passed).toBe(false); }); });