|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import { convertCircuitJsonToTscircuit } from "lib/index" |
| 3 | +import type { AnyCircuitElement } from "circuit-json" |
| 4 | +import { runTscircuitCode } from "tscircuit" |
| 5 | +import { convertCircuitJsonToSchematicSvg } from "circuit-to-svg" |
| 6 | + |
| 7 | +const circuitJson: AnyCircuitElement[] = [ |
| 8 | + { |
| 9 | + type: "schematic_arc", |
| 10 | + center: { x: 0, y: 0 }, |
| 11 | + schematic_arc_id: "schematic_arc_id_1", |
| 12 | + radius: 1, |
| 13 | + schematic_component_id: "schematic_component_id_1", |
| 14 | + start_angle_degrees: 0, |
| 15 | + end_angle_degrees: 180, |
| 16 | + stroke_width: 0.05, |
| 17 | + color: "black", |
| 18 | + is_dashed: false, |
| 19 | + direction: "counterclockwise", |
| 20 | + }, |
| 21 | + { |
| 22 | + type: "schematic_line", |
| 23 | + x1: 2, |
| 24 | + schematic_line_id: "schematic_line_id_1", |
| 25 | + schematic_component_id: "schematic_component_id_1", |
| 26 | + y1: 0, |
| 27 | + x2: 4, |
| 28 | + y2: 0, |
| 29 | + stroke_width: 0.05, |
| 30 | + color: "black", |
| 31 | + is_dashed: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + type: "schematic_box", |
| 35 | + schematic_component_id: "schematic_component_id_1", |
| 36 | + x: 0, |
| 37 | + y: 3, |
| 38 | + width: 2, |
| 39 | + height: 2, |
| 40 | + is_dashed: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + type: "schematic_path", |
| 44 | + schematic_path_id: "schematic_path_id_1", |
| 45 | + points: [ |
| 46 | + { x: 3, y: 3 }, |
| 47 | + { x: 4, y: 4 }, |
| 48 | + ], |
| 49 | + fill_color: "blue", |
| 50 | + }, |
| 51 | + { |
| 52 | + type: "schematic_text", |
| 53 | + schematic_text_id: "schematic_text_id_1", |
| 54 | + text: "U1", |
| 55 | + position: { x: 0, y: -3 }, |
| 56 | + anchor: "center", |
| 57 | + font_size: 0.2, |
| 58 | + color: "red", |
| 59 | + rotation: 45, |
| 60 | + }, |
| 61 | + { |
| 62 | + type: "schematic_circle", |
| 63 | + center: { x: 3, y: -3 }, |
| 64 | + schematic_circle_id: "schematic_circle_id_1", |
| 65 | + radius: 0.5, |
| 66 | + stroke_width: 0.05, |
| 67 | + color: "green", |
| 68 | + is_filled: true, |
| 69 | + is_dashed: true, |
| 70 | + }, |
| 71 | +] |
| 72 | + |
| 73 | +test("test7 comprehensive schematic symbol support", async () => { |
| 74 | + const tscircuit = convertCircuitJsonToTscircuit(circuitJson, { |
| 75 | + componentName: "U1", |
| 76 | + }) |
| 77 | + |
| 78 | + expect(tscircuit).toMatchInlineSnapshot(` |
| 79 | +"import { type ChipProps } from "tscircuit" |
| 80 | +export const U1 = (props: ChipProps) => ( |
| 81 | + <chip |
| 82 | + symbol={<symbol> |
| 83 | + <schematicarc |
| 84 | + center={{ x: 0, y: 0 }} |
| 85 | + radius={1} |
| 86 | + startAngleDegrees={0} |
| 87 | + endAngleDegrees={180} |
| 88 | + strokeWidth={0.05} |
| 89 | + color="black" |
| 90 | + isDashed={false} |
| 91 | + direction="counterclockwise" |
| 92 | + /> |
| 93 | + <schematicline x1={2} y1={0} x2={4} y2={0} strokeWidth={0.05} color="black" isDashed={false}/> |
| 94 | + <schematicbox center={{ x: 0, y: 3 }} width={2} height={2} isDashed={true}/> |
| 95 | + <schematicpath points={[{"x":3,"y":3},{"x":4,"y":4}]} strokeColor="blue" fillColor="blue" isFilled={false}/> |
| 96 | + <schematictext text="U1" x={0} y={-3} anchorAlignment="center" fontSize={0.2} color="red" rotation={45} /> |
| 97 | + <schematiccircle center={{ x: 3, y: -3 }} radius={0.5} strokeWidth={0.05} color="green" isFilled={true} isDashed={true} /> |
| 98 | +</symbol>} |
| 99 | + {...props} |
| 100 | + /> |
| 101 | +)" |
| 102 | +`) |
| 103 | + const result = await runTscircuitCode(tscircuit) |
| 104 | + |
| 105 | + expect(Array.isArray(result)).toBe(true) |
| 106 | + expect(result).not.toHaveLength(0) |
| 107 | +}, 15000) |
| 108 | + |
| 109 | +test("test7 comprehensive schematic symbol support svg", async () => { |
| 110 | + const tscircuit = convertCircuitJsonToTscircuit(circuitJson, { |
| 111 | + componentName: "U1", |
| 112 | + }) |
| 113 | + |
| 114 | + const result = await runTscircuitCode(tscircuit) |
| 115 | + |
| 116 | + const schematicSvg = convertCircuitJsonToSchematicSvg( |
| 117 | + result as AnyCircuitElement[], |
| 118 | + ) |
| 119 | + await expect(schematicSvg).toMatchSvgSnapshot( |
| 120 | + import.meta.path, |
| 121 | + "comprehensive-schematic-symbol", |
| 122 | + ) |
| 123 | +}) |
0 commit comments