Files
snapcad-studio/tests/unit/circuitsStudio.test.ts
T

175 lines
7.3 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { compileBlocksToCpp, DEFAULT_BLOCKS } from "@/components/snapcad/circuits/blocksEngine";
import {
COMPONENTS_CATALOG,
COMPONENT_CATEGORIES,
getPinsForComponent,
} from "@/components/snapcad/circuits/componentsCatalog";
import { ARDUINO_LIBRARIES } from "@/components/snapcad/circuits/librariesData";
import { executeSimulationTick } from "@/components/snapcad/circuits/simulationEngine";
import { CIRCUIT_TEMPLATES } from "@/components/snapcad/circuits/templates";
import type { CircuitComponent } from "@/components/snapcad/circuits/types";
describe("Circuits Studio - Components Catalog & Studio Parity", () => {
it("contains all essential circuit components", () => {
const keys = Object.keys(COMPONENTS_CATALOG);
expect(keys).toContain("arduino_uno");
expect(keys).toContain("arduino_nano");
expect(keys).toContain("esp32");
expect(keys).toContain("stepper_motor");
expect(keys).toContain("dht11");
expect(keys).toContain("joystick");
expect(keys).toContain("oscilloscope");
expect(keys).toContain("breadboard");
expect(keys).toContain("battery_9v");
expect(keys).toContain("rgb_led");
expect(keys).toContain("seven_segment");
expect(keys).toContain("servo");
expect(keys).toContain("dc_motor");
expect(keys).toContain("pir_sensor");
expect(keys).toContain("ultrasonic");
expect(keys).toContain("lcd16x2");
expect(keys).toContain("oled128x64");
expect(keys).toContain("relay");
expect(keys).toContain("keypad4x4");
expect(keys.length).toBeGreaterThanOrEqual(34);
});
it("provides valid categories with descriptions", () => {
expect(COMPONENT_CATEGORIES.length).toBe(6);
for (const cat of COMPONENT_CATEGORIES) {
expect(cat.label).toBeTruthy();
}
});
it("calculates transformed pin coordinates when component is rotated", () => {
const defaultUno: CircuitComponent = {
id: "uno1",
kind: "arduino_uno",
name: "Uno",
x: 100,
y: 100,
rotation: 0,
};
const rot0Pins = getPinsForComponent(defaultUno);
const d13Pin0 = rot0Pins.find((p) => p.id === "D13");
expect(d13Pin0).toBeDefined();
const rot90Uno: CircuitComponent = {
...defaultUno,
rotation: 90,
};
const rot90Pins = getPinsForComponent(rot90Uno);
const d13Pin90 = rot90Pins.find((p) => p.id === "D13");
expect(d13Pin90).toBeDefined();
expect(d13Pin90?.relX).not.toBe(d13Pin0?.relX);
});
});
describe("Circuits Studio - Arduino Simulation Engine", () => {
it("activates danger alarm pins when ultrasonic distance is under 25cm", () => {
const components: CircuitComponent[] = [
{ id: "uno1", kind: "arduino_uno", name: "Uno", x: 0, y: 0 },
{ id: "ult1", kind: "ultrasonic", name: "Sonar", x: 100, y: 100, distanceCm: 15 },
];
const code = "if (distanceCm < 25) { digitalWrite(13, HIGH); tone(8, 880); }";
const result = executeSimulationTick(1, code, components);
expect(result.buzzerActive).toBe(true);
expect(result.activePins[13]?.digital).toBe(true);
expect(result.activePins[8]?.digital).toBe(true);
expect(result.plotterValues["Mesafe_cm"]).toBe(15);
});
it("calculates servo angle from potentiometer analog values", () => {
const components: CircuitComponent[] = [
{ id: "uno1", kind: "arduino_uno", name: "Uno", x: 0, y: 0 },
{ id: "pot1", kind: "potentiometer", name: "Pot", x: 100, y: 100, potValue: 1023 },
{ id: "srv1", kind: "servo", name: "Servo", x: 200, y: 100, servoAngle: 0 },
];
const code = "myServo.write(aci);";
const result = executeSimulationTick(1, code, components);
expect(result.componentsPatch["srv1"]?.servoAngle).toBe(180);
expect(result.plotterValues["Servo_Aci"]).toBe(180);
});
it("simulates pure DC battery circuits without microcontroller", () => {
const components: CircuitComponent[] = [
{ id: "bat1", kind: "battery_9v", name: "9V Pil", x: 0, y: 0, batteryVoltage: 9 },
{ id: "sw1", kind: "slide_switch", name: "Anahtar", x: 50, y: 0, switchOn: true },
];
const code = "// Pil devresi";
const result = executeSimulationTick(1, code, components);
expect(result.activePins[13]?.voltage).toBe(9);
expect(result.plotterValues["V_Bat"]).toBe(9);
});
it("simulates stepper motor step rotation and ULN2003 coil switching", () => {
const components: CircuitComponent[] = [
{ id: "nano1", kind: "arduino_nano", name: "Nano", x: 0, y: 0 },
{ id: "stp1", kind: "stepper_motor", name: "28BYJ-48", x: 100, y: 100, stepperAngle: 0, stepperCoils: 0 },
];
const code = "stepper.step(1);";
const result = executeSimulationTick(1, code, components);
expect(result.componentsPatch["stp1"]?.stepperAngle).toBe(22.5);
expect(result.componentsPatch["stp1"]?.stepperCoils).toEqual([false, true, false, false]);
expect(result.activePins[9]?.digital).toBe(true);
expect(result.plotterValues["Step_Aci"]).toBe(22.5);
});
it("simulates joystick coordinate tracking and servo steering", () => {
const components: CircuitComponent[] = [
{ id: "esp1", kind: "esp32", name: "ESP32", x: 0, y: 0 },
{ id: "joy1", kind: "joystick", name: "Joystick", x: 100, y: 100, joystickX: 900, joystickY: 512, joystickPressed: false },
{ id: "srv1", kind: "servo", name: "Servo", x: 200, y: 100, servoAngle: 90 },
];
const code = "servo.write(map(analogRead(A0), 0, 1023, 0, 180));";
const result = executeSimulationTick(1, code, components);
expect(result.componentsPatch["srv1"]?.servoAngle).toBe(158);
expect(result.plotterValues["Joy_X"]).toBe(900);
});
});
describe("Circuits Studio - Block Coding & C++ Compiler", () => {
it("compiles visual blocks into valid Arduino C++ setup and loop syntax", () => {
const code = compileBlocksToCpp(DEFAULT_BLOCKS);
expect(code).toContain("void setup()");
expect(code).toContain("void loop()");
expect(code).toContain("pinMode(13, OUTPUT);");
expect(code).toContain("digitalWrite(13, HIGH);");
expect(code).toContain("delay(1000);");
});
});
describe("Circuits Studio - Arduino Libraries & Templates", () => {
it("includes all requested standard Arduino libraries with documentation", () => {
expect(ARDUINO_LIBRARIES.length).toBeGreaterThanOrEqual(12);
const headers = ARDUINO_LIBRARIES.map((l) => l.header);
expect(headers).toContain("<Servo.h>");
expect(headers).toContain("<LiquidCrystal_I2C.h>");
expect(headers).toContain("<Adafruit_NeoPixel.h>");
expect(headers).toContain("<DHT.h>");
expect(headers).toContain("<WiFi.h>");
expect(headers).toContain("<SPI.h>");
expect(headers).toContain("<AccelStepper.h>");
expect(headers).toContain("<PubSubClient.h>");
});
it("contains rich working project templates", () => {
expect(CIRCUIT_TEMPLATES.length).toBeGreaterThanOrEqual(14);
const ids = CIRCUIT_TEMPLATES.map((t) => t.id);
expect(ids).toContain("stepper-motor-uln2003");
expect(ids).toContain("dht11-weather-station");
expect(ids).toContain("joystick-servo-control");
expect(ids).toContain("esp32-iot-smart-device");
for (const tpl of CIRCUIT_TEMPLATES) {
expect(tpl.components.length).toBeGreaterThan(0);
expect(tpl.code).toContain("void setup");
expect(tpl.code).toContain("void loop");
}
});
});