Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/external-notion-assignment.ts
T

65 lines
10 KiB
TypeScript

import { expect } from "@playwright/test";
import type { ToolProfileEffectiveSummary, ToolRunDecisionLookup, ToolConnection } from "../../../packages/shared/src/types/tool-access.js";
import { definition, root, checks, externalObservation, observeExternal, verifyRenderedReply } from "./external-support.js";
import { externalRequirements, selectedExternalBinding } from "./external-fixtures.js";
import { readNotionFixturePages, connectNamedNotion, type NotionPageOracle } from "./external-notion-provider.js";
import { notionFactSpan, notionDateMentioned, notionPageIdentityMatches } from "./external-notion-semantics.js";
import { identityWorker, waitForReply } from "./identity-support.js";
import { sendChatMessage } from "../chat-flow.js";
import type { ChatCatalogContext } from "../chat-catalog-contract.js";
async function decisions(ctx: ChatCatalogContext) { return Promise.all((await ctx.allRuns()).map(run => ctx.input.api.get<ToolRunDecisionLookup>(`${root(ctx)}/tools/runs/${run.id}/decisions`))); }
function successfulRead(rows: ToolRunDecisionLookup[], state: Record<string, unknown>, runId?: string) { return rows.some(row => (!runId || row.runId === runId) && row.decisions.some(decision => decision.invocation.runId === row.runId && decision.invocation.connectionId === state.connectionId && decision.invocation.agentId === state.ceoId && decision.invocation.issueId === state.sourceIssueId && (state.readEntryIds as string[]).includes(decision.invocation.catalogEntryId ?? "") && decision.invocation.status === "succeeded")); }
export function gradeNotionAssignment(value: unknown) {
const c = checks(), o = externalObservation(value); c.add("complete-named-notion-assignment-evidence", !!o, "Actual official Notion workspace, Morgan-only assignment, human grant and continued model read are required."); if (!o) return c.result();
c.add("usable-account-assigned-only-to-morgan", o.state.namedProvider === "notion" && o.state.workspaceMatched === true && o.state.healthyBefore === true && o.state.morganHadReadBefore === true && o.state.ceoHadReadBefore === false && o.state.morganId !== o.leadId, "Official provider reads and public effective access independently establish a usable account unavailable to this CEO.");
c.add("honest-assignment-handoff", o.state.beforeGrantReadCount === 0 && o.state.initialAccessExplanation === true && o.state.initialResearchClaim === false && o.state.humanManagementVisible === true, "The original phase identifies assignment as a prerequisite without borrowing Morgan's authority or inventing a summary.");
c.add("same-account-authorized-assignment", o.state.authorizedGrant === true && o.activity.some(row => row.entityId === o.state.connectionId && row.action === "tool_app.finished" && row.actorType === "user") && o.state.operatorChannel === "public-browser-agent-selection" && o.state.sameConnectionCount === true && o.state.morganRetainedRead === true && o.state.ceoHasReadAfter === true && o.state.fixturesUnchanged === true, "The authorized human adds this CEO to the existing connection without replacing Morgan or creating a duplicate account.");
const page = (o.state.pages as NotionPageOracle[] | undefined)?.[0], replies = o.phaseReplies.filter(row => row.runId !== o.state.initialRunId), body = replies.map(row => row.body).join("\n");
c.add("phase-bound-current-workspace-read", Array.isArray(o.state.decisions) && replies.some(reply => o.runs.some(run => run.id === reply.runId && run.status === "succeeded") && successfulRead(o.state.decisions as ToolRunDecisionLookup[], o.state, reply.runId))
&& !!page && !!notionFactSpan(body, page.fact) && notionDateMentioned(body, page.editedAt) && o.links.some(link => notionPageIdentityMatches(link, page.id)), "The resumed source task performs its own actual named-provider read and grounds its answer in the allowlisted current page body, date and exact source identity.");
return c.result();
}
export const notionAssignmentExternalCases = [definition("connection-setup.notion-agent-unassigned", async ctx => {
const binding = selectedExternalBinding(ctx), pages = await readNotionFixturePages(ctx, binding), morgan = await identityWorker(ctx);
const connection = await connectNamedNotion(ctx, binding, [morgan.id]);
const saved = await ctx.input.api.get<ToolConnection>(`/api/tool-connections/${connection.connectionId}`);
const effective = async (id: string) => ctx.input.api.get<ToolProfileEffectiveSummary>(`${root(ctx)}/tools/profiles/effective/agents/${id}`);
const morganAccess = await effective(morgan.id), ceoAccess = await effective(ctx.input.fixtures.agent.id);
const hasRead = (row: ToolProfileEffectiveSummary) => connection.readEntryIds.some(id => row.allowedTools.some(tool => tool.id === id));
if (saved.status !== "active" || !saved.enabled || saved.healthStatus !== "ok" || !hasRead(morganAccess) || hasRead(ceoAccess)) throw new Error("Setup blocker: actual official Notion connection must be healthy and assigned to Morgan only");
const count = (await ctx.input.api.get<{ connections: ToolConnection[] }>(`${root(ctx)}/tools/connections`)).connections.length;
await ctx.input.api.post(`${root(ctx)}/chats/${ctx.input.fixtures.agent.id}`); await ctx.refreshIssue();
await ctx.input.api.patch(`/api/issues/${ctx.issue()!.id}`, { description: `The intended Launch workspace is on the existing Launch Notion account (${connection.connectionId}). It is already usable by Morgan (${morgan.id}); the CEO is not assigned. The authorized human may assign the existing account to this CEO. Keep Morgan's access.` });
return { ...connection, pages, ceoId: ctx.input.fixtures.agent.id, morganId: morgan.id, namedProvider: "notion", workspaceMatched: true, healthyBefore: true, morganHadReadBefore: true, ceoHadReadBefore: false, connectionCountBefore: count, sourceIssueId: ctx.issue()!.id };
}, gradeNotionAssignment, {
authentication: "human-session", expectedRunCount: 2, expectedHumanMessageCount: 2, attemptTimeoutMs: 480_000,
externalInput: externalRequirements["connection-setup.notion-agent-unassigned"],
sourceFiles: ["chat-catalog-domains/external-notion-assignment.ts", "chat-catalog-domains/external-notion-provider.ts", "chat-catalog-domains/external-notion-semantics.ts", "chat-catalog-domains/external-fixtures.ts", "chat-catalog-domains/external-support.ts", "chat-catalog-domains/internal-support.ts", "chat-catalog-domains/identity-support.ts", "chat-flow.ts", "chat-scenarios/connection-setup.yaml"],
async act(ctx, state) {
await waitForReply(ctx); const o = await observeExternal(ctx, state); state.initialRunId = o.phaseReplies.at(-1)?.runId;
const rows = await decisions(ctx); state.beforeGrantReadCount = rows.reduce((count, row) => count + row.decisions.filter(decision => decision.invocation.connectionId === state.connectionId && decision.invocation.status === "succeeded").length, 0);
state.initialAccessExplanation = /assign|access|permission|not.*available|not.*connect/i.test(o.reply) && /notion|morgan/i.test(o.reply);
const page = (state.pages as NotionPageOracle[])[0]!; state.initialResearchClaim = !!notionFactSpan(o.reply, page.fact);
const prefix = `/${ctx.input.fixtures.company.issuePrefix}/apps/${state.connectionId}`;
const href = o.links.find(link => { try { const url = new URL(link, o.origin); return url.origin === o.origin && (url.pathname === prefix || url.pathname.startsWith(`${prefix}/`)); } catch { return false; } });
if (!href) throw new Error("Chat did not provide the existing account's supported access-management handoff");
const source = ctx.input.page.url(); await ctx.input.page.goto(new URL(href, o.origin).href, { waitUntil: "domcontentloaded" }); await expect(ctx.input.page.getByRole("main")).toContainText("Launch Notion"); state.humanManagementVisible = true;
if (!ctx.input.page.url().endsWith("/permissions")) await ctx.input.page.goto(`${prefix}/permissions`, {waitUntil:"domcontentloaded"});
await ctx.input.page.getByRole("button", {name:"1 agent selected",exact:true}).click();
const completed = ctx.input.page.waitForResponse(response => new URL(response.url()).pathname === `${root(ctx)}/tools/apps/${state.connectionId}/finish` && response.request().method() === "POST" && response.ok());
await ctx.input.page.getByRole("checkbox", {name:`Allow ${ctx.input.fixtures.agent.name}`,exact:true}).check();
const response = await completed, request = response.request().postDataJSON() as {access:{agentIds?:string[]}};
state.authorizedGrant = Array.isArray(request.access.agentIds) && request.access.agentIds.length === 2 && request.access.agentIds.includes(String(state.morganId)) && request.access.agentIds.includes(String(state.ceoId)); state.operatorChannel = "public-browser-agent-selection";
await ctx.input.page.goto(source, { waitUntil: "domcontentloaded" }); await sendChatMessage(ctx.input.page, "I assigned the existing Notion account to you and kept Morgan's access. Please continue the original launch summary here.");
},
async observe(ctx, state) {
const pages = await readNotionFixturePages(ctx), before = state.pages as NotionPageOracle[];
state.fixturesUnchanged = pages.length === before.length && pages.every((page, i) => page.id === before[i]?.id && page.editedAt === before[i]?.editedAt && JSON.stringify(page.fact) === JSON.stringify(before[i]?.fact));
const connections = await ctx.input.api.get<{ connections: ToolConnection[] }>(`${root(ctx)}/tools/connections`); state.sameConnectionCount = connections.connections.length === state.connectionCountBefore && connections.connections.some(row => row.id === state.connectionId);
for (const [id, field] of [[state.ceoId, "ceoHasReadAfter"], [state.morganId, "morganRetainedRead"]] as const) { const effective = await ctx.input.api.get<ToolProfileEffectiveSummary>(`${root(ctx)}/tools/profiles/effective/agents/${id}`); state[field] = (state.readEntryIds as string[]).some(read => effective.allowedTools.some(tool => tool.id === read)); }
state.decisions = await decisions(ctx); return observeExternal(ctx, state);
},
async verifyResult(ctx, state, value) { await verifyRenderedReply(ctx, value, String(state.initialRunId)); },
async cleanup(ctx, state) { if (typeof state.connectionId === "string") await ctx.input.api.delete(`/api/tool-connections/${state.connectionId}`, { allowNotFound: true }); },
})];