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
17 changes: 6 additions & 11 deletions lib/components/fuse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from "zod"
import { createConnectionsProp } from "lib/common/connectionsProp"
import {
type CommonComponentProps,
commonComponentProps,
Expand All @@ -8,6 +9,7 @@ import {
type SchematicOrientation,
} from "lib/common/schematicOrientation"
import type { Connections } from "lib/utility-types/connections-and-selectors"
import { expectTypesMatch } from "lib/typecheck"

/**
* Pin labels for fuse component
Expand Down Expand Up @@ -38,7 +40,7 @@ export interface FuseProps<PinLabel extends string = string>
/**
* Connections to other components
*/
connections?: Connections<PinLabel>
connections?: Connections<FusePinLabels>
}

/**
Expand All @@ -49,16 +51,9 @@ export const fuseProps = commonComponentProps.extend({
voltageRating: z.union([z.number(), z.string()]).optional(),
schShowRatings: z.boolean().optional(),
schOrientation: schematicOrientation.optional(),
connections: z
.record(
z.string(),
z.union([
z.string(),
z.array(z.string()).readonly(),
z.array(z.string()),
]),
)
.optional(),
connections: createConnectionsProp(fusePinLabels).optional(),
})

export type InferredFuseProps = z.input<typeof fuseProps>

expectTypesMatch<FuseProps, InferredFuseProps>(true)
48 changes: 48 additions & 0 deletions tests/fuse-connections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, test } from "bun:test"
import { capacitorProps } from "lib/components/capacitor"
import { fuseProps } from "lib/components/fuse"

const parseFuse = (connections: unknown) =>
fuseProps.safeParse({ name: "F1", currentRating: "1A", connections })

test("fuse accepts its real pin labels in connections", () => {
expect(parseFuse({ pin1: ".R1 > .pin1" }).success).toBe(true)
expect(parseFuse({ pin2: ".R1 > .pin1" }).success).toBe(true)
expect(parseFuse({ pin1: ".R1 > .pin1", pin2: ".R2 > .pin1" }).success).toBe(
true,
)
})

test("fuse rejects a pin label it does not have", () => {
// Previously accepted: the schema used a bare z.record(z.string(), ...) so
// any key at all passed, including a typo'd pin that the fuse has no port for.
expect(parseFuse({ pin99: ".R1 > .pin1" }).success).toBe(false)
expect(parseFuse({ "not a pin at all": ".R1 > .pin1" }).success).toBe(false)
expect(parseFuse({ "": ".R1 > .pin1" }).success).toBe(false)
})

test("fuse validates connections the same way the other two-pin passives do", () => {
// Same typo, same shape of component — these must agree.
const typo = { pin99: ".R1 > .pin1" }

expect(parseFuse(typo).success).toBe(
capacitorProps.safeParse({
name: "C1",
capacitance: "1uF",
connections: typo,
}).success,
)
})

test("fuse still accepts array and readonly-array connection targets", () => {
expect(parseFuse({ pin1: [".R1 > .pin1", ".R2 > .pin1"] }).success).toBe(true)
expect(
parseFuse({ pin1: [".R1 > .pin1"] as readonly string[] }).success,
).toBe(true)
})

test("connections stays optional on fuse", () => {
expect(fuseProps.safeParse({ name: "F1", currentRating: "1A" }).success).toBe(
true,
)
})
Loading