|
| 1 | +// Qa Lab plugin module defines scenario metadata for transport-backed QA runs. |
| 2 | +import type { |
| 3 | + LiveTransportScenarioDefinition, |
| 4 | + LiveTransportStandardScenarioId, |
| 5 | +} from "openclaw/plugin-sdk/qa-live-transport-scenarios"; |
| 6 | +import type { |
| 7 | + QaTransportActionName, |
| 8 | + QaTransportAdapter, |
| 9 | + QaTransportCapabilities, |
| 10 | +} from "./qa-transport.js"; |
| 11 | + |
| 12 | +export type QaTransportCapabilityName = keyof QaTransportCapabilities; |
| 13 | + |
| 14 | +export type QaTransportScenarioRequirements = { |
| 15 | + actions?: readonly QaTransportActionName[]; |
| 16 | + capabilities?: readonly QaTransportCapabilityName[]; |
| 17 | +}; |
| 18 | + |
| 19 | +export type QaTransportScenarioDefinition<TId extends string = string> = |
| 20 | + LiveTransportScenarioDefinition<TId> & { |
| 21 | + transportRequirements: QaTransportScenarioRequirements; |
| 22 | + }; |
| 23 | + |
| 24 | +export type QaTransportScenarioDefinitionInput<TId extends string = string> = |
| 25 | + LiveTransportScenarioDefinition<TId> & { |
| 26 | + transportRequirements?: QaTransportScenarioRequirements; |
| 27 | + }; |
| 28 | + |
| 29 | +export type QaTransportScenarioUnsupportedRequirements = { |
| 30 | + actions: QaTransportActionName[]; |
| 31 | + capabilities: QaTransportCapabilityName[]; |
| 32 | +}; |
| 33 | + |
| 34 | +const TEXT_REPLY_CAPABILITIES = [ |
| 35 | + "assertNoFailureReplies", |
| 36 | + "sendInboundMessage", |
| 37 | + "waitForOutboundMessage", |
| 38 | +] as const satisfies readonly QaTransportCapabilityName[]; |
| 39 | + |
| 40 | +const NO_REPLY_CAPABILITIES = [ |
| 41 | + "assertNoFailureReplies", |
| 42 | + "sendInboundMessage", |
| 43 | + "waitForCondition", |
| 44 | +] as const satisfies readonly QaTransportCapabilityName[]; |
| 45 | + |
| 46 | +const STANDARD_SCENARIO_REQUIREMENTS = { |
| 47 | + canary: { capabilities: TEXT_REPLY_CAPABILITIES }, |
| 48 | + "mention-gating": { capabilities: NO_REPLY_CAPABILITIES }, |
| 49 | + "allowlist-block": { capabilities: NO_REPLY_CAPABILITIES }, |
| 50 | + "top-level-reply-shape": { capabilities: TEXT_REPLY_CAPABILITIES }, |
| 51 | + "quote-reply": { capabilities: TEXT_REPLY_CAPABILITIES }, |
| 52 | + "restart-resume": { |
| 53 | + capabilities: [...TEXT_REPLY_CAPABILITIES, "waitForReady"], |
| 54 | + }, |
| 55 | + "thread-follow-up": { capabilities: TEXT_REPLY_CAPABILITIES }, |
| 56 | + "thread-isolation": { |
| 57 | + capabilities: [...TEXT_REPLY_CAPABILITIES, "waitForCondition"], |
| 58 | + }, |
| 59 | + "reaction-observation": { |
| 60 | + capabilities: ["getNormalizedMessageState", "waitForCondition"], |
| 61 | + }, |
| 62 | + "help-command": { capabilities: TEXT_REPLY_CAPABILITIES }, |
| 63 | +} as const satisfies Record<LiveTransportStandardScenarioId, QaTransportScenarioRequirements>; |
| 64 | + |
| 65 | +function uniqueInOrder<T extends string>(values: readonly T[]) { |
| 66 | + const seen = new Set<T>(); |
| 67 | + const unique: T[] = []; |
| 68 | + for (const value of values) { |
| 69 | + if (!seen.has(value)) { |
| 70 | + seen.add(value); |
| 71 | + unique.push(value); |
| 72 | + } |
| 73 | + } |
| 74 | + return unique; |
| 75 | +} |
| 76 | + |
| 77 | +export function mergeQaTransportScenarioRequirements( |
| 78 | + requirements: readonly QaTransportScenarioRequirements[], |
| 79 | +): QaTransportScenarioRequirements { |
| 80 | + return { |
| 81 | + actions: uniqueInOrder(requirements.flatMap((requirement) => requirement.actions ?? [])), |
| 82 | + capabilities: uniqueInOrder( |
| 83 | + requirements.flatMap((requirement) => requirement.capabilities ?? []), |
| 84 | + ), |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +export function qaTransportRequirementsForStandardScenario( |
| 89 | + standardId: LiveTransportStandardScenarioId, |
| 90 | +): QaTransportScenarioRequirements { |
| 91 | + return STANDARD_SCENARIO_REQUIREMENTS[standardId]; |
| 92 | +} |
| 93 | + |
| 94 | +export function defineQaTransportScenario<TId extends string>( |
| 95 | + input: QaTransportScenarioDefinitionInput<TId>, |
| 96 | +): QaTransportScenarioDefinition<TId> { |
| 97 | + const standardRequirements = input.standardId |
| 98 | + ? qaTransportRequirementsForStandardScenario(input.standardId) |
| 99 | + : {}; |
| 100 | + return { |
| 101 | + ...input, |
| 102 | + transportRequirements: mergeQaTransportScenarioRequirements([ |
| 103 | + standardRequirements, |
| 104 | + input.transportRequirements ?? {}, |
| 105 | + ]), |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +export function findUnsupportedQaTransportScenarioRequirements(params: { |
| 110 | + scenario: QaTransportScenarioDefinition; |
| 111 | + transport: Pick<QaTransportAdapter, "capabilities" | "supportedActions">; |
| 112 | +}): QaTransportScenarioUnsupportedRequirements { |
| 113 | + const supportedActions = new Set(params.transport.supportedActions); |
| 114 | + return { |
| 115 | + actions: (params.scenario.transportRequirements.actions ?? []).filter( |
| 116 | + (action) => !supportedActions.has(action), |
| 117 | + ), |
| 118 | + capabilities: (params.scenario.transportRequirements.capabilities ?? []).filter( |
| 119 | + (capability) => typeof params.transport.capabilities[capability] !== "function", |
| 120 | + ), |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +export function assertQaTransportSupportsScenario(params: { |
| 125 | + scenario: QaTransportScenarioDefinition; |
| 126 | + transport: Pick<QaTransportAdapter, "capabilities" | "id" | "supportedActions">; |
| 127 | +}) { |
| 128 | + const unsupported = findUnsupportedQaTransportScenarioRequirements(params); |
| 129 | + const problems = [ |
| 130 | + unsupported.capabilities.length > 0 |
| 131 | + ? `missing capabilities: ${unsupported.capabilities.join(", ")}` |
| 132 | + : undefined, |
| 133 | + unsupported.actions.length > 0 |
| 134 | + ? `unsupported actions: ${unsupported.actions.join(", ")}` |
| 135 | + : undefined, |
| 136 | + ].filter((problem): problem is string => Boolean(problem)); |
| 137 | + if (problems.length > 0) { |
| 138 | + throw new Error( |
| 139 | + `QA transport ${params.transport.id} cannot run scenario ${params.scenario.id}; ${problems.join("; ")}`, |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +export function filterQaTransportScenariosForTransport< |
| 145 | + TScenario extends QaTransportScenarioDefinition, |
| 146 | +>(params: { |
| 147 | + scenarios: readonly TScenario[]; |
| 148 | + transport: Pick<QaTransportAdapter, "capabilities" | "supportedActions">; |
| 149 | +}): TScenario[] { |
| 150 | + return params.scenarios.filter((scenario) => { |
| 151 | + const unsupported = findUnsupportedQaTransportScenarioRequirements({ |
| 152 | + scenario, |
| 153 | + transport: params.transport, |
| 154 | + }); |
| 155 | + return unsupported.actions.length === 0 && unsupported.capabilities.length === 0; |
| 156 | + }); |
| 157 | +} |
0 commit comments