imrishabh18/rp2040-motor-controller

Bottom-side PCB silkscreen artwork implementing mirrored tsCircuit logos as embedded SVG paths, with via-clearance-aware placement and no active electronics.

Version
1.0.57
License
unset
Stars
5

tests/schematic-style.test.tsx

import { descriptions } from "../schematic/ChipDescriptions";
import { expect, test } from "bun:test";
import { Circuit } from "tscircuit";
import { analyzeSchematicPlacement } from "../vendor/schematic-style-analysis";
import Board from "../index.circuit";

test("automatic schematic refinement clears all style issues on all sheets", async () => {
	const circuit = new Circuit({ platform: { pcbDisabled: true } });
	circuit.add(<Board routingDisabled />);
	await circuit.renderUntilSettled();
	const json = circuit.getCircuitJson();
	expect(json.filter((e) => e.type === "schematic_net_label" && !e.symbol_name)).toEqual([]);
	const traceLabelKeys = json
		.filter((e: any) => e.type === "schematic_text" && e.source_trace_id)
		.map((e: any) =>
			JSON.stringify([
				e.source_trace_id,
				e.schematic_sheet_id,
				e.text,
				e.position,
				e.anchor,
				e.rotation,
			]),
		);
	expect(new Set(traceLabelKeys).size).toBe(traceLabelKeys.length);
	const headings = json.filter(
		(e: any) =>
			e.type === "schematic_text" &&
			!e.schematic_component_id &&
			!e.schematic_symbol_id &&
			!e.source_trace_id,
	);
	const headingKeys = headings.map(
		(e: any) => `${e.schematic_sheet_id}:${e.text}`,
	);
	expect(new Set(headingKeys).size).toBe(headingKeys.length);
	expect(
		headings.filter((e: any) => e.text === "Current-sense Supply Bypass"),
	).toHaveLength(1);
	const alarmSheet = json
		.filter((e) => e.type === "schematic_sheet")
		.find((e) => e.name === "position_alarm")!;
	const pwmLabel = json.find(
		(e) =>
			e.type === "schematic_text" &&
			e.source_trace_id &&
			e.text === "BUZZER_PWM" &&
			e.schematic_sheet_id === alarmSheet.schematic_sheet_id,
	);
	if (pwmLabel?.type !== "schematic_text") throw new Error("Missing inline BUZZER_PWM label");
	expect(pwmLabel.anchor).toBe("bottom_right");
	const { x: labelRight, y: labelY } = pwmLabel.position;
	const labelLeft = labelRight - pwmLabel.text.length * pwmLabel.font_size * 2 / 3;
  for (const divider of json.filter((e: any) => e.type === "schematic_line" && e.is_dashed &&
    e.schematic_sheet_id === alarmSheet.schematic_sheet_id && e.x1 === e.x2 &&
    Math.min(e.y1, e.y2) < labelY + pwmLabel.font_size + 0.2 && Math.max(e.y1, e.y2) > labelY - 0.2) as any[]) {
    expect(divider.x1 < labelLeft - 0.2 || divider.x1 > labelRight + 0.2).toBe(true);
  }

	const text = json
		.filter((e) => e.type === "schematic_text")
		.map((e) => e.text)
		.join(" ");
	for (const description of descriptions)
		for (const line of description.lines) expect(text).toContain(line);
	for (const heading of [
		"RP2040 & Power",
		"Clock",
		"Controls, Status & SWD",
		"QSPI Flash",
		"3.3 V Supply",
		"Programming USB-C",
		"USB-C PD Negotiation",
		"Power Filtering",
		"Temperature Sensing & MCU Alert",
		"RP2040-selectable USB-PD voltage",
	])
		expect(text).toContain(heading);
	expect(text).toContain(
		"GPIO4/5/6: 000 = 5 V; 111 = 9 V; 110 = 12 V; 100 = 15 V; 101 = 20 V.",
	);
	expect(json.filter((e) => e.type === "schematic_sheet")).toHaveLength(10);
	for (const sheet of json.filter((e) => e.type === "schematic_sheet")) {
		expect(sheet.sheet_width).toBe(297);
		expect(sheet.sheet_height).toBe(210);
	}
	expect(
		json.some(
			(e) => e.type === "schematic_sheet" && e.name === "controller_controls",
		),
	).toBe(true);

	expect(analyzeSchematicPlacement(json).getIssues()).toEqual([]);
	expect(
		json.filter((e) => e.type === "schematic_component_styling_warning"),
	).toEqual([]);
	for (const color of ["R", "G", "B"]) {
		const name = `Q_STATUS_${color}`;
		const source = json.find(
			(e: any) => e.type === "source_component" && e.name === name,
		) as any;
		const component = json.find(
			(e: any) => e.type === "schematic_component" &&
				e.source_component_id === source?.source_component_id,
		) as any;
		const labels = json.filter((e: any) => e.type === "schematic_text" &&
				e.text === name &&
				e.schematic_component_id === component?.schematic_component_id) as any[];
		expect(labels).toHaveLength(1);
		expect(Math.hypot(
			labels[0].position.x - component.center.x,
			labels[0].position.y - component.center.y,
		)).toBeLessThan(1.5);
	}
	expect(
		json.filter(
			(e) =>
				e.type.endsWith("_error") ||
				e.type === "schematic_element_outside_sheet_warning",
		),
	).toEqual([]);
	// Rerouting must still connect the design, not merely clear the visible drawing.
	expect(
		json.filter((e) => e.type === "schematic_trace").length,
	).toBeGreaterThan(100);
	for (const trace of json.filter((e) => e.type === "schematic_trace")) {
		for (let i = 0; i < trace.edges.length; i++) {
			const edge = trace.edges[i]!;
			expect(
				[edge.from.x, edge.from.y, edge.to.x, edge.to.y].every(Number.isFinite),
			).toBe(true);
			if (i > 0) {
				const previous = trace.edges[i - 1]!;
				expect(edge.from.x).toBeCloseTo(previous.to.x, 6);
				expect(edge.from.y).toBeCloseTo(previous.to.y, 6);
			}
		}
	}

	// Keep the adjacent DRV8825 reference pins joined by a short local wire.
	const referencePins = json.filter((e) => e.type === "schematic_port" &&
		(e.display_pin_label === "AVREF" || e.display_pin_label === "BVREF"));
	expect(referencePins).toHaveLength(2);
	const [avref, bvref] = referencePins;
	if (avref?.type !== "schematic_port" || bvref?.type !== "schematic_port")
		throw new Error("Missing DRV8825 reference pins");
	const samePoint = (a: { x: number; y: number }, b: { x: number; y: number }) =>
		Math.hypot(a.x - b.x, a.y - b.y) < 1e-6;
	const bridge = json.find((e) => {
		if (e.type !== "schematic_trace") return false;
		const start = e.edges[0]?.from;
		const end = e.edges.at(-1)?.to;
		return start && end && (
			(samePoint(start, avref.center) && samePoint(end, bvref.center)) ||
			(samePoint(start, bvref.center) && samePoint(end, avref.center))
		);
	});
	if (bridge?.type !== "schematic_trace") throw new Error("Missing DRV8825 reference wire");
	const topOfBridge = Math.max(...bridge.edges.flatMap(({ from, to }) => [from.y, to.y]));
	expect(topOfBridge - Math.max(avref.center.y, bvref.center.y)).toBeLessThan(0.25);
}, 120000);