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

132 lines
4.2 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { analyzeStructuralStability } from "@/lib/fxSystem";
import { executeSimulationTick } from "@/components/snapcad/circuits/simulationEngine";
import type { CircuitComponent } from "@/components/snapcad/circuits/types";
import type { WorkplaneShape } from "@/types/sketchforge";
describe("FX & Structural Stability System", () => {
it("evaluates a well-proportioned architectural structure as structurally sound", () => {
// A stable, wide base building (width 80, depth 60, height 40) at elevation 0
const shapes: WorkplaneShape[] = [
{
id: "base1",
name: "Temel ve Zemin Kat",
kind: "box",
color: "#cbd5e1",
x: 0,
z: 0,
elevation: 0,
size: 80,
width: 80,
depth: 60,
height: 40,
rotation: 0,
},
{
id: "roof1",
name: "Çatı Katı",
kind: "roof",
color: "#b91c1c",
x: 0,
z: 0,
elevation: 40,
size: 70,
width: 70,
depth: 50,
height: 20,
rotation: 0,
},
];
const result = analyzeStructuralStability(shapes);
expect(result.passed).toBe(true);
expect(result.score).toBeGreaterThanOrEqual(60);
expect(result.slendernessRatio).toBeLessThanOrEqual(2.5);
expect(result.strengths.length).toBeGreaterThan(0);
expect(result.flaws.length).toBe(0);
});
it("fails a dangerously slender tower under earthquake simulation", () => {
// A needle-like tower: width 10, depth 10, height 120 (slenderness = 12)
const shapes: WorkplaneShape[] = [
{
id: "tower1",
name: "Narin Kule",
kind: "box",
color: "#475569",
x: 0,
z: 0,
elevation: 0,
size: 10,
width: 10,
depth: 10,
height: 120,
rotation: 0,
},
];
const result = analyzeStructuralStability(shapes);
expect(result.passed).toBe(false);
expect(result.score).toBeLessThan(60);
expect(result.slendernessRatio).toBeGreaterThan(4.2);
expect(result.flaws.some((f) => f.includes("Narin"))).toBe(true);
expect(result.recommendations.length).toBeGreaterThan(0);
});
it("detects floating buildings with no ground foundation contact", () => {
const shapes: WorkplaneShape[] = [
{
id: "floating1",
name: "Havada Asılı Yapı",
kind: "box",
color: "#38bdf8",
x: 0,
z: 0,
elevation: 35, // Floating 35mm in the air!
size: 30,
width: 30,
depth: 30,
height: 30,
rotation: 0,
},
];
const result = analyzeStructuralStability(shapes);
expect(result.passed).toBe(false);
expect(result.flaws.some((f) => f.includes("Temel Bağlantısı Eksik"))).toBe(true);
});
it("handles empty scenes safely", () => {
const result = analyzeStructuralStability([]);
expect(result.passed).toBe(false);
expect(result.score).toBe(0);
});
});
describe("Circuits Studio - Short Circuit & Explosion Detection", () => {
it("detects over-current when LED is connected to 9V battery with no resistor", () => {
const components: CircuitComponent[] = [
{ id: "bat1", kind: "battery_9v", name: "9V Pil", x: 0, y: 0, batteryVoltage: 9 },
{ id: "led1", kind: "led", name: "Kırmızı LED", x: 100, y: 0, ledColor: "red" },
{ id: "sw1", kind: "slide_switch", name: "Anahtar", x: 50, y: 0, switchOn: true },
];
// No resistor in the circuit!
const result = executeSimulationTick(1, "// Dirençsiz devre", components);
expect(result.shortCircuit).toBe(true);
expect(result.shortCircuitReason).toMatch(/direnç/i);
expect(result.explodedComponentId).toBe("led1");
expect(result.logLines.some((l) => l.includes("KRİTİK ALARM"))).toBe(true);
});
it("detects code-triggered short circuit tests", () => {
const components: CircuitComponent[] = [
{ id: "uno1", kind: "arduino_uno", name: "Uno", x: 0, y: 0 },
];
const code = "// KISA_DEVRE_TESTI: VCC ile GND temas ettirildi";
const result = executeSimulationTick(1, code, components);
expect(result.shortCircuit).toBe(true);
expect(result.shortCircuitReason).toContain("Kısa Devre");
});
});