Skip to content

Commit da86813

Browse files
committed
refactor(ios-snapshot): isolate differential route assertions
1 parent b9165c7 commit da86813

1 file changed

Lines changed: 102 additions & 30 deletions

File tree

‎packages/capture-kit/src/ios-snapshot-engine/conformance-harness.ts‎

Lines changed: 102 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ function runnerPresentationAgrees(
212212
if (swift.outcome !== acquired.outcome) return false;
213213
if (swift.outcome === 'failure')
214214
return JSON.stringify(swift.error) === JSON.stringify(acquired.error);
215+
const { presented, published } = runRunnerComposition(testCase, swift);
216+
return runnerOutputAgrees(testCase, swift, acquired, presented, published);
217+
}
218+
219+
function runRunnerComposition(
220+
testCase: DifferentialCase,
221+
swift: DifferentialOutcome,
222+
): Readonly<{
223+
presented: ReturnType<typeof presentIosSnapshot>;
224+
published: readonly CanonicalNode[];
225+
}> {
215226
const request = createIosSnapshotRequest(testCase);
216227
const input = {
217228
stage: 'presented' as const,
@@ -235,45 +246,106 @@ function runnerPresentationAgrees(
235246
const published = canonicalNodes(
236247
publishIosSnapshot(input, request, { foldPolicy: testCase.foldPolicy }).payload.nodes,
237248
);
238-
if (
239-
testCase.scope !== null &&
240-
JSON.stringify(semanticMembership(canonicalNodes(presented.qualityNodes ?? []))) !==
241-
JSON.stringify(semanticMembership(acquired.canonicalQualityNodes ?? []))
242-
)
243-
return false;
244-
const outputIndexes = new Set(presented.nodes.map((node) => node.index));
245-
if (
246-
(swift.rawNodes ?? []).some((node) => !presented.presentedIndexesBySourceIndex.has(node.index))
247-
)
249+
return { presented, published };
250+
}
251+
252+
function runnerOutputAgrees(
253+
testCase: DifferentialCase,
254+
swift: DifferentialOutcome,
255+
acquired: DifferentialOutcome,
256+
presented: ReturnType<typeof presentIosSnapshot>,
257+
published: readonly CanonicalNode[],
258+
): boolean {
259+
return (
260+
qualityMembershipAgrees(testCase, presented.qualityNodes, acquired.canonicalQualityNodes) &&
261+
representativesAreValid(swift.rawNodes ?? [], presented) &&
262+
capturedMembershipAgrees(testCase, swift.rawNodes ?? [], presented, published) &&
263+
semanticMembershipAgrees(published, acquired.nodes)
264+
);
265+
}
266+
267+
function capturedMembershipAgrees(
268+
testCase: DifferentialCase,
269+
sources: readonly RawSnapshotNode[],
270+
presented: ReturnType<typeof presentIosSnapshot>,
271+
published: readonly CanonicalNode[],
272+
): boolean {
273+
return (
274+
requiredLabelsHaveRepresentatives(
275+
testCase.requiredLabels ?? [],
276+
sources,
277+
presented,
278+
published,
279+
) &&
280+
(testCase.absentLabels ?? []).every(
281+
(label) => !published.some((node) => node.label === label),
282+
) &&
283+
clippedLabelAgrees(testCase.clippedLabel, published)
284+
);
285+
}
286+
287+
function semanticMembershipAgrees(
288+
left: readonly CanonicalNode[],
289+
right: readonly CanonicalNode[],
290+
): boolean {
291+
return JSON.stringify(semanticMembership(left)) === JSON.stringify(semanticMembership(right));
292+
}
293+
294+
function qualityMembershipAgrees(
295+
testCase: DifferentialCase,
296+
actual: readonly RawSnapshotNode[] | undefined,
297+
expected: readonly CanonicalNode[] | undefined,
298+
): boolean {
299+
return (
300+
testCase.scope === null ||
301+
JSON.stringify(semanticMembership(canonicalNodes(actual ?? []))) ===
302+
JSON.stringify(semanticMembership(expected ?? []))
303+
);
304+
}
305+
306+
function representativesAreValid(
307+
sources: readonly RawSnapshotNode[],
308+
presentation: ReturnType<typeof presentIosSnapshot>,
309+
): boolean {
310+
const outputIndexes = new Set(presentation.nodes.map((node) => node.index));
311+
if (sources.some((node) => !presentation.presentedIndexesBySourceIndex.has(node.index)))
248312
return false;
249-
for (const indexes of presented.presentedIndexesBySourceIndex.values()) {
250-
if (indexes.some((index) => !outputIndexes.has(index))) return false;
251-
}
252-
for (const label of testCase.requiredLabels ?? []) {
253-
const sources = swift.rawNodes?.filter((node) => node.label === label) ?? [];
313+
return [...presentation.presentedIndexesBySourceIndex.values()].every((indexes) =>
314+
indexes.every((index) => outputIndexes.has(index)),
315+
);
316+
}
317+
318+
function requiredLabelsHaveRepresentatives(
319+
requiredLabels: readonly string[],
320+
sources: readonly RawSnapshotNode[],
321+
presentation: ReturnType<typeof presentIosSnapshot>,
322+
published: readonly CanonicalNode[],
323+
): boolean {
324+
for (const label of requiredLabels) {
325+
const matchingSources = sources.filter((node) => node.label === label);
254326
if (
255-
sources.length === 0 ||
256-
!sources.some(
257-
(source) => (presented.presentedIndexesBySourceIndex.get(source.index) ?? []).length > 0,
327+
matchingSources.length === 0 ||
328+
!matchingSources.some(
329+
(source) => (presentation.presentedIndexesBySourceIndex.get(source.index) ?? []).length > 0,
258330
)
259331
)
260332
return false;
261333
if (!published.some((node) => node.label === label)) return false;
262334
}
263-
if ((testCase.absentLabels ?? []).some((label) => published.some((node) => node.label === label)))
264-
return false;
265-
if (
266-
testCase.clippedLabel &&
267-
!published.some(
335+
return true;
336+
}
337+
338+
function clippedLabelAgrees(
339+
expected: DifferentialCase['clippedLabel'],
340+
published: readonly CanonicalNode[],
341+
): boolean {
342+
return (
343+
!expected ||
344+
published.some(
268345
(node) =>
269-
node.label === testCase.clippedLabel!.label &&
270-
JSON.stringify(node.rect) === JSON.stringify(testCase.clippedLabel!.rect),
346+
node.label === expected.label &&
347+
JSON.stringify(node.rect) === JSON.stringify(expected.rect),
271348
)
272-
)
273-
return false;
274-
return (
275-
JSON.stringify(semanticMembership(published)) ===
276-
JSON.stringify(semanticMembership(acquired.nodes))
277349
);
278350
}
279351

0 commit comments

Comments
 (0)