|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import { convertCircuitJsonToTscircuit } from "lib/index" |
| 3 | +import type { AnyCircuitElement } from "circuit-json" |
| 4 | + |
| 5 | +declare module "bun:test" { |
| 6 | + interface Matchers<T = unknown> { |
| 7 | + toMatchInlineSnapshot(snapshot?: string | null): Promise<MatcherResult> |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +test("test pcb_courtyard_rect conversion - basic courtyard", async () => { |
| 12 | + const circuitJson: AnyCircuitElement[] = [ |
| 13 | + { |
| 14 | + type: "pcb_smtpad", |
| 15 | + pcb_smtpad_id: "pad1", |
| 16 | + shape: "rect", |
| 17 | + x: -1, |
| 18 | + y: 0, |
| 19 | + width: 0.6, |
| 20 | + height: 0.8, |
| 21 | + layer: "top", |
| 22 | + pcb_component_id: "comp1", |
| 23 | + pcb_port_id: "port1", |
| 24 | + port_hints: ["1", "left"], |
| 25 | + }, |
| 26 | + { |
| 27 | + type: "pcb_smtpad", |
| 28 | + pcb_smtpad_id: "pad2", |
| 29 | + shape: "rect", |
| 30 | + x: 1, |
| 31 | + y: 0, |
| 32 | + width: 0.6, |
| 33 | + height: 0.8, |
| 34 | + layer: "top", |
| 35 | + pcb_component_id: "comp1", |
| 36 | + pcb_port_id: "port2", |
| 37 | + port_hints: ["2", "right"], |
| 38 | + }, |
| 39 | + { |
| 40 | + type: "pcb_courtyard_rect", |
| 41 | + pcb_courtyard_rect_id: "courtyard1", |
| 42 | + center: { x: 0, y: 0 }, |
| 43 | + width: 3, |
| 44 | + height: 2, |
| 45 | + layer: "top", |
| 46 | + stroke_width: 0.1, |
| 47 | + pcb_component_id: "comp1", |
| 48 | + }, |
| 49 | + ] |
| 50 | + |
| 51 | + const tscircuit = convertCircuitJsonToTscircuit(circuitJson, { |
| 52 | + componentName: "ComponentWithCourtyard", |
| 53 | + }) |
| 54 | + |
| 55 | + expect(tscircuit).toMatchInlineSnapshot(` |
| 56 | + "import { type ChipProps } from "tscircuit" |
| 57 | + export const ComponentWithCourtyard = (props: ChipProps) => ( |
| 58 | + <chip |
| 59 | + footprint={<footprint> |
| 60 | + <smtpad portHints={["1","left"]} pcbX="-1mm" pcbY="0mm" width="0.6mm" height="0.8mm" shape="rect" /> |
| 61 | + <smtpad portHints={["2","right"]} pcbX="1mm" pcbY="0mm" width="0.6mm" height="0.8mm" shape="rect" /> |
| 62 | + <courtyardrect pcbX="0mm" pcbY="0mm" width="3mm" height="2mm" layer="top" /> |
| 63 | + </footprint>} |
| 64 | + {...props} |
| 65 | + /> |
| 66 | + )" |
| 67 | + `) |
| 68 | +}) |
| 69 | + |
| 70 | +test("test pcb_courtyard_rect conversion - multiple courtyards different layers", async () => { |
| 71 | + const circuitJson: AnyCircuitElement[] = [ |
| 72 | + { |
| 73 | + type: "pcb_smtpad", |
| 74 | + pcb_smtpad_id: "pad1", |
| 75 | + shape: "rect", |
| 76 | + x: 0, |
| 77 | + y: 0, |
| 78 | + width: 1, |
| 79 | + height: 1, |
| 80 | + layer: "top", |
| 81 | + pcb_component_id: "comp1", |
| 82 | + pcb_port_id: "port1", |
| 83 | + port_hints: ["1"], |
| 84 | + }, |
| 85 | + { |
| 86 | + type: "pcb_courtyard_rect", |
| 87 | + pcb_courtyard_rect_id: "courtyard1", |
| 88 | + center: { x: 0, y: 0 }, |
| 89 | + width: 2.5, |
| 90 | + height: 2.5, |
| 91 | + layer: "top", |
| 92 | + stroke_width: 0.1, |
| 93 | + pcb_component_id: "comp1", |
| 94 | + }, |
| 95 | + { |
| 96 | + type: "pcb_courtyard_rect", |
| 97 | + pcb_courtyard_rect_id: "courtyard2", |
| 98 | + center: { x: 0, y: 0 }, |
| 99 | + width: 3, |
| 100 | + height: 3, |
| 101 | + layer: "bottom", |
| 102 | + stroke_width: 0.1, |
| 103 | + pcb_component_id: "comp1", |
| 104 | + }, |
| 105 | + ] |
| 106 | + |
| 107 | + const tscircuit = convertCircuitJsonToTscircuit(circuitJson, { |
| 108 | + componentName: "ComponentWithMultipleCourtyards", |
| 109 | + }) |
| 110 | + |
| 111 | + expect(tscircuit).toMatchInlineSnapshot(` |
| 112 | + "import { type ChipProps } from "tscircuit" |
| 113 | + export const ComponentWithMultipleCourtyards = (props: ChipProps) => ( |
| 114 | + <chip |
| 115 | + footprint={<footprint> |
| 116 | + <smtpad portHints={["1"]} pcbX="0mm" pcbY="0mm" width="1mm" height="1mm" shape="rect" /> |
| 117 | + <courtyardrect pcbX="0mm" pcbY="0mm" width="2.5mm" height="2.5mm" layer="top" /> |
| 118 | + <courtyardrect pcbX="0mm" pcbY="0mm" width="3mm" height="3mm" layer="bottom" /> |
| 119 | + </footprint>} |
| 120 | + {...props} |
| 121 | + /> |
| 122 | + )" |
| 123 | + `) |
| 124 | +}) |
| 125 | + |
| 126 | +test("test pcb_courtyard_rect conversion - offset courtyard", async () => { |
| 127 | + const circuitJson: AnyCircuitElement[] = [ |
| 128 | + { |
| 129 | + type: "pcb_plated_hole", |
| 130 | + pcb_plated_hole_id: "hole1", |
| 131 | + shape: "circle", |
| 132 | + outer_diameter: 1.5, |
| 133 | + hole_diameter: 0.8, |
| 134 | + x: -2.54, |
| 135 | + y: 0, |
| 136 | + layers: ["top", "bottom"], |
| 137 | + pcb_component_id: "comp1", |
| 138 | + pcb_port_id: "port1", |
| 139 | + port_hints: ["1"], |
| 140 | + }, |
| 141 | + { |
| 142 | + type: "pcb_plated_hole", |
| 143 | + pcb_plated_hole_id: "hole2", |
| 144 | + shape: "circle", |
| 145 | + outer_diameter: 1.5, |
| 146 | + hole_diameter: 0.8, |
| 147 | + x: 2.54, |
| 148 | + y: 0, |
| 149 | + layers: ["top", "bottom"], |
| 150 | + pcb_component_id: "comp1", |
| 151 | + pcb_port_id: "port2", |
| 152 | + port_hints: ["2"], |
| 153 | + }, |
| 154 | + { |
| 155 | + type: "pcb_courtyard_rect", |
| 156 | + pcb_courtyard_rect_id: "courtyard1", |
| 157 | + center: { x: 0, y: 0 }, |
| 158 | + width: 6.5, |
| 159 | + height: 3.5, |
| 160 | + layer: "top", |
| 161 | + stroke_width: 0.1, |
| 162 | + pcb_component_id: "comp1", |
| 163 | + }, |
| 164 | + ] |
| 165 | + |
| 166 | + const tscircuit = convertCircuitJsonToTscircuit(circuitJson, { |
| 167 | + componentName: "ComponentWithOffsetCourtyard", |
| 168 | + }) |
| 169 | + |
| 170 | + expect(tscircuit).toMatchInlineSnapshot(` |
| 171 | + "import { type ChipProps } from "tscircuit" |
| 172 | + export const ComponentWithOffsetCourtyard = (props: ChipProps) => ( |
| 173 | + <chip |
| 174 | + footprint={<footprint> |
| 175 | + <platedhole portHints={["1"]} pcbX="-2.54mm" pcbY="0mm" outerDiameter="1.5mm" holeDiameter="0.8mm" shape="circle" /> |
| 176 | + <platedhole portHints={["2"]} pcbX="2.54mm" pcbY="0mm" outerDiameter="1.5mm" holeDiameter="0.8mm" shape="circle" /> |
| 177 | + <courtyardrect pcbX="0mm" pcbY="0mm" width="6.5mm" height="3.5mm" layer="top" /> |
| 178 | + </footprint>} |
| 179 | + {...props} |
| 180 | + /> |
| 181 | + )" |
| 182 | + `) |
| 183 | +}) |
| 184 | + |
| 185 | +test("test pcb_courtyard_rect conversion - courtyard without layer defaults to top", async () => { |
| 186 | + const circuitJson: AnyCircuitElement[] = [ |
| 187 | + { |
| 188 | + type: "pcb_smtpad", |
| 189 | + pcb_smtpad_id: "pad1", |
| 190 | + shape: "rect", |
| 191 | + x: 0, |
| 192 | + y: 0, |
| 193 | + width: 1, |
| 194 | + height: 1, |
| 195 | + layer: "top", |
| 196 | + pcb_component_id: "comp1", |
| 197 | + pcb_port_id: "port1", |
| 198 | + port_hints: ["1"], |
| 199 | + }, |
| 200 | + { |
| 201 | + type: "pcb_courtyard_rect", |
| 202 | + pcb_courtyard_rect_id: "courtyard1", |
| 203 | + center: { x: 0, y: 0 }, |
| 204 | + width: 2, |
| 205 | + height: 2, |
| 206 | + layer: "top", |
| 207 | + stroke_width: 0.1, |
| 208 | + pcb_component_id: "comp1", |
| 209 | + }, |
| 210 | + ] |
| 211 | + |
| 212 | + const tscircuit = convertCircuitJsonToTscircuit(circuitJson, { |
| 213 | + componentName: "ComponentWithDefaultLayerCourtyard", |
| 214 | + }) |
| 215 | + |
| 216 | + expect(tscircuit).toMatchInlineSnapshot(` |
| 217 | + "import { type ChipProps } from "tscircuit" |
| 218 | + export const ComponentWithDefaultLayerCourtyard = (props: ChipProps) => ( |
| 219 | + <chip |
| 220 | + footprint={<footprint> |
| 221 | + <smtpad portHints={["1"]} pcbX="0mm" pcbY="0mm" width="1mm" height="1mm" shape="rect" /> |
| 222 | + <courtyardrect pcbX="0mm" pcbY="0mm" width="2mm" height="2mm" layer="top" /> |
| 223 | + </footprint>} |
| 224 | + {...props} |
| 225 | + /> |
| 226 | + )" |
| 227 | + `) |
| 228 | +}) |
| 229 | + |
0 commit comments