-
Notifications
You must be signed in to change notification settings - Fork 1
Snapshot test #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+314
−19
Merged
Snapshot test #42
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aeb96e0
WIP
0hmX f231b7a
WIP
0hmX ad5aa80
WIP
0hmX 9b761b8
WIP
0hmX ba2df6f
WIP
0hmX 2532128
WIP
0hmX 44408c0
WIP
0hmX 6294cf2
WIO
0hmX aa84bb7
WIP
0hmX 86b8743
Merge branch 'main' into snapshot-test
0hmX 26deec0
WIP
0hmX 0c45189
WIP
0hmX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export const getColorForZLayer = ( | ||
| zLayers: number[], | ||
| ): { | ||
| fill: string | ||
| stroke: string | ||
| } => { | ||
| const minZ = Math.min(...zLayers) | ||
| const colors = [ | ||
| { fill: "#dbeafe", stroke: "#3b82f6" }, | ||
| { fill: "#fef3c7", stroke: "#f59e0b" }, | ||
| { fill: "#d1fae5", stroke: "#10b981" }, | ||
| { fill: "#e9d5ff", stroke: "#a855f7" }, | ||
| { fill: "#fed7aa", stroke: "#f97316" }, | ||
| { fill: "#fecaca", stroke: "#ef4444" }, | ||
| ] as const | ||
| return colors[minZ % colors.length]! | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import type { GraphicsObject, Line, Point, Rect } from "graphics-debug" | ||
|
|
||
| export function getPerLayerVisualizations( | ||
| graphics: GraphicsObject, | ||
| ): Map<string, GraphicsObject> { | ||
| const rects = (graphics.rects ?? []) as NonNullable<Rect[]> | ||
| const lines = (graphics.lines ?? []) as NonNullable<Line[]> | ||
| const points = (graphics.points ?? []) as NonNullable<Point[]> | ||
|
|
||
| const zValues = new Set<number>() | ||
|
|
||
| const addZValuesFromLayer = (layer: string) => { | ||
| if (!layer.startsWith("z")) return | ||
| const rest = layer.slice(1) | ||
| if (!rest) return | ||
| for (const part of rest.split(",")) { | ||
| const value = Number.parseInt(part, 10) | ||
| if (!Number.isNaN(value)) zValues.add(value) | ||
| } | ||
| } | ||
|
|
||
| for (const rect of rects) addZValuesFromLayer(rect.layer!) | ||
| for (const line of lines) addZValuesFromLayer(line.layer!) | ||
| for (const point of points) addZValuesFromLayer(point.layer!) | ||
|
|
||
| const result = new Map<string, GraphicsObject>() | ||
| if (!zValues.size) return result | ||
|
|
||
| const sortedZ = Array.from(zValues).sort((a, b) => a - b) | ||
|
|
||
| const commonRects: NonNullable<Rect[]> = [] | ||
| const perLayerRects: { layers: number[]; rect: Rect }[] = [] | ||
|
|
||
| for (const rect of rects) { | ||
| const layer = rect.layer! | ||
| if (layer.startsWith("z")) { | ||
| const rest = layer.slice(1) | ||
| if (rest) { | ||
| const layers = rest | ||
| .split(",") | ||
| .map((part) => Number.parseInt(part, 10)) | ||
| .filter((value) => !Number.isNaN(value)) | ||
| if (layers.length) { | ||
| perLayerRects.push({ layers, rect }) | ||
| continue | ||
| } | ||
| } | ||
| } | ||
| commonRects.push(rect) | ||
| } | ||
|
|
||
| const commonLines: NonNullable<Line[]> = [] | ||
| const perLayerLines: { layers: number[]; line: Line }[] = [] | ||
|
|
||
| for (const line of lines) { | ||
| const layer = line.layer! | ||
| if (layer.startsWith("z")) { | ||
| const rest = layer.slice(1) | ||
| if (rest) { | ||
| const layers = rest | ||
| .split(",") | ||
| .map((part) => Number.parseInt(part, 10)) | ||
| .filter((value) => !Number.isNaN(value)) | ||
| if (layers.length) { | ||
| perLayerLines.push({ layers, line }) | ||
| continue | ||
| } | ||
| } | ||
| } | ||
| commonLines.push(line) | ||
| } | ||
|
|
||
| const commonPoints: NonNullable<Point[]> = [] | ||
| const perLayerPoints: { layers: number[]; point: Point }[] = [] | ||
|
|
||
| for (const point of points) { | ||
| const layer = point.layer! | ||
| if (layer.startsWith("z")) { | ||
| const rest = layer.slice(1) | ||
| if (rest) { | ||
| const layers = rest | ||
| .split(",") | ||
| .map((part) => Number.parseInt(part, 10)) | ||
| .filter((value) => !Number.isNaN(value)) | ||
| if (layers.length) { | ||
| perLayerPoints.push({ layers, point }) | ||
| continue | ||
| } | ||
| } | ||
| } | ||
| commonPoints.push(point) | ||
| } | ||
|
|
||
| const allCombos: number[][] = [[]] | ||
| for (const z of sortedZ) { | ||
| const withZ = allCombos.map((combo) => [...combo, z]) | ||
| allCombos.push(...withZ) | ||
| } | ||
|
|
||
| for (const combo of allCombos.filter((c) => c.length > 0)) { | ||
| const key = `z${combo.join(",")}` | ||
|
|
||
| const layerRects: NonNullable<Rect[]> = [...commonRects] | ||
| const layerLines: NonNullable<Line[]> = [...commonLines] | ||
| const layerPoints: NonNullable<Point[]> = [...commonPoints] | ||
|
|
||
| const intersects = (layers: number[]) => | ||
| layers.some((layer) => combo.includes(layer)) | ||
|
|
||
| for (const { layers, rect } of perLayerRects) { | ||
| if (intersects(layers)) layerRects.push(rect) | ||
| } | ||
| for (const { layers, line } of perLayerLines) { | ||
| if (intersects(layers)) layerLines.push(line) | ||
| } | ||
| for (const { layers, point } of perLayerPoints) { | ||
| if (intersects(layers)) layerPoints.push(point) | ||
| } | ||
|
|
||
| result.set(key, { | ||
| title: `${graphics.title ?? ""} - z${combo.join(",")}`, | ||
| coordinateSystem: graphics.coordinateSystem, | ||
| rects: layerRects, | ||
| lines: layerLines, | ||
| points: layerPoints, | ||
| }) | ||
| } | ||
|
|
||
| return result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { Rect } from "graphics-debug" | ||
| import type { CapacityMeshNode } from "lib/types/capacity-mesh-types" | ||
| import { getColorForZLayer } from "lib/utils/getColorForZLayer" | ||
|
|
||
| export const makeCapacityMeshNodeWithLayerInfo = ( | ||
| nodes: CapacityMeshNode[], | ||
| ): Map<string, Rect[]> => { | ||
| const map = new Map<string, Rect[]>() | ||
|
|
||
| for (const node of nodes) { | ||
| if (!node.availableZ.length) continue | ||
| const key = node.availableZ.join(",") | ||
| const colors = getColorForZLayer(node.availableZ) | ||
| const rect: Rect = { | ||
| center: node.center, | ||
| width: node.width, | ||
| height: node.height, | ||
| layer: `z${key}`, | ||
| stroke: "black", | ||
| fill: node._containsObstacle ? "red" : colors.fill, | ||
| label: "node", | ||
| } | ||
|
|
||
| const existing = map.get(key) | ||
| if (existing) { | ||
| existing.push(rect) | ||
| } else { | ||
| map.set(key, [rect]) | ||
| } | ||
| } | ||
|
|
||
| return map | ||
| } |
44 changes: 44 additions & 0 deletions
44
tests/solver/__snapshots__/rectDiffGridSolverPipeline.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { expect, test } from "bun:test" | ||
| import srj from "test-assets/bugreport11-b2de3c.json" | ||
| import { | ||
| getBounds, | ||
| getSvgFromGraphicsObject, | ||
| stackGraphicsVertically, | ||
| type GraphicsObject, | ||
| type Rect, | ||
| } from "graphics-debug" | ||
| import { RectDiffPipeline } from "lib/RectDiffPipeline" | ||
| import { makeCapacityMeshNodeWithLayerInfo } from "tests/fixtures/makeCapacityMeshNodeWithLayerInfo" | ||
|
|
||
| test("RectDiffPipeline mesh layer snapshots", async () => { | ||
| const solver = new RectDiffPipeline({ | ||
| simpleRouteJson: srj.simple_route_json, | ||
| }) | ||
|
|
||
| solver.solve() | ||
|
|
||
| const { meshNodes } = solver.getOutput() | ||
| const rectsByCombo = makeCapacityMeshNodeWithLayerInfo(meshNodes) | ||
| const allGraphicsObjects: GraphicsObject[] = [] | ||
|
|
||
| // Generate a snapshot for each z-layer | ||
| for (const z of [0, 1, 2, 3]) { | ||
| const layerRects: Rect[] = [] | ||
|
|
||
| for (const [key, rects] of rectsByCombo) { | ||
| const layers = key | ||
| .split(",") | ||
| .map((value) => Number.parseInt(value, 10)) | ||
| .filter((value) => !Number.isNaN(value)) | ||
|
|
||
| if (layers.includes(z)) { | ||
| layerRects.push(...rects) | ||
| } | ||
| } | ||
|
|
||
| let labelY = 0 | ||
|
|
||
| if (layerRects.length > 0) { | ||
| let maxY = -Infinity | ||
|
|
||
| for (const rect of layerRects) { | ||
| const top = rect.center.y + rect.height * (2 / 3) | ||
|
|
||
| if (top > maxY) maxY = top | ||
| } | ||
|
|
||
| labelY = maxY | ||
| } | ||
|
|
||
| const graphics: GraphicsObject = { | ||
| title: `RectDiffPipeline - z${z}`, | ||
| texts: [ | ||
| { | ||
| anchorSide: "top_right", | ||
| text: `Layer z=${z}`, | ||
| x: 0, | ||
| y: labelY, | ||
| fontSize: 0.5, | ||
| }, | ||
| ], | ||
| coordinateSystem: "cartesian", | ||
| rects: layerRects, | ||
| points: [], | ||
| lines: [], | ||
| } | ||
|
|
||
| allGraphicsObjects.push(graphics) | ||
| } | ||
|
|
||
| const stackedGraphics = stackGraphicsVertically(allGraphicsObjects) | ||
| const bounds = getBounds(stackedGraphics) | ||
| const boundsWidth = Math.max(1, bounds.maxX - bounds.minX) | ||
| const boundsHeight = Math.max(1, bounds.maxY - bounds.minY) | ||
| const svgWidth = 640 | ||
| const svgHeight = Math.max( | ||
| svgWidth, | ||
| Math.ceil((boundsHeight / boundsWidth) * svgWidth), | ||
| ) | ||
|
|
||
| const svg = getSvgFromGraphicsObject(stackedGraphics, { | ||
| svgWidth, | ||
| svgHeight, | ||
| }) | ||
| await expect(svg).toMatchSvgSnapshot(import.meta.path) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.