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
2 changes: 1 addition & 1 deletion lib/components/pin-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface PinHeaderProps extends CommonComponentProps {
}

export const pinHeaderProps = commonComponentProps.extend({
pinCount: z.number(),
pinCount: z.number().int().positive(),
pitch: distance.optional(),
schFacingDirection: z.enum(["up", "down", "left", "right"]).optional(),
gender: z.enum(["male", "female", "unpopulated"]).optional().default("male"),
Expand Down
43 changes: 43 additions & 0 deletions tests/pin-header-pin-count.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect, test } from "bun:test"
import { pinHeaderProps } from "lib/components/pin-header"

const parse = (pinCount: unknown) =>
pinHeaderProps.safeParse({ name: "H1", pinCount })

test("pinCount accepts whole positive counts", () => {
expect(parse(1).success).toBe(true)
expect(parse(4).success).toBe(true)
expect(parse(40).success).toBe(true)
})

test("pinCount rejects a fractional count", () => {
// Previously accepted. A fractional count produced a header whose port count
// and pad count disagreed (pinCount 2.5 -> 2 source_ports but 3 pads) with no
// error raised anywhere.
expect(parse(2.5).success).toBe(false)
expect(parse(3.7).success).toBe(false)
})

test("pinCount rejects zero and negative counts", () => {
// A header with no pins is not a header; these previously reached the renderer
// and surfaced later as an unrelated pcb_missing_footprint_error.
expect(parse(0).success).toBe(false)
expect(parse(-4).success).toBe(false)
})

test("pinCount rejects non-finite counts", () => {
expect(parse(Number.NaN).success).toBe(false)
expect(parse(Number.POSITIVE_INFINITY).success).toBe(false)
})

test("pinCount is still required", () => {
// The guard must not turn a required prop into an optional one.
expect(pinHeaderProps.safeParse({ name: "H1" }).success).toBe(false)
})

test("a valid pinCount still parses to the same value", () => {
const result = pinHeaderProps.safeParse({ name: "H1", pinCount: 8 })

expect(result.success).toBe(true)
if (result.success) expect(result.data.pinCount).toBe(8)
})
Loading