validate-position.ts
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Position validation for a single iteration.
* Captures browser positions, groups by component, identifies misaligned components.
*/
import { logger } from '../../../utils/logger';
import { PositionTool } from '../../../tools/position-tool';
import type { MisalignedComponent } from '../../../types/validation-types';
import type { ValidationIterationConfig, ValidationIterationResult, SkippedElement } from '../types';
/**
* Validate positions by capturing browser positions and comparing with Figma.
* This function integrates with the existing captureBrowserPositions tool
* and transforms its output into the format expected by the validation loop.
*
* @param config - Validation iteration configuration
* @returns Validation result with MAE, SAE, misaligned components, and viewport
*/
export async function validatePositions(config: ValidationIterationConfig): Promise<ValidationIterationResult> {
const { serverUrl, figmaThumbnailUrl, protocol, positionThreshold, validationContext, elementRegistry, resolvedComponentPaths } =
config;
const positionTool = new PositionTool();
// Type assertion: captureBrowserPositions accepts flexible input types for compatibility
const captureResult = await positionTool.capturePosition({
protocol,
validationContext,
url: serverUrl,
figmaThumbnailUrl,
positionThreshold,
returnScreenshot: true,
elementRegistry,
});
// Create resolved elementToComponent mapping with absolute paths
// Replace alias paths with absolute filesystem paths from resolvedComponentPaths
const resolvedElementToComponent = new Map<string, { id: string; name: string; path: string }>();
for (const [elementId, componentInfo] of validationContext.elementToComponent) {
const resolvedPath = resolvedComponentPaths[componentInfo.id] || componentInfo.path;
resolvedElementToComponent.set(elementId, {
id: componentInfo.id,
name: componentInfo.name,
path: resolvedPath,
});
}
const aggregated = positionTool.aggregateElements(captureResult.positions, resolvedElementToComponent, positionThreshold);
const misalignedComponents = aggregated.misalignedComponents as unknown as MisalignedComponent[];
const skippedElements = aggregated.skippedElements as unknown as SkippedElement[];
// Calculate SAE
const sae = misalignedComponents.reduce((sum, comp) => {
const [errorX, errorY] = comp.validationReport.absoluteError;
return sum + errorX + errorY;
}, 0);
logger.printInfoLog(`Found ${misalignedComponents.length} misaligned components`);
return {
mae: captureResult.metadata.mae,
sae,
misalignedComponents,
skippedElements,
viewport: captureResult.metadata.viewport,
screenshots: captureResult.screenshot
? {
renderSnap: captureResult.screenshot,
}
: undefined,
};
}