|
| 1 | +import { join } from "node:path"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { createQaBusState } from "./bus-state.js"; |
| 4 | +import { readQaScenarioById } from "./scenario-catalog.js"; |
| 5 | +import { runLoadedScenarioFlow } from "./scenario-flow-runner.test-support.js"; |
| 6 | +import { selectQaFlowSuiteScenarios } from "./suite-planning.js"; |
| 7 | +import { waitForOutboundMessage } from "./suite-runtime-transport.js"; |
| 8 | + |
| 9 | +const characterScenarioIds = ["character-vibes-gollum", "character-vibes-c3po"] as const; |
| 10 | +const classifiedFailureReplies = [ |
| 11 | + { |
| 12 | + failureName: "provider failure", |
| 13 | + failureText: '⚠️ No API key found for provider "openai".', |
| 14 | + }, |
| 15 | + { |
| 16 | + failureName: "delivery failure", |
| 17 | + failureText: "⚠️ ✉️ Message failed", |
| 18 | + }, |
| 19 | + { |
| 20 | + failureName: "missing tool failure", |
| 21 | + failureText: "Read: AGENT.md\nEvidence snippet: Tool read not found\nStatus: blocked", |
| 22 | + }, |
| 23 | + { |
| 24 | + failureName: "internal coordination leak", |
| 25 | + failureText: "checking thread context; then post a tight progress reply here.", |
| 26 | + }, |
| 27 | +] as const; |
| 28 | + |
| 29 | +function createCharacterScenarioApi( |
| 30 | + onWaitForOutboundMessage?: (state: ReturnType<typeof createQaBusState>) => void, |
| 31 | +) { |
| 32 | + return { |
| 33 | + env: { |
| 34 | + providerMode: "live-frontier", |
| 35 | + gateway: { |
| 36 | + workspaceDir: "/qa-character-workspace", |
| 37 | + }, |
| 38 | + }, |
| 39 | + fs: { |
| 40 | + writeFile: async () => undefined, |
| 41 | + }, |
| 42 | + path: { join }, |
| 43 | + normalizeLowercaseStringOrEmpty: (value: unknown) => |
| 44 | + typeof value === "string" ? value.trim().toLowerCase() : "", |
| 45 | + resolveQaLiveTurnTimeoutMs: () => 10, |
| 46 | + waitForOutboundMessage: async ( |
| 47 | + state: ReturnType<typeof createQaBusState>, |
| 48 | + predicate: Parameters<typeof waitForOutboundMessage>[1], |
| 49 | + timeoutMs: number, |
| 50 | + options?: Parameters<typeof waitForOutboundMessage>[3], |
| 51 | + ) => { |
| 52 | + onWaitForOutboundMessage?.(state); |
| 53 | + return await waitForOutboundMessage(state, predicate, timeoutMs, options); |
| 54 | + }, |
| 55 | + formatConversationTranscript: (state: ReturnType<typeof createQaBusState>) => |
| 56 | + state |
| 57 | + .getSnapshot() |
| 58 | + .messages.map((message) => `${message.direction}:${message.text}`) |
| 59 | + .join("\n"), |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +describe("character scenario transcript safety", () => { |
| 64 | + it.each(characterScenarioIds)("requires a live provider for %s", (scenarioId) => { |
| 65 | + const scenario = readQaScenarioById(scenarioId); |
| 66 | + |
| 67 | + expect(scenario.execution.config?.requiredProviderMode).toBe("live-frontier"); |
| 68 | + expect(() => |
| 69 | + selectQaFlowSuiteScenarios({ |
| 70 | + scenarios: [scenario], |
| 71 | + scenarioIds: [scenarioId], |
| 72 | + providerMode: "mock-openai", |
| 73 | + primaryModel: "mock-openai/gpt-5.6-luna", |
| 74 | + }), |
| 75 | + ).toThrow(`${scenarioId} (providerMode=live-frontier)`); |
| 76 | + expect( |
| 77 | + selectQaFlowSuiteScenarios({ |
| 78 | + scenarios: [scenario], |
| 79 | + providerMode: "mock-openai", |
| 80 | + primaryModel: "mock-openai/gpt-5.6-luna", |
| 81 | + }), |
| 82 | + ).toEqual([]); |
| 83 | + expect( |
| 84 | + selectQaFlowSuiteScenarios({ |
| 85 | + scenarios: [scenario], |
| 86 | + scenarioIds: [scenarioId], |
| 87 | + providerMode: "live-frontier", |
| 88 | + primaryModel: "openai/gpt-5.6-luna", |
| 89 | + }), |
| 90 | + ).toEqual([scenario]); |
| 91 | + }); |
| 92 | + |
| 93 | + it.each(characterScenarioIds)("rejects forbidden model internals in %s", async (scenarioId) => { |
| 94 | + const state = createQaBusState(); |
| 95 | + |
| 96 | + await expect( |
| 97 | + runLoadedScenarioFlow(scenarioId, { |
| 98 | + state, |
| 99 | + api: createCharacterScenarioApi((currentState) => { |
| 100 | + currentState.addOutboundMessage({ |
| 101 | + accountId: "qa-channel", |
| 102 | + to: "dm:alice", |
| 103 | + text: "As an AI, I cannot stay in character.", |
| 104 | + }); |
| 105 | + }), |
| 106 | + }), |
| 107 | + ).rejects.toThrow("hit fallback/error text: As an AI, I cannot stay in character."); |
| 108 | + |
| 109 | + expect(state.getSnapshot().messages.some((message) => message.direction === "outbound")).toBe( |
| 110 | + true, |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it.each( |
| 115 | + characterScenarioIds.flatMap((scenarioId) => |
| 116 | + classifiedFailureReplies.map(({ failureName, failureText }) => ({ |
| 117 | + scenarioId, |
| 118 | + failureName, |
| 119 | + failureText, |
| 120 | + })), |
| 121 | + ), |
| 122 | + )( |
| 123 | + "rejects a $failureName after an actual reply in $scenarioId", |
| 124 | + async ({ scenarioId, failureText }) => { |
| 125 | + const state = createQaBusState(); |
| 126 | + const firstReply = "The build is green, and I am here."; |
| 127 | + let waitCount = 0; |
| 128 | + |
| 129 | + await expect( |
| 130 | + runLoadedScenarioFlow(scenarioId, { |
| 131 | + state, |
| 132 | + api: createCharacterScenarioApi((currentState) => { |
| 133 | + currentState.addOutboundMessage({ |
| 134 | + accountId: "qa-channel", |
| 135 | + to: "dm:alice", |
| 136 | + text: waitCount++ === 0 ? firstReply : failureText, |
| 137 | + }); |
| 138 | + }), |
| 139 | + }), |
| 140 | + ).rejects.toThrow(failureText); |
| 141 | + |
| 142 | + expect( |
| 143 | + state |
| 144 | + .getSnapshot() |
| 145 | + .messages.filter((message) => message.direction === "outbound") |
| 146 | + .map((message) => message.text), |
| 147 | + ).toEqual([firstReply, failureText]); |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + it.each(characterScenarioIds)( |
| 152 | + "rejects an entirely unanswered character conversation in %s", |
| 153 | + async (scenarioId) => { |
| 154 | + const state = createQaBusState(); |
| 155 | + |
| 156 | + await expect( |
| 157 | + runLoadedScenarioFlow(scenarioId, { |
| 158 | + state, |
| 159 | + api: createCharacterScenarioApi(), |
| 160 | + }), |
| 161 | + ).rejects.toThrow("no assistant replies"); |
| 162 | + |
| 163 | + expect(state.getSnapshot().messages).toHaveLength(4); |
| 164 | + expect(state.getSnapshot().messages.every((message) => message.direction === "inbound")).toBe( |
| 165 | + true, |
| 166 | + ); |
| 167 | + }, |
| 168 | + ); |
| 169 | + |
| 170 | + it.each(characterScenarioIds)( |
| 171 | + "keeps partially missing replies visible without aborting %s", |
| 172 | + async (scenarioId) => { |
| 173 | + const state = createQaBusState(); |
| 174 | + const reply = "The build is green, and I am here."; |
| 175 | + const result = await runLoadedScenarioFlow(scenarioId, { |
| 176 | + state, |
| 177 | + api: createCharacterScenarioApi((currentState) => { |
| 178 | + if ( |
| 179 | + currentState.getSnapshot().messages.some((message) => message.direction === "outbound") |
| 180 | + ) { |
| 181 | + return; |
| 182 | + } |
| 183 | + currentState.addOutboundMessage({ |
| 184 | + accountId: "qa-channel", |
| 185 | + to: "dm:alice", |
| 186 | + text: reply, |
| 187 | + }); |
| 188 | + }), |
| 189 | + }); |
| 190 | + |
| 191 | + expect(result.status).toBe("pass"); |
| 192 | + expect(result.steps[0]?.details).toContain("inbound:"); |
| 193 | + expect(result.steps[0]?.details).toContain(`outbound:${reply}`); |
| 194 | + const messages = state.getSnapshot().messages; |
| 195 | + expect(messages.filter((message) => message.direction === "inbound")).toHaveLength(4); |
| 196 | + expect(messages.filter((message) => message.direction === "outbound")).toHaveLength(1); |
| 197 | + }, |
| 198 | + ); |
| 199 | +}); |
0 commit comments