Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions lib/components/primitive-components/Group/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
trace_clearance: props.autorouter.traceClearance,
}
: undefined,
anchor_alignment: props.pcbAnchorAlignment ?? null,
// circuit-json declares this as `ninePointAnchor.default("center")`, so a
// `null` fails validation while omitting it lets the schema default apply.
anchor_alignment: props.pcbAnchorAlignment ?? undefined,
})
this.pcb_group_id = pcb_group.pcb_group_id

Expand Down Expand Up @@ -1587,7 +1589,10 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
const { _parsedProps: props } = this
const schematic_group = db.schematic_group.insert({
is_subcircuit: this.isSubcircuit,
subcircuit_id: this.subcircuit_id!,
// `subcircuit_id` is `string | null` on the component but
// `z.string().optional()` on the schema, so `null` fails validation.
// A group outside any subcircuit simply has none.
subcircuit_id: this.subcircuit_id ?? undefined,
name: this.name,
center: this._getGlobalSchematicPositionBeforeLayout(),
width: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test } from "bun:test"
import * as CJ from "circuit-json"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("groups do not emit null anchor_alignment / subcircuit_id", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="30mm" height="30mm" routingDisabled>
<group name="G1" pcbX={4}>
<resistor name="R1" resistance="1k" footprint="0402" />
</group>
<group name="G2" pcbX={-6} pcbAnchorAlignment="top_left">
<resistor name="R2" resistance="1k" footprint="0402" />
</group>
</board>,
)

await circuit.renderUntilSettled()

const circuitJson = circuit.getCircuitJson()
const pcbGroups = circuitJson.filter(
(e: any) => e.type === "pcb_group",
) as any[]
const schematicGroups = circuitJson.filter(
(e: any) => e.type === "schematic_group",
) as any[]

expect(pcbGroups.length).toBeGreaterThan(0)
expect(schematicGroups.length).toBeGreaterThan(0)

// `anchor_alignment` is `ninePointAnchor.default("center")` — `null` fails
// validation, while omitting it lets the schema default apply.
for (const group of pcbGroups) {
expect(group.anchor_alignment).not.toBeNull()
}

// `subcircuit_id` is `z.string().optional()`, so `null` is rejected.
for (const group of schematicGroups) {
expect(group.subcircuit_id).not.toBeNull()
}

// An explicitly requested alignment must still survive.
const explicit = pcbGroups.find((g) => g.anchor_alignment === "top_left")
expect(explicit).toBeDefined()

// And these two fields must no longer be the reason validation fails.
for (const group of [...pcbGroups, ...schematicGroups]) {
const schema = (CJ as any)[group.type]
const result = schema.safeParse(group)
const relevantIssues = result.success
? []
: result.error.issues
.filter((i: any) =>
["anchor_alignment", "subcircuit_id"].includes(String(i.path[0])),
)
.map((i: any) => `${i.path.join(".")}: ${i.message}`)
expect({ type: group.type, relevantIssues }).toEqual({
type: group.type,
relevantIssues: [],
})
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test("Subcircuit group should have subcircuit_id", async () => {
expect(circuit.db.pcb_group.list()).toMatchInlineSnapshot(`
[
{
"anchor_alignment": null,
"anchor_alignment": undefined,
"anchor_position": {
"x": 0,
"y": 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/groups/group-outline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("group with outline specified", async () => {
expect(pcb_groups).toMatchInlineSnapshot(`
[
{
"anchor_alignment": null,
"anchor_alignment": undefined,
"anchor_position": {
"x": 0,
"y": 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/groups/group-schematic-box.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test("group schematic box", () => {
"schematic_group_id": "schematic_group_0",
"show_as_schematic_box": true,
"source_group_id": "source_group_0",
"subcircuit_id": null,
"subcircuit_id": undefined,
"type": "schematic_group",
"width": 0.4,
},
Expand Down
Loading