@fliwright/tdd 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -3
- package/dist/baseline/BaselineManager.d.ts +3 -3
- package/dist/daemon/FlutterDaemonController.d.ts +1 -0
- package/dist/daemon/FlutterDaemonController.js +13 -2
- package/dist/daemon/SubprocessDaemonTransport.d.ts +3 -0
- package/dist/daemon/SubprocessDaemonTransport.js +25 -9
- package/dist/executor/FocusedRerunRecipe.js +9 -2
- package/dist/executor/PersistentTestExecutor.d.ts +1 -4
- package/dist/executor/PersistentTestExecutor.js +31 -38
- package/dist/executor/TestRunResults.d.ts +13 -0
- package/dist/executor/TestRunResults.js +19 -0
- package/dist/index.d.ts +3 -17
- package/dist/index.js +1 -8
- package/dist/runtime/TddRuntime.d.ts +5 -19
- package/dist/runtime/TddRuntime.js +78 -115
- package/dist/types.d.ts +17 -192
- package/package.json +4 -4
- package/dist/devassist/DevAssistCoordinator.d.ts +0 -74
- package/dist/devassist/DevAssistCoordinator.js +0 -388
- package/dist/executor/ResultReporter.d.ts +0 -23
- package/dist/executor/ResultReporter.js +0 -50
- package/dist/generator/RedFirstTestGenerator.d.ts +0 -32
- package/dist/generator/RedFirstTestGenerator.js +0 -189
- package/dist/repair/TddRepairPlanner.d.ts +0 -54
- package/dist/repair/TddRepairPlanner.js +0 -132
- package/dist/selectors/SelectorSynthesizer.d.ts +0 -34
- package/dist/selectors/SelectorSynthesizer.js +0 -174
- package/dist/spec/InteractionSpec.d.ts +0 -94
- package/dist/spec/InteractionSpec.js +0 -236
- package/dist/spec/InteractionSpecCoverage.d.ts +0 -17
- package/dist/spec/InteractionSpecCoverage.js +0 -69
- package/dist/workflow/RedFirstWorkflow.d.ts +0 -28
- package/dist/workflow/RedFirstWorkflow.js +0 -33
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { parseInteractionSpec } from '../spec/InteractionSpec.js';
|
|
2
|
-
import { bestLocatorHint, synthesizeSelectorsForElements } from '../selectors/SelectorSynthesizer.js';
|
|
3
|
-
export function generateRedFirstTest(spec, options = {}) {
|
|
4
|
-
spec = parseInteractionSpec(spec);
|
|
5
|
-
const warnings = [];
|
|
6
|
-
const flow = selectFlow(spec, options.flowId);
|
|
7
|
-
if (!flow)
|
|
8
|
-
throw new Error('InteractionSpec must include at least one flow.');
|
|
9
|
-
const elements = new Map(spec.elements.map((element) => [element.id, element]));
|
|
10
|
-
const testName = options.testName ?? flow.name;
|
|
11
|
-
const resetToHomeBeforeEach = options.resetToHomeBeforeEach ?? true;
|
|
12
|
-
const homeRoute = options.homeRoute ?? spec.initialState?.route ?? spec.app?.route ?? '/';
|
|
13
|
-
const assertions = [...(flow.expectedOutcome ?? []), ...(spec.assertions ?? [])];
|
|
14
|
-
const needsMock = assertions.some((assertion) => assertion.kind === 'mockCalled');
|
|
15
|
-
const widgets = options.widgets ?? [];
|
|
16
|
-
const selectorDiagnostics = synthesizeSelectorsForElements(spec.elements, widgets);
|
|
17
|
-
const lines = [];
|
|
18
|
-
const imports = resetToHomeBeforeEach ? 'test, expect, beforeEach' : 'test, expect';
|
|
19
|
-
lines.push(`import { ${imports} } from '@fliwright/vitest';`);
|
|
20
|
-
lines.push('');
|
|
21
|
-
if (resetToHomeBeforeEach) {
|
|
22
|
-
lines.push('beforeEach(async ({ page }) => {');
|
|
23
|
-
lines.push(` await page.resetToHome({ homeRoute: ${stringLiteral(homeRoute)} });`);
|
|
24
|
-
lines.push('});');
|
|
25
|
-
lines.push('');
|
|
26
|
-
}
|
|
27
|
-
lines.push(`test(${stringLiteral(testName)}, async ({ ${needsMock ? 'page, mock' : 'page'} }) => {`);
|
|
28
|
-
for (const step of flow.steps)
|
|
29
|
-
appendStep(lines, step, elements, widgets, warnings);
|
|
30
|
-
for (const assertion of assertions)
|
|
31
|
-
appendAssertion(lines, assertion, elements, widgets, warnings);
|
|
32
|
-
if (flow.steps.length === 0 && assertions.length === 0) {
|
|
33
|
-
warnings.push(`Flow '${flow.id}' has no steps or assertions.`);
|
|
34
|
-
}
|
|
35
|
-
lines.push('});');
|
|
36
|
-
return {
|
|
37
|
-
testCode: lines.join('\n'),
|
|
38
|
-
testName,
|
|
39
|
-
flowId: flow.id,
|
|
40
|
-
warnings,
|
|
41
|
-
selectorDiagnostics,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
export function generateRedFirstTestSuite(spec, options = {}) {
|
|
45
|
-
spec = parseInteractionSpec(spec);
|
|
46
|
-
const warnings = [];
|
|
47
|
-
const flows = selectFlows(spec, options.flowIds);
|
|
48
|
-
const elements = new Map(spec.elements.map((element) => [element.id, element]));
|
|
49
|
-
const resetToHomeBeforeEach = options.resetToHomeBeforeEach ?? true;
|
|
50
|
-
const homeRoute = options.homeRoute ?? spec.initialState?.route ?? spec.app?.route ?? '/';
|
|
51
|
-
const widgets = options.widgets ?? [];
|
|
52
|
-
const selectorDiagnostics = synthesizeSelectorsForElements(spec.elements, widgets);
|
|
53
|
-
const lines = testFilePreamble({ resetToHomeBeforeEach, homeRoute });
|
|
54
|
-
const tests = [];
|
|
55
|
-
for (const flow of flows) {
|
|
56
|
-
const flowWarnings = [];
|
|
57
|
-
const testName = options.testNamePrefix ? `${options.testNamePrefix}: ${flow.name}` : flow.name;
|
|
58
|
-
appendTest(lines, flow, testName, elements, spec.assertions ?? [], widgets, flowWarnings);
|
|
59
|
-
tests.push({ testName, flowId: flow.id, warnings: flowWarnings });
|
|
60
|
-
warnings.push(...flowWarnings.map((warning) => `[${flow.id}] ${warning}`));
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
testCode: lines.join('\n'),
|
|
64
|
-
tests,
|
|
65
|
-
warnings,
|
|
66
|
-
selectorDiagnostics,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function selectFlow(spec, flowId) {
|
|
70
|
-
if (!flowId)
|
|
71
|
-
return spec.flows[0];
|
|
72
|
-
const flow = spec.flows.find((candidate) => candidate.id === flowId);
|
|
73
|
-
if (!flow)
|
|
74
|
-
throw new Error(`Flow '${flowId}' was not found in InteractionSpec.flows.`);
|
|
75
|
-
return flow;
|
|
76
|
-
}
|
|
77
|
-
function selectFlows(spec, flowIds) {
|
|
78
|
-
if (!flowIds?.length)
|
|
79
|
-
return spec.flows;
|
|
80
|
-
const flows = flowIds.map((flowId) => {
|
|
81
|
-
const flow = spec.flows.find((candidate) => candidate.id === flowId);
|
|
82
|
-
if (!flow)
|
|
83
|
-
throw new Error(`Flow '${flowId}' was not found in InteractionSpec.flows.`);
|
|
84
|
-
return flow;
|
|
85
|
-
});
|
|
86
|
-
return flows;
|
|
87
|
-
}
|
|
88
|
-
function testFilePreamble(opts) {
|
|
89
|
-
const lines = [];
|
|
90
|
-
const imports = opts.resetToHomeBeforeEach ? 'test, expect, beforeEach' : 'test, expect';
|
|
91
|
-
lines.push(`import { ${imports} } from '@fliwright/vitest';`);
|
|
92
|
-
lines.push('');
|
|
93
|
-
if (opts.resetToHomeBeforeEach) {
|
|
94
|
-
lines.push('beforeEach(async ({ page }) => {');
|
|
95
|
-
lines.push(` await page.resetToHome({ homeRoute: ${stringLiteral(opts.homeRoute)} });`);
|
|
96
|
-
lines.push('});');
|
|
97
|
-
lines.push('');
|
|
98
|
-
}
|
|
99
|
-
return lines;
|
|
100
|
-
}
|
|
101
|
-
function appendTest(lines, flow, testName, elements, globalAssertions, widgets, warnings) {
|
|
102
|
-
const assertions = [...(flow.expectedOutcome ?? []), ...globalAssertions];
|
|
103
|
-
const needsMock = assertions.some((assertion) => assertion.kind === 'mockCalled');
|
|
104
|
-
lines.push(`test(${stringLiteral(testName)}, async ({ ${needsMock ? 'page, mock' : 'page'} }) => {`);
|
|
105
|
-
for (const step of flow.steps)
|
|
106
|
-
appendStep(lines, step, elements, widgets, warnings);
|
|
107
|
-
for (const assertion of assertions)
|
|
108
|
-
appendAssertion(lines, assertion, elements, widgets, warnings);
|
|
109
|
-
if (flow.steps.length === 0 && assertions.length === 0) {
|
|
110
|
-
warnings.push(`Flow '${flow.id}' has no steps or assertions.`);
|
|
111
|
-
}
|
|
112
|
-
lines.push('});');
|
|
113
|
-
lines.push('');
|
|
114
|
-
}
|
|
115
|
-
function appendStep(lines, step, elements, widgets, warnings) {
|
|
116
|
-
const element = elements.get(step.target);
|
|
117
|
-
if (!element) {
|
|
118
|
-
warnings.push(`Step target '${step.target}' was not found in spec.elements.`);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
const locator = locatorExpression(element, widgets);
|
|
122
|
-
if (step.action === 'tap') {
|
|
123
|
-
lines.push(` await ${locator}.click();`);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
if (step.action === 'type') {
|
|
127
|
-
lines.push(` await ${locator}.fill(${stringLiteral(step.value)});`);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
if (step.action === 'select') {
|
|
131
|
-
lines.push(` await ${locator}.selectOption(${stringLiteral(step.value)});`);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (step.action === 'waitFor' || step.action === 'assertVisible') {
|
|
135
|
-
lines.push(` await expect(${locator}).toBeVisible();`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
function appendAssertion(lines, assertion, elements, widgets, warnings) {
|
|
139
|
-
if (assertion.kind === 'mockCalled') {
|
|
140
|
-
const matcher = assertion.method
|
|
141
|
-
? `{ path: ${stringLiteral(assertion.endpoint)}, method: ${stringLiteral(assertion.method)} }`
|
|
142
|
-
: stringLiteral(assertion.endpoint);
|
|
143
|
-
lines.push(` await mock.waitForCall(${matcher});`);
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
if (assertion.kind === 'route') {
|
|
147
|
-
lines.push(` await expect(await page.currentRoute()).toBe(${stringLiteral(assertion.equals)});`);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if (assertion.kind === 'state') {
|
|
151
|
-
warnings.push(`State assertion '${assertion.path}' requires a custom state adapter and was not emitted.`);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const element = elements.get(assertion.target);
|
|
155
|
-
if (!element) {
|
|
156
|
-
warnings.push(`Assertion target '${assertion.target}' was not found in spec.elements.`);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
const locator = locatorExpression(element, widgets);
|
|
160
|
-
if (assertion.kind === 'visible') {
|
|
161
|
-
lines.push(` await expect(${locator}).toBeVisible();`);
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
if (assertion.equals !== undefined) {
|
|
165
|
-
lines.push(` await expect(${locator}).toHaveText(${stringLiteral(assertion.equals)});`);
|
|
166
|
-
}
|
|
167
|
-
else if (assertion.contains !== undefined) {
|
|
168
|
-
lines.push(` await expect(${locator}).toContainText(${stringLiteral(assertion.contains)});`);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
lines.push(` await expect(${locator}).toBeVisible();`);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
function locatorExpression(element, widgets) {
|
|
175
|
-
const hint = bestLocatorHint(element, widgets);
|
|
176
|
-
return `page.locator(${locatorInputExpression(hint)})`;
|
|
177
|
-
}
|
|
178
|
-
function locatorInputExpression(hint) {
|
|
179
|
-
if (hint.strategy === 'key')
|
|
180
|
-
return `{ key: ${stringLiteral(hint.value)} }`;
|
|
181
|
-
if (hint.strategy === 'semantics')
|
|
182
|
-
return `{ semantics: { label: ${stringLiteral(hint.value)} } }`;
|
|
183
|
-
if (hint.strategy === 'type')
|
|
184
|
-
return `{ type: ${stringLiteral(hint.value)} }`;
|
|
185
|
-
return `{ text: ${stringLiteral(hint.value)} }`;
|
|
186
|
-
}
|
|
187
|
-
function stringLiteral(value) {
|
|
188
|
-
return JSON.stringify(value);
|
|
189
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { AgentRepair, type RepairProposal, type RepairResult } from '@fliwright/core';
|
|
2
|
-
import type { Page } from '@fliwright/core';
|
|
3
|
-
import type { TddCycleResult, TddRepairPlan, TddRepairPlannerLike } from '../types.js';
|
|
4
|
-
import { type TddFailureContext } from '../diagnostics/TddFailureContext.js';
|
|
5
|
-
export interface TddRepairPlannerOptions {
|
|
6
|
-
/**
|
|
7
|
-
* The `AgentRepair` instance backing the guardrail + safe-apply execution. In production this is
|
|
8
|
-
* constructed with the runtime-owned `Page` (so click/dismissModal/wait act on the live app). The
|
|
9
|
-
* guardrail (`validate`) is authoritative: nothing reaches the app unless it passes.
|
|
10
|
-
*/
|
|
11
|
-
repair: AgentRepair;
|
|
12
|
-
/**
|
|
13
|
-
* Optional override of the candidate-proposal synthesizer. Tests inject a deterministic one; the
|
|
14
|
-
* default maps failure kinds to bounded runtime actions (see {@link synthesizeCandidateProposal}).
|
|
15
|
-
*/
|
|
16
|
-
synthesize?: (failure: TddFailureContext | undefined) => RepairProposal | null;
|
|
17
|
-
/**
|
|
18
|
-
* Stable step id passed to `AgentRepair.execute` so its per-step retry counter is meaningful across
|
|
19
|
-
* loop iterations. Defaults to 'tdd-cycle'.
|
|
20
|
-
*/
|
|
21
|
-
stepId?: string;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Proposes a minimal, guardrail-bounded repair for a red TDD cycle, using `@fliwright/core`'s
|
|
25
|
-
* {@link AgentRepair} as the safety authority. Two modes (design §7 P3):
|
|
26
|
-
*
|
|
27
|
-
* - `'suggest'` returns the plan (diff + per-proposal guardrail verdicts) and applies nothing. The
|
|
28
|
-
* caller (an agent) reviews the diff and decides.
|
|
29
|
-
* - `'safe-apply'` applies only the proposals the AgentRepair `validate()` guard accepts (bounded
|
|
30
|
-
* runtime actions: click/wait/dismissModal/retryStep/observe) and returns their execute results.
|
|
31
|
-
* Source-mutating `codePatch` and unknown kinds are always rejected by the guardrail — the loop
|
|
32
|
-
* can never apply an unbounded edit.
|
|
33
|
-
*
|
|
34
|
-
* The planner never bypasses the guardrail: `validate()` is the single chokepoint. This keeps the
|
|
35
|
-
* closed loop safe even if a future synthesizer is LLM-driven.
|
|
36
|
-
*/
|
|
37
|
-
export declare class TddRepairPlanner implements TddRepairPlannerLike {
|
|
38
|
-
private readonly repair;
|
|
39
|
-
private readonly synthesize;
|
|
40
|
-
private readonly stepId;
|
|
41
|
-
constructor(options: TddRepairPlannerOptions);
|
|
42
|
-
/**
|
|
43
|
-
* Convenience factory: builds an `AgentRepair` over the given page (the same page the runtime's
|
|
44
|
-
* baseline reset uses) and wraps it. Pass the runtime-owned `Page` so safe-apply repairs act on
|
|
45
|
-
* the live app the loop is driving.
|
|
46
|
-
*/
|
|
47
|
-
static forPage(page: Page, options?: {
|
|
48
|
-
maxRetriesPerStep?: number;
|
|
49
|
-
synthesize?: TddRepairPlannerOptions['synthesize'];
|
|
50
|
-
}): TddRepairPlanner;
|
|
51
|
-
propose(result: TddCycleResult, mode: 'suggest' | 'safe-apply'): Promise<TddRepairPlan & {
|
|
52
|
-
applied?: RepairResult[];
|
|
53
|
-
}>;
|
|
54
|
-
}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { AgentRepair } from '@fliwright/core';
|
|
2
|
-
import { buildTddFailureContext } from '../diagnostics/TddFailureContext.js';
|
|
3
|
-
/**
|
|
4
|
-
* Maps a red TDD cycle's failure context to a *candidate* minimal `AgentRepair` proposal. This is
|
|
5
|
-
* deliberately heuristic and bounded: it only ever emits one of the runtime-action kinds the
|
|
6
|
-
* AgentRepair guardrail already understands (click / wait / dismissModal / retryStep / observe), so
|
|
7
|
-
* the guardrail can accept or reject each one deterministically. It never emits `codePatch` (the one
|
|
8
|
-
* kind `AgentRepair.validate` always rejects as "cannot mutate source code") — keeping repairs
|
|
9
|
-
* strictly runtime-side is the guardrail's invariant.
|
|
10
|
-
*
|
|
11
|
-
* The synthesizer is intentionally dumb on purpose: a real P3+ iteration would call an LLM here, but
|
|
12
|
-
* the closed loop's safety does NOT depend on the synthesizer being smart — it depends on every
|
|
13
|
-
* proposed action passing `AgentRepair.validate()` before it touches the app. An LLM-driven
|
|
14
|
-
* synthesizer that emitted a `codePatch` or an unknown kind would simply be rejected, exactly like
|
|
15
|
-
* the unit-tested unsafe-proposal case.
|
|
16
|
-
*/
|
|
17
|
-
function synthesizeCandidateProposal(failure) {
|
|
18
|
-
if (!failure)
|
|
19
|
-
return null;
|
|
20
|
-
switch (failure.kind) {
|
|
21
|
-
case 'missing-element':
|
|
22
|
-
case 'ambiguous-element':
|
|
23
|
-
// A common cause: a modal/overlay is in the way, or the element needs a beat to settle.
|
|
24
|
-
return { kind: 'dismissModal' };
|
|
25
|
-
case 'navigation-failed':
|
|
26
|
-
return { kind: 'wait', ms: 250 };
|
|
27
|
-
case 'timeout':
|
|
28
|
-
// A retry is the safe, bounded response; the guardrail caps retries per step.
|
|
29
|
-
return { kind: 'retryStep' };
|
|
30
|
-
case 'mock-not-called':
|
|
31
|
-
case 'state-mismatch':
|
|
32
|
-
case 'wrong-text':
|
|
33
|
-
case 'test-error':
|
|
34
|
-
case 'disconnected':
|
|
35
|
-
return null;
|
|
36
|
-
default:
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Builds an agent-readable diff/patch string describing the proposed minimal fix. This is what
|
|
42
|
-
* `suggest` mode returns for approval and what `safe-apply` records per iteration so the agent can
|
|
43
|
-
* audit exactly which bounded runtime actions were applied.
|
|
44
|
-
*/
|
|
45
|
-
function describePlan(failure, entries, appliedResults) {
|
|
46
|
-
const header = failure
|
|
47
|
-
? `# TDD repair proposal\n## Failure\n- kind: ${failure.kind}\n- message: ${failure.message}\n- test: ${failure.testFile}${failure.testName ? ` :: ${failure.testName}` : ''}\n`
|
|
48
|
-
: `# TDD repair proposal\n## Failure\n- (no structured failure context)\n`;
|
|
49
|
-
const lines = [header, '## Proposed actions'];
|
|
50
|
-
if (entries.length === 0) {
|
|
51
|
-
lines.push('- (no safe runtime action could be synthesized for this failure)');
|
|
52
|
-
}
|
|
53
|
-
for (const entry of entries) {
|
|
54
|
-
const verdict = entry.safe ? 'SAFE' : `REJECTED${entry.reason ? ` (${entry.reason})` : ''}`;
|
|
55
|
-
lines.push(`- [${verdict}] ${JSON.stringify(entry.proposal)}`);
|
|
56
|
-
}
|
|
57
|
-
if (appliedResults && appliedResults.length > 0) {
|
|
58
|
-
lines.push('', '## Applied');
|
|
59
|
-
for (const r of appliedResults) {
|
|
60
|
-
lines.push(`- ${r.accepted ? 'accepted' : 'rejected'}: ${JSON.stringify(r.action)}${r.reason ? ` — ${r.reason}` : ''}`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return lines.join('\n');
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Proposes a minimal, guardrail-bounded repair for a red TDD cycle, using `@fliwright/core`'s
|
|
67
|
-
* {@link AgentRepair} as the safety authority. Two modes (design §7 P3):
|
|
68
|
-
*
|
|
69
|
-
* - `'suggest'` returns the plan (diff + per-proposal guardrail verdicts) and applies nothing. The
|
|
70
|
-
* caller (an agent) reviews the diff and decides.
|
|
71
|
-
* - `'safe-apply'` applies only the proposals the AgentRepair `validate()` guard accepts (bounded
|
|
72
|
-
* runtime actions: click/wait/dismissModal/retryStep/observe) and returns their execute results.
|
|
73
|
-
* Source-mutating `codePatch` and unknown kinds are always rejected by the guardrail — the loop
|
|
74
|
-
* can never apply an unbounded edit.
|
|
75
|
-
*
|
|
76
|
-
* The planner never bypasses the guardrail: `validate()` is the single chokepoint. This keeps the
|
|
77
|
-
* closed loop safe even if a future synthesizer is LLM-driven.
|
|
78
|
-
*/
|
|
79
|
-
export class TddRepairPlanner {
|
|
80
|
-
repair;
|
|
81
|
-
synthesize;
|
|
82
|
-
stepId;
|
|
83
|
-
constructor(options) {
|
|
84
|
-
this.repair = options.repair;
|
|
85
|
-
this.synthesize = options.synthesize ?? synthesizeCandidateProposal;
|
|
86
|
-
this.stepId = options.stepId ?? 'tdd-cycle';
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Convenience factory: builds an `AgentRepair` over the given page (the same page the runtime's
|
|
90
|
-
* baseline reset uses) and wraps it. Pass the runtime-owned `Page` so safe-apply repairs act on
|
|
91
|
-
* the live app the loop is driving.
|
|
92
|
-
*/
|
|
93
|
-
static forPage(page, options) {
|
|
94
|
-
return new TddRepairPlanner({
|
|
95
|
-
repair: new AgentRepair({ page, maxRetriesPerStep: options?.maxRetriesPerStep }),
|
|
96
|
-
synthesize: options?.synthesize,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
async propose(result, mode) {
|
|
100
|
-
const failure = result.failureContext ?? toFallbackFailure(result);
|
|
101
|
-
const candidate = this.synthesize(failure);
|
|
102
|
-
const proposals = [];
|
|
103
|
-
if (candidate) {
|
|
104
|
-
// The guardrail is authoritative: validate() returns null when the proposal is a bounded,
|
|
105
|
-
// supported runtime action. A codePatch / unknown kind is rejected here regardless of source.
|
|
106
|
-
const reason = this.repair.validate(candidate, this.stepId);
|
|
107
|
-
proposals.push({ proposal: candidate, safe: reason === null, reason: reason ?? undefined });
|
|
108
|
-
}
|
|
109
|
-
let applied;
|
|
110
|
-
if (mode === 'safe-apply') {
|
|
111
|
-
applied = [];
|
|
112
|
-
for (const entry of proposals) {
|
|
113
|
-
if (!entry.safe)
|
|
114
|
-
continue;
|
|
115
|
-
// execute() re-validates and then applies via the page; capture its verdict for the trace.
|
|
116
|
-
applied.push(await this.repair.execute(entry.proposal, this.stepId));
|
|
117
|
-
}
|
|
118
|
-
if (applied.length === 0)
|
|
119
|
-
applied = undefined;
|
|
120
|
-
}
|
|
121
|
-
const diff = describePlan(failure, proposals, applied);
|
|
122
|
-
return { mode, diff, proposals, applied };
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
/** Builds a minimal failure context when a red result lacked a structured one (defensive). */
|
|
126
|
-
function toFallbackFailure(result) {
|
|
127
|
-
return buildTddFailureContext({
|
|
128
|
-
file: result.file,
|
|
129
|
-
testName: result.testName,
|
|
130
|
-
message: result.failure?.message ?? 'Focused TDD test failed.',
|
|
131
|
-
});
|
|
132
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { LocatorHint, SpecElement } from '../spec/InteractionSpec.js';
|
|
2
|
-
export type SelectorSynthesisStatus = 'resolved' | 'ambiguous' | 'missing' | 'hint-only';
|
|
3
|
-
export interface WidgetCandidate {
|
|
4
|
-
key?: string;
|
|
5
|
-
text?: string;
|
|
6
|
-
type?: string;
|
|
7
|
-
semanticsLabel?: string;
|
|
8
|
-
role?: string;
|
|
9
|
-
}
|
|
10
|
-
export interface SelectorCandidate {
|
|
11
|
-
hint: LocatorHint;
|
|
12
|
-
confidence: number;
|
|
13
|
-
reason: string;
|
|
14
|
-
matchedWidgets?: WidgetCandidate[];
|
|
15
|
-
}
|
|
16
|
-
export interface SelectorTraceStep {
|
|
17
|
-
strategy: LocatorHint['strategy'];
|
|
18
|
-
status: 'selected' | 'candidate' | 'rejected' | 'missing';
|
|
19
|
-
value: string;
|
|
20
|
-
message: string;
|
|
21
|
-
}
|
|
22
|
-
export interface SelectorSynthesisResult {
|
|
23
|
-
elementId: string;
|
|
24
|
-
status: SelectorSynthesisStatus;
|
|
25
|
-
candidates: SelectorCandidate[];
|
|
26
|
-
trace: SelectorTraceStep[];
|
|
27
|
-
stabilityHints: Array<{
|
|
28
|
-
kind: 'add-key' | 'add-semantics' | 'refine-copy' | 'stale-hint';
|
|
29
|
-
description: string;
|
|
30
|
-
}>;
|
|
31
|
-
}
|
|
32
|
-
export declare function synthesizeSelector(element: SpecElement, widgets?: WidgetCandidate[]): SelectorSynthesisResult;
|
|
33
|
-
export declare function synthesizeSelectorsForElements(elements: SpecElement[], widgets?: WidgetCandidate[]): SelectorSynthesisResult[];
|
|
34
|
-
export declare function bestLocatorHint(element: SpecElement, widgets?: WidgetCandidate[]): LocatorHint;
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
export function synthesizeSelector(element, widgets = []) {
|
|
2
|
-
const trace = [];
|
|
3
|
-
const candidates = candidateHints(element, widgets)
|
|
4
|
-
.map((hint) => scoreHint(hint, widgets, trace))
|
|
5
|
-
.sort((a, b) => b.confidence - a.confidence);
|
|
6
|
-
const selected = candidates[0];
|
|
7
|
-
const status = statusFor(selected, widgets);
|
|
8
|
-
if (selected) {
|
|
9
|
-
trace.push({
|
|
10
|
-
strategy: selected.hint.strategy,
|
|
11
|
-
status: 'selected',
|
|
12
|
-
value: selected.hint.value,
|
|
13
|
-
message: `Selected ${selected.hint.strategy} locator with confidence ${selected.confidence.toFixed(2)}.`,
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return {
|
|
17
|
-
elementId: element.id,
|
|
18
|
-
status,
|
|
19
|
-
candidates,
|
|
20
|
-
trace,
|
|
21
|
-
stabilityHints: stabilityHintsFor(element, selected, status),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
export function synthesizeSelectorsForElements(elements, widgets = []) {
|
|
25
|
-
return elements.map((element) => synthesizeSelector(element, widgets));
|
|
26
|
-
}
|
|
27
|
-
export function bestLocatorHint(element, widgets = []) {
|
|
28
|
-
return synthesizeSelector(element, widgets).candidates[0]?.hint ?? {
|
|
29
|
-
strategy: 'text',
|
|
30
|
-
value: element.text ?? element.placeholder ?? element.name,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
function candidateHints(element, widgets) {
|
|
34
|
-
const hints = [...(element.locatorHints ?? [])];
|
|
35
|
-
hints.push(...stableWidgetHints(element, widgets));
|
|
36
|
-
if (element.text)
|
|
37
|
-
hints.push({ strategy: 'text', value: element.text });
|
|
38
|
-
if (element.placeholder)
|
|
39
|
-
hints.push({ strategy: 'semantics', value: element.placeholder });
|
|
40
|
-
if (element.name && !hints.some((hint) => hint.value === element.name)) {
|
|
41
|
-
hints.push({ strategy: 'text', value: element.name });
|
|
42
|
-
}
|
|
43
|
-
return uniqueHints(hints);
|
|
44
|
-
}
|
|
45
|
-
function uniqueHints(hints) {
|
|
46
|
-
const seen = new Set();
|
|
47
|
-
return hints.filter((hint) => {
|
|
48
|
-
const key = `${hint.strategy}:${hint.value}`;
|
|
49
|
-
if (seen.has(key))
|
|
50
|
-
return false;
|
|
51
|
-
seen.add(key);
|
|
52
|
-
return true;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function stableWidgetHints(element, widgets) {
|
|
56
|
-
if (widgets.length === 0)
|
|
57
|
-
return [];
|
|
58
|
-
const matches = widgets.filter((widget) => widgetLooksLikeElement(widget, element));
|
|
59
|
-
if (matches.length !== 1)
|
|
60
|
-
return [];
|
|
61
|
-
const match = matches[0];
|
|
62
|
-
const hints = [];
|
|
63
|
-
if (match.key)
|
|
64
|
-
hints.push({ strategy: 'key', value: match.key });
|
|
65
|
-
if (match.semanticsLabel)
|
|
66
|
-
hints.push({ strategy: 'semantics', value: match.semanticsLabel });
|
|
67
|
-
return hints;
|
|
68
|
-
}
|
|
69
|
-
function widgetLooksLikeElement(widget, element) {
|
|
70
|
-
const roleMatches = !widget.role || widget.role === element.role;
|
|
71
|
-
if (!roleMatches)
|
|
72
|
-
return false;
|
|
73
|
-
const elementLabels = [element.text, element.placeholder, element.name]
|
|
74
|
-
.filter((value) => Boolean(value));
|
|
75
|
-
const widgetLabels = [widget.text, widget.semanticsLabel]
|
|
76
|
-
.filter((value) => Boolean(value));
|
|
77
|
-
return elementLabels.some((label) => widgetLabels.includes(label));
|
|
78
|
-
}
|
|
79
|
-
function scoreHint(hint, widgets, trace) {
|
|
80
|
-
const matchedWidgets = widgets.length > 0 ? widgets.filter((widget) => widgetMatchesHint(widget, hint)) : undefined;
|
|
81
|
-
const base = baseConfidence(hint.strategy);
|
|
82
|
-
const confidence = matchedWidgets
|
|
83
|
-
? confidenceFromMatches(base, matchedWidgets.length)
|
|
84
|
-
: base;
|
|
85
|
-
trace.push({
|
|
86
|
-
strategy: hint.strategy,
|
|
87
|
-
status: matchedWidgets && matchedWidgets.length === 0 ? 'missing' : 'candidate',
|
|
88
|
-
value: hint.value,
|
|
89
|
-
message: matchedWidgets
|
|
90
|
-
? `${matchedWidgets.length} widget candidate(s) matched ${hint.strategy}='${hint.value}'.`
|
|
91
|
-
: `No widget snapshot provided; using ${hint.strategy}='${hint.value}' as a generated hint.`,
|
|
92
|
-
});
|
|
93
|
-
return {
|
|
94
|
-
hint,
|
|
95
|
-
confidence,
|
|
96
|
-
reason: reasonFor(hint, matchedWidgets),
|
|
97
|
-
matchedWidgets,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function confidenceFromMatches(base, matchCount) {
|
|
101
|
-
if (matchCount === 1)
|
|
102
|
-
return Math.min(0.99, base + 0.08);
|
|
103
|
-
if (matchCount === 0)
|
|
104
|
-
return 0.1;
|
|
105
|
-
return Math.max(0.2, base - matchCount * 0.12);
|
|
106
|
-
}
|
|
107
|
-
function widgetMatchesHint(widget, hint) {
|
|
108
|
-
if (hint.strategy === 'key')
|
|
109
|
-
return widget.key === hint.value;
|
|
110
|
-
if (hint.strategy === 'semantics')
|
|
111
|
-
return widget.semanticsLabel === hint.value;
|
|
112
|
-
if (hint.strategy === 'type')
|
|
113
|
-
return widget.type === hint.value;
|
|
114
|
-
return widget.text === hint.value;
|
|
115
|
-
}
|
|
116
|
-
function baseConfidence(strategy) {
|
|
117
|
-
if (strategy === 'key')
|
|
118
|
-
return 0.95;
|
|
119
|
-
if (strategy === 'semantics')
|
|
120
|
-
return 0.88;
|
|
121
|
-
if (strategy === 'text')
|
|
122
|
-
return 0.72;
|
|
123
|
-
return 0.45;
|
|
124
|
-
}
|
|
125
|
-
function reasonFor(hint, matchedWidgets) {
|
|
126
|
-
if (!matchedWidgets)
|
|
127
|
-
return `${hint.strategy} hint is available from the interaction spec.`;
|
|
128
|
-
if (matchedWidgets.length === 1)
|
|
129
|
-
return `${hint.strategy} matches exactly one widget candidate.`;
|
|
130
|
-
if (matchedWidgets.length > 1)
|
|
131
|
-
return `${hint.strategy} matches multiple widget candidates.`;
|
|
132
|
-
return `${hint.strategy} did not match the provided widget candidates.`;
|
|
133
|
-
}
|
|
134
|
-
function statusFor(candidate, widgets) {
|
|
135
|
-
if (!candidate)
|
|
136
|
-
return 'missing';
|
|
137
|
-
if (!candidate.matchedWidgets)
|
|
138
|
-
return 'hint-only';
|
|
139
|
-
if (candidate.matchedWidgets.length === 1)
|
|
140
|
-
return 'resolved';
|
|
141
|
-
if (candidate.matchedWidgets.length > 1)
|
|
142
|
-
return 'ambiguous';
|
|
143
|
-
return widgets.length > 0 ? 'missing' : 'hint-only';
|
|
144
|
-
}
|
|
145
|
-
function stabilityHintsFor(element, selected, status) {
|
|
146
|
-
const hints = [];
|
|
147
|
-
const hasStableHint = element.locatorHints?.some((hint) => hint.strategy === 'key' || hint.strategy === 'semantics') ?? false;
|
|
148
|
-
const selectedStableHint = selected?.hint.strategy === 'key' || selected?.hint.strategy === 'semantics';
|
|
149
|
-
const staleStableHints = (element.locatorHints ?? []).filter((hint) => ((hint.strategy === 'key' || hint.strategy === 'semantics')
|
|
150
|
-
&& selected?.hint.value !== hint.value));
|
|
151
|
-
for (const hint of staleStableHints) {
|
|
152
|
-
hints.push({
|
|
153
|
-
kind: 'stale-hint',
|
|
154
|
-
description: `Stable ${hint.strategy} locator '${hint.value}' did not match the current widget snapshot for '${element.name}'.`,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
if (!hasStableHint && !selectedStableHint) {
|
|
158
|
-
hints.push({
|
|
159
|
-
kind: 'add-key',
|
|
160
|
-
description: `Add a stable Key for '${element.name}' so generated TDD tests do not depend on visible copy.`,
|
|
161
|
-
});
|
|
162
|
-
hints.push({
|
|
163
|
-
kind: 'add-semantics',
|
|
164
|
-
description: `Add a semantics label for '${element.name}' if this widget is user-facing or accessibility-relevant.`,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
if (status === 'ambiguous') {
|
|
168
|
-
hints.push({
|
|
169
|
-
kind: 'refine-copy',
|
|
170
|
-
description: `The locator '${selected?.hint.value ?? element.name}' matches multiple widgets; refine copy or add a unique key.`,
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
return hints;
|
|
174
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
export type SpecElementRole = 'button' | 'textbox' | 'text' | 'list' | 'image' | 'checkbox' | 'switch' | 'tab';
|
|
2
|
-
export interface LocatorHint {
|
|
3
|
-
strategy: 'key' | 'semantics' | 'text' | 'type';
|
|
4
|
-
value: string;
|
|
5
|
-
}
|
|
6
|
-
export interface SpecElement {
|
|
7
|
-
id: string;
|
|
8
|
-
role: SpecElementRole;
|
|
9
|
-
name: string;
|
|
10
|
-
text?: string;
|
|
11
|
-
placeholder?: string;
|
|
12
|
-
figmaNodeId?: string;
|
|
13
|
-
importance?: 'required' | 'optional';
|
|
14
|
-
locatorHints?: LocatorHint[];
|
|
15
|
-
}
|
|
16
|
-
export type SpecStep = {
|
|
17
|
-
action: 'tap';
|
|
18
|
-
target: string;
|
|
19
|
-
} | {
|
|
20
|
-
action: 'type';
|
|
21
|
-
target: string;
|
|
22
|
-
value: string;
|
|
23
|
-
} | {
|
|
24
|
-
action: 'select';
|
|
25
|
-
target: string;
|
|
26
|
-
value: string;
|
|
27
|
-
} | {
|
|
28
|
-
action: 'waitFor';
|
|
29
|
-
target: string;
|
|
30
|
-
} | {
|
|
31
|
-
action: 'assertVisible';
|
|
32
|
-
target: string;
|
|
33
|
-
};
|
|
34
|
-
export type SpecAssertion = {
|
|
35
|
-
kind: 'visible';
|
|
36
|
-
target: string;
|
|
37
|
-
} | {
|
|
38
|
-
kind: 'text';
|
|
39
|
-
target: string;
|
|
40
|
-
equals?: string;
|
|
41
|
-
contains?: string;
|
|
42
|
-
} | {
|
|
43
|
-
kind: 'route';
|
|
44
|
-
equals: string;
|
|
45
|
-
} | {
|
|
46
|
-
kind: 'mockCalled';
|
|
47
|
-
endpoint: string;
|
|
48
|
-
method?: string;
|
|
49
|
-
} | {
|
|
50
|
-
kind: 'state';
|
|
51
|
-
path: string;
|
|
52
|
-
equals: unknown;
|
|
53
|
-
};
|
|
54
|
-
export interface SpecFlow {
|
|
55
|
-
id: string;
|
|
56
|
-
name: string;
|
|
57
|
-
steps: SpecStep[];
|
|
58
|
-
expectedOutcome?: SpecAssertion[];
|
|
59
|
-
}
|
|
60
|
-
export interface InteractionSpec {
|
|
61
|
-
app?: {
|
|
62
|
-
platform?: 'flutter';
|
|
63
|
-
route?: string;
|
|
64
|
-
screenName?: string;
|
|
65
|
-
};
|
|
66
|
-
initialState?: {
|
|
67
|
-
route?: string;
|
|
68
|
-
mockProfile?: string;
|
|
69
|
-
riverpodOverrides?: unknown[];
|
|
70
|
-
storageSeed?: Record<string, unknown>;
|
|
71
|
-
authState?: 'guest' | 'authenticated';
|
|
72
|
-
};
|
|
73
|
-
elements: SpecElement[];
|
|
74
|
-
flows: SpecFlow[];
|
|
75
|
-
assertions?: SpecAssertion[];
|
|
76
|
-
}
|
|
77
|
-
export interface InteractionSpecValidationIssue {
|
|
78
|
-
path: string;
|
|
79
|
-
message: string;
|
|
80
|
-
}
|
|
81
|
-
export type InteractionSpecValidationResult = {
|
|
82
|
-
ok: true;
|
|
83
|
-
spec: InteractionSpec;
|
|
84
|
-
issues: [];
|
|
85
|
-
} | {
|
|
86
|
-
ok: false;
|
|
87
|
-
issues: InteractionSpecValidationIssue[];
|
|
88
|
-
};
|
|
89
|
-
export declare class InteractionSpecValidationError extends Error {
|
|
90
|
-
readonly issues: InteractionSpecValidationIssue[];
|
|
91
|
-
constructor(issues: InteractionSpecValidationIssue[]);
|
|
92
|
-
}
|
|
93
|
-
export declare function parseInteractionSpec(value: unknown): InteractionSpec;
|
|
94
|
-
export declare function validateInteractionSpec(value: unknown): InteractionSpecValidationResult;
|