Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,22 +589,7 @@ valid but bigger than that PR's rename-only remit, so they're recorded here:
complexity-only refactor (which had to preserve behavior). All of these are
pre-existing behaviors carried over unchanged from `main`, not regressions.*

### 1. `skipTypeParams` should not treat the `>` in `=>` as a closing angle bracket

`test/lib/code-quality/detectors.ts` — `skipTypeParams` counts every `>` as a
type-parameter close, so a type alias whose params contain an arrow default,
e.g. `type A<T = () => void> = { ... }`, is parsed as ending at the arrow and
`parseTypeAliasBody` silently ignores the (valid) alias. The sibling helper
`angleDepthDelta` already handles this token correctly (it ignores a `>`
preceded by `=`).

Fix direction: reuse `angleDepthDelta` in `skipTypeParams`. Note the naive
rewrite reintroduces an unreachable `return i` fall-through that fails the
repo's 100% line/branch coverage gate — so the fix must be paired with a
covering test that exercises an arrow-in-type-param alias (and keep the
fall-through on the covered path, as the current loop-with-`break` form does).

### 2. `skipTemplateSubstitution` should skip comment contents
### 1. `skipTemplateSubstitution` should skip comment contents

`test/lib/code-quality/detectors.ts` — the template-substitution scanner tracks
brace depth but does not skip comments, so a `}` inside a comment inside a
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"noExcessiveCognitiveComplexity": {
"level": "warn",
"options": {
"maxAllowedComplexity": 15
"maxAllowedComplexity": 13
}
},
"noForEach": "error",
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test:raw": "deno task build:static && deno test --no-check --allow-net --allow-env --allow-read --allow-write --allow-run --allow-sys --allow-ffi test/",
"lint": "deno run -A scripts/biome.ts check --write --error-on-warnings src test scripts cli e2e-payments",
"lint:ci": "deno run -A scripts/biome.ts check --error-on-warnings src test scripts cli e2e-payments",
"typecheck": "deno check src/**/*.ts src/**/*.tsx test/**/*.ts cli/**/*.ts scripts/database-snapshot.ts scripts/mutation.ts scripts/mutation/**/*.ts scripts/precommit.ts scripts/precommit/**/*.ts scripts/check-copy.ts scripts/check-copy/**/*.ts scripts/bench/bundle-composition/**/*.ts scripts/screenshots.ts scripts/screenshots/**/*.ts && deno check --config=e2e-payments/deno.json e2e-payments/src/**/*.ts",
"typecheck": "deno check src/**/*.ts src/**/*.tsx test/**/*.ts cli/**/*.ts scripts/database-snapshot.ts scripts/mutation.ts scripts/mutation/**/*.ts scripts/precommit.ts scripts/precommit/**/*.ts scripts/check-copy.ts scripts/check-copy/**/*.ts scripts/test-quality-audit.ts scripts/bench/bundle-composition/**/*.ts scripts/screenshots.ts scripts/screenshots/**/*.ts && deno check --config=e2e-payments/deno.json e2e-payments/src/**/*.ts",
Comment thread
stefan-burke marked this conversation as resolved.
Resolved
"cpd": "deno run -A scripts/cpd.ts --config .jscpd.json && deno run -A scripts/cpd.ts --config .jscpd.test.json && deno run -A scripts/cpd.ts --config .jscpd.css.json src/ui/static/style.scss",
"check:copy": "deno run --allow-read=src/locales scripts/check-copy.ts",
"precommit": "deno run -A scripts/precommit.ts",
Expand All @@ -42,6 +42,7 @@
},
"imports": {
"@jridgewell/sourcemap-codec": "npm:@jridgewell/sourcemap-codec@^1.5.5",
"#e2e/": "./e2e-payments/src/",
"#fp": "./src/fp.ts",
"#shared/test-overrides": "./src/shared/test-overrides.ts",
"#test-utils/": "./test/test-utils/",
Expand Down
24 changes: 10 additions & 14 deletions e2e-payments/src/providers/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
* tweak to one selector doesn't break the whole flow.
*/

import type { Frame, Locator, Page } from "playwright";
import type { Frame, Page } from "playwright";
import { log, warn } from "#e2e/log.ts";
import {
tryFirstVisibleIn,
type VisibleAction,
} from "#e2e/providers/visible-action.ts";

const FILL_TIMEOUT = 8_000;

Expand All @@ -24,21 +28,13 @@ const searchRoots = (page: Page): (Page | Frame)[] => [page, ...page.frames()];
const withFirstVisible = async <T>(
page: Page,
selectors: string[],
act: (loc: Locator, selector: string) => Promise<T>,
act: VisibleAction<T>,
): Promise<T | null> => {
const deadline = Date.now() + FILL_TIMEOUT;
while (Date.now() < deadline) {
for (const root of searchRoots(page)) {
for (const selector of selectors) {
const loc: Locator = root.locator(selector).first();
try {
if (await loc.isVisible({ timeout: 250 })) {
return await act(loc, selector);
}
} catch {
// selector not present in this root right now
}
}
const attempted = await tryFirstVisibleIn(root, selectors, act);
if (attempted.matched) return attempted.value;
}
await page.waitForTimeout(250);
}
Expand Down Expand Up @@ -152,7 +148,7 @@ export interface CardDetails {
* so those are tried first; the rest are common name/id/placeholder/aria
* fallbacks. fillFirst also searches child frames, covering SDK iframes.
*/
const CARD_SELECTORS: Record<string, string[]> = {
const CARD_SELECTORS = {
cvc: [
'input[autocomplete="cc-csc"]',
'input[name="cvc"]',
Expand Down Expand Up @@ -220,7 +216,7 @@ const CARD_SELECTORS: Record<string, string[]> = {
'input[id*="zip" i]',
"#billingPostalCode",
],
};
} satisfies Record<keyof CardDetails, string[]>;

/**
* Fill a hosted checkout's card form using the standard selectors. Pass
Expand Down
30 changes: 30 additions & 0 deletions e2e-payments/src/providers/visible-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Frame, Locator, Page } from "playwright";

export type VisibleAction<T> = (
locator: Locator,
selector: string,
) => Promise<T>;

export type VisibleAttempt<T> =
| { matched: true; value: T }
| { matched: false };

/** Try selectors in order. A hosted control that detaches or rejects an action
* is not ready yet, so the caller can poll this root again. */
export const tryFirstVisibleIn = async <T>(
root: Page | Frame,
selectors: string[],
act: VisibleAction<T>,
): Promise<VisibleAttempt<T>> => {
for (const selector of selectors) {
const locator = root.locator(selector).first();
try {
if (await locator.isVisible({ timeout: 250 })) {
return { matched: true, value: await act(locator, selector) };
}
} catch {
// The selector or hosted control is not ready in this root yet.
}
}
return { matched: false };
};
181 changes: 105 additions & 76 deletions scripts/mutation/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export interface EvaluationDeps {

type FailedTestState = Exclude<MutantTestStateResult, { status: "ready" }>;

interface MutantRunContext {
deps: EvaluationDeps;
plan: FileMutationPlan;
run: TestRunConfig;
signal: AbortSignal;
}

const realDeps: EvaluationDeps = {
createState: createMutantTestState,
runTests,
Expand All @@ -76,11 +83,8 @@ const testStatus = (
};

const confirmStateMutation = async (
plan: FileMutationPlan,
run: TestRunConfig,
signal: AbortSignal,
{ deps, plan, run, signal }: MutantRunContext,
failed: FailedTestState,
deps: EvaluationDeps,
): Promise<Status> => {
if (failed.status === "timed-out") return "timed-out";
await deps.write(plan.file, plan.original);
Expand All @@ -97,10 +101,8 @@ const confirmStateMutation = async (
};

const confirmAssetMutation = async (
plan: FileMutationPlan,
{ deps, plan, signal }: MutantRunContext,
assets: MutantAssetHooks,
signal: AbortSignal,
deps: EvaluationDeps,
): Promise<Status> => {
if (signal.aborted) return "timed-out";
await deps.write(plan.file, plan.original);
Expand All @@ -112,6 +114,89 @@ const confirmAssetMutation = async (
return "killed";
};

const runStaticGates = async (
{ plan, signal }: MutantRunContext,
gates: StaticGate[],
timings: PhaseTiming[],
): Promise<MutantEvaluation | null> => {
for (const gate of gates) {
const measured = await measurePhase(gate.phase, () =>
gate.exit(plan.file, signal),
);
timings.push(measured.timing);
if (measured.value !== 0) {
return { detectedBy: gate.phase, status: "killed", timings };
}
}
return null;
};

const runTestFiles = async (
{ deps, run, signal }: MutantRunContext,
phase: "direct-tests" | "integration-tests",
testFiles: string[],
env: Record<string, string>,
): Promise<TestStageResult> =>
testStatus(phase, await deps.runTests({ ...run, env, testFiles }, signal));

const runIntegrationTestStage = async (
context: MutantRunContext,
testFiles: string[],
): Promise<TestStageResult> => {
const { deps, plan, run, signal } = context;
let state: MutantTestStateResult | null = null;
let env = mutantTestEnv(run.env, plan.rebuildTestState);
const timings: PhaseTiming[] = [];
if (plan.rebuildTestState) {
const measured = await measurePhase("test-state", () =>
deps.createState(run.env, signal),
);
timings.push(measured.timing);
state = measured.value;
if (state.status !== "ready") {
const status = await confirmStateMutation(context, state);
return {
detectedBy: detectedByKill("test-state", status),
status,
timings,
};
}
env = state.state.env;
}
try {
const tested = await runTestFiles(
context,
"integration-tests",
testFiles,
env,
);
return {
detectedBy: tested.detectedBy,
status: tested.status,
timings: [...timings, ...tested.timings],
};
} finally {
if (state?.status === "ready") await state.state.cleanup();
}
};

const runTestStage =
(context: MutantRunContext) =>
async (
phase: "direct-tests" | "integration-tests",
testFiles: string[],
): Promise<TestStageResult> => {
if (phase === "direct-tests") {
return runTestFiles(
context,
phase,
testFiles,
mutantTestEnv(context.run.env, context.plan.rebuildTestState),
);
}
return runIntegrationTestStage(context, testFiles);
};

export const evaluateMutant = async (
plan: FileMutationPlan,
mutant: Mutant,
Expand All @@ -121,25 +206,18 @@ export const evaluateMutant = async (
signal: AbortSignal,
deps: EvaluationDeps = realDeps,
): Promise<MutantEvaluation> => {
const { assets, directTestFiles, file, original, rebuildTestState } = plan;
await deps.write(plan.file, applyMutant(plan.original, mutant));
const context = { deps, plan, run, signal };
const timings: PhaseTiming[] = [];
await deps.write(file, applyMutant(original, mutant));
return withCleanup(async () => {
try {
for (const gate of gates) {
const measured = await measurePhase(gate.phase, () =>
gate.exit(file, signal),
);
timings.push(measured.timing);
if (measured.value !== 0) {
return { detectedBy: gate.phase, status: "killed", timings };
}
}
if (assets) {
const measured = await measurePhase("asset-build", assets.rebuild);
const gateResult = await runStaticGates(context, gates, timings);
if (gateResult) return gateResult;
if (plan.assets) {
const measured = await measurePhase("asset-build", plan.assets.rebuild);
timings.push(measured.timing);
if (!measured.value) {
const status = await confirmAssetMutation(plan, assets, signal, deps);
const status = await confirmAssetMutation(context, plan.assets);
return {
detectedBy: detectedByKill("asset-build", status),
status,
Expand All @@ -148,61 +226,9 @@ export const evaluateMutant = async (
}
}
const stages = await runTestStages(
directTestFiles,
plan.directTestFiles,
integrationTestFiles,
async (phase, testFiles) => {
if (phase === "direct-tests") {
return testStatus(
phase,
await deps.runTests(
{
...run,
env: mutantTestEnv(run.env, rebuildTestState),
testFiles,
},
signal,
),
);
}
let state: MutantTestStateResult | null = null;
let env = mutantTestEnv(run.env, rebuildTestState);
const stageTimings: PhaseTiming[] = [];
if (rebuildTestState) {
const measured = await measurePhase("test-state", () =>
deps.createState(run.env, signal),
);
stageTimings.push(measured.timing);
state = measured.value;
if (state.status !== "ready") {
const status = await confirmStateMutation(
plan,
run,
signal,
state,
deps,
);
return {
detectedBy: detectedByKill("test-state", status),
status,
timings: stageTimings,
};
}
env = state.state.env;
}
try {
const tested = testStatus(
phase,
await deps.runTests({ ...run, env, testFiles }, signal),
);
return {
detectedBy: tested.detectedBy,
status: tested.status,
timings: [...stageTimings, ...tested.timings],
};
} finally {
if (state?.status === "ready") await state.state.cleanup();
}
},
runTestStage(context),
);
timings.push(...stages.timings);
return {
Expand All @@ -216,7 +242,10 @@ export const evaluateMutant = async (
}
throw error;
}
}, [() => deps.write(file, original), ...(assets ? [assets.restore] : [])]);
}, [
() => deps.write(plan.file, plan.original),
...(plan.assets ? [plan.assets.restore] : []),
]);
};

const logFilePlan = (plan: FileMutationPlan, affectedCount: number): void => {
Expand Down
Loading