Repository navigation
Expand file tree
/
Copy pathplugin-contract.test.ts
More file actions
611 lines (563 loc) · 24.3 KB
/
Copy pathplugin-contract.test.ts
File metadata and controls
611 lines (563 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import { describe, expect, test } from 'bun:test'
import { type AnyNode, type GeometryContext, loadPlugin, nodeRegistry } from '@pascal-app/core'
import { Box3 } from 'three'
import { electricalAgentOperations } from '../agent-operations'
import { electricalAgentTools } from '../agent-tools'
import {
ELECTRICAL_PLUGIN_ID,
electricalChatExtension,
electricalHostPanel,
electricalPlugin,
electricalRenderPlugin,
electricalScheduleView,
} from '../index'
import { buildWiredPanelInteriorCad, ELECTRICAL_CAD_CATALOG, measureCadDimensionsMm } from '../cad'
import { buildElectricalTakeoff, buildPanelSchedule } from '../nec/panel-schedule'
import { findNextWallPlacement } from '../nec/autopopulate'
import type { SceneNodeLike } from '../nec/load-calc'
import {
buildElectricalConduitRunFloorplan,
buildElectricalConduitRunGeometry,
ElectricalConduitRunNode,
} from '../nodes/conduit-run'
import {
buildElectricalDeviceFloorplan,
buildElectricalDeviceGeometry,
ElectricalDeviceNode,
} from '../nodes/device'
import {
buildElectricalPanelFloorplan,
buildElectricalPanelGeometry,
ElectricalPanelNode,
} from '../nodes/panel'
import {
buildElectricalTransformerFloorplan,
buildElectricalTransformerGeometry,
ElectricalTransformerNode,
electricalTransformerRenderDefinition,
getElectricalTransformerPorts,
} from '../nodes/transformer'
const dummyCtx = {} as GeometryContext
describe('Pascal Plugin API v1 Conformance & Agent Operations (E-014 / E-016)', () => {
test('electricalPlugin & electricalRenderPlugin conform to Plugin v1 and register all 4 node kinds', async () => {
expect(electricalPlugin.id).toBe(ELECTRICAL_PLUGIN_ID)
expect(electricalPlugin.apiVersion).toBe(1)
expect(electricalRenderPlugin.id).toBe(ELECTRICAL_PLUGIN_ID)
expect(electricalRenderPlugin.apiVersion).toBe(1)
expect(electricalPlugin.nodes?.length).toBe(4)
expect(electricalRenderPlugin.nodes?.length).toBe(4)
expect(electricalHostPanel.pluginId).toBe(ELECTRICAL_PLUGIN_ID)
expect(electricalScheduleView.pluginId).toBe(ELECTRICAL_PLUGIN_ID)
expect(electricalChatExtension.pluginId).toBe(ELECTRICAL_PLUGIN_ID)
await loadPlugin(electricalPlugin)
expect(nodeRegistry.get('electrical:panel')).toBeDefined()
expect(nodeRegistry.get('electrical:device')).toBeDefined()
expect(nodeRegistry.get('electrical:conduit-run')).toBeDefined()
expect(nodeRegistry.get('electrical:transformer')).toBeDefined()
})
test('Every PluginAgentTool contract in ./agent-tools has a matching pure operation in ./agent-operations', () => {
expect(electricalAgentTools.pluginId).toBe(ELECTRICAL_PLUGIN_ID)
expect(electricalAgentTools.apiVersion).toBe(1)
const opNames = new Set(Object.keys(electricalAgentOperations))
for (const tool of electricalAgentTools.tools) {
expect(tool.name.startsWith('electrical_')).toBe(true)
if (tool.runsIn === 'operation') {
expect(opNames.has(tool.name)).toBe(true)
}
}
})
test('End-to-end pure agent operations place panel + device, route around plumbing & HVAC, and pass NEC audit', () => {
const nodes: Record<string, AnyNode> = {
level_0: {
object: 'node',
id: 'level_0',
type: 'level',
name: 'Level 0',
parentId: null,
visible: true,
metadata: {},
level: 0,
children: [],
} as unknown as AnyNode,
'pipe-segment_1': {
object: 'node',
id: 'pipe-segment_1',
type: 'pipe-segment',
parentId: 'level_0',
visible: true,
metadata: {},
diameter: 4,
path: [
[2, 2.55, -1],
[2, 2.55, 2],
],
} as unknown as AnyNode,
'duct-segment_1': {
object: 'node',
id: 'duct-segment_1',
type: 'duct-segment',
parentId: 'level_0',
visible: true,
metadata: {},
shape: 'rect',
width: 16,
height: 8,
path: [
[3, 2.55, -1],
[3, 2.55, 2],
],
} as unknown as AnyNode,
}
const ctx = { activeLevelId: 'level_0' }
const panelOut = electricalAgentOperations.electrical_place_panel(
nodes,
{ name: 'MDP-1', position: [0, 1.52, 0], occupancy: 'residential' },
ctx,
)
const panelNode = panelOut.changes!.create![0]!.node
nodes[panelNode.id] = panelNode
const devOut = electricalAgentOperations.electrical_place_device(
nodes,
{
name: 'Living Outlet',
position: [4.5, 0.38, 0],
deviceKind: 'afci',
panelId: panelNode.id,
circuitNumber: 1,
loadVa: 180,
},
ctx,
)
const devNode = devOut.changes!.create![0]!.node
nodes[devNode.id] = devNode
const routeOut = electricalAgentOperations.electrical_route_circuit(
nodes,
{ panelId: panelNode.id, targetDeviceId: devNode.id, racewayType: 'emt' },
ctx,
)
const runNode = routeOut.changes!.create![0]!.node
nodes[runNode.id] = runNode
expect((routeOut.result.avoidedObstacles as unknown[]).length).toBeGreaterThanOrEqual(2)
expect((routeOut.result.remainingClashes as unknown[]).length).toBe(0)
const auditOut = electricalAgentOperations.electrical_audit_nec(nodes)
expect(auditOut.result.passed).toBe(true)
expect(auditOut.result.errorCount).toBe(0)
const schedOut = electricalAgentOperations.electrical_generate_schedule(nodes, {})
expect(schedOut.result.panelCount).toBe(1)
const loadOut = electricalAgentOperations.electrical_calculate_load(nodes, {
fallbackSquareFootage: 2000,
})
expect(loadOut.result.minimumServiceAmps).toBe(100)
// BUG-03: Placing & routing a 50A continuous EVSE without manual wireGauge/tradeSize auto-sizes cleanly
const evseOut = electricalAgentOperations.electrical_place_device(
nodes,
{
name: 'Garage EVSE',
position: [6, 1.1, 0],
deviceKind: 'evse',
panelId: panelNode.id,
circuitNumber: 2,
poles: 2,
breakerAmps: 50,
loadVa: 9600,
continuous: true,
gfciProtected: true,
},
ctx,
)
const evseNode = evseOut.changes!.create![0]!.node
nodes[evseNode.id] = evseNode
const evseRoute = electricalAgentOperations.electrical_route_circuit(
nodes,
{ panelId: panelNode.id, targetDeviceId: evseNode.id, racewayType: 'nm-b' },
ctx,
)
const evseRun = evseRoute.changes!.create![0]!.node
nodes[evseRun.id] = evseRun
// 40A * 1.25 = 50A required -> NM-B 60°C cap requires #6 Cu (55A)
expect(evseRoute.result.wireGauge).toBe('6')
const auditAfterEvse = electricalAgentOperations.electrical_audit_nec(nodes)
expect(auditAfterEvse.result.passed).toBe(true)
// OPT-02: Commercial step-down transformer placement operation creates electrical:transformer with NEC 450.3(B) sizing
const txOut = electricalAgentOperations.electrical_place_transformer(
nodes,
{
name: 'TX-1 (75 kVA)',
position: [3, 0, -2],
kvaRating: 75,
primaryVoltage: 480,
secondaryVoltage: 208,
fedFromPanelId: panelNode.id,
},
ctx,
)
const txNode = txOut.changes!.create![0]!.node
nodes[txNode.id] = txNode
expect(String(txNode.type)).toBe('electrical:transformer')
expect(txOut.result.maxPrimaryOcpdAmps).toBe(125)
expect(txOut.result.maxSecondaryOcpdAmps).toBe(300)
})
test('All 4 node 3D geometry and 2D floorplan builders produce valid Three.js & Floorplan outputs', () => {
const panel = ElectricalPanelNode.parse({ id: 'elecpanel_test', type: 'electrical:panel' })
const pGeo = buildElectricalPanelGeometry(panel, dummyCtx)
const pPlan = buildElectricalPanelFloorplan(panel, dummyCtx)
expect(pGeo.children.length).toBeGreaterThan(2)
expect(pPlan.kind).toBe('group')
const dev = ElectricalDeviceNode.parse({ id: 'elecdev_test', type: 'electrical:device' })
const dGeo = buildElectricalDeviceGeometry(dev, dummyCtx)
const dPlan = buildElectricalDeviceFloorplan(dev, dummyCtx)
expect(dGeo.children.length).toBeGreaterThan(0)
expect(dPlan.kind).toBe('group')
const run = ElectricalConduitRunNode.parse({
id: 'eleccond_test',
type: 'electrical:conduit-run',
path: [
[0, 1.5, 0],
[0, 2.5, 0],
[3, 2.5, 0],
[3, 0.4, 0],
],
})
const rGeo = buildElectricalConduitRunGeometry(run, dummyCtx)
const rPlan = buildElectricalConduitRunFloorplan(run, dummyCtx)
expect(rGeo.children.length).toBeGreaterThanOrEqual(3) // swept filleted tube + start/end box connectors + straps
expect(rGeo.children.some((c) => c.name === 'conduit-tube')).toBe(true)
expect(rPlan.kind).toBe('group')
const tx = ElectricalTransformerNode.parse({ id: 'electx_test', type: 'electrical:transformer' })
const tGeo = buildElectricalTransformerGeometry(tx, dummyCtx)
const tPlan = buildElectricalTransformerFloorplan(tx, dummyCtx)
expect(tGeo.children.length).toBeGreaterThan(2)
expect(tPlan.kind).toBe('group')
// BUG-04: Rotated 90° transformer rotates ports & coil circles onto Z axis and keys geometry by primaryVoltage
const txRot = ElectricalTransformerNode.parse({
id: 'electx_rot',
type: 'electrical:transformer',
position: [5, 0, 10],
rotation: Math.PI / 2,
primaryVoltage: 240,
secondaryVoltage: 120,
})
const ports = getElectricalTransformerPorts(txRot)
expect(ports[0]!.position[0]).toBeCloseTo(5, 5)
expect(ports[0]!.position[2]).toBeGreaterThan(10.1)
expect(ports[1]!.position[2]).toBeLessThan(9.9)
expect(electricalTransformerRenderDefinition.geometryKey!(tx)).not.toBe(
electricalTransformerRenderDefinition.geometryKey!(txRot),
)
})
test('All 50 clean-room electrical CAD material models (panels, solar/ESS, boxes, smart devices, fixtures, conduit/grounding) build valid 3D meshes', () => {
expect(ELECTRICAL_CAD_CATALOG.length).toBe(50)
const seen = new Set<string>()
const categories = new Set<string>()
for (const entry of ELECTRICAL_CAD_CATALOG) {
expect(seen.has(entry.slug)).toBe(false)
seen.add(entry.slug)
categories.add(entry.category)
const group = entry.build()
expect(group.children.length).toBeGreaterThan(0)
const [wMm, hMm, dMm] = measureCadDimensionsMm(group)
expect(wMm).toBeGreaterThan(0)
expect(hMm).toBeGreaterThan(0)
expect(dMm).toBeGreaterThan(0)
}
expect(categories.size).toBe(6)
})
test('Cover-off wired panel interior builder generates exposed busbars, breakers, feeders, and individual branch wires with circuit metadata', () => {
const panel = ElectricalPanelNode.parse({
id: 'elecpanel_wired',
type: 'electrical:panel',
name: 'MDP-1',
voltageSystem: '120/240V-1P',
spaces: 12,
coverRemoved: true,
})
const openInScene = buildElectricalPanelGeometry(panel, dummyCtx)
expect(openInScene.children.length).toBeGreaterThan(0)
const sched = buildPanelSchedule(panel, [
{
id: 'd1',
type: 'electrical:device',
name: 'EVSE 50A',
panelId: 'elecpanel_wired',
circuitNumber: 1,
poles: 2,
breakerAmps: 50,
loadVa: 9600,
wireGauge: '6',
},
{
id: 'd2',
type: 'electrical:device',
name: 'Kitchen GFCI',
panelId: 'elecpanel_wired',
circuitNumber: 2,
poles: 1,
breakerAmps: 20,
loadVa: 1500,
wireGauge: '12',
gfciProtected: true,
},
])
const wired = buildWiredPanelInteriorCad({ schedule: sched, viewMode: 'cover-off' })
const kinds = new Set<string>()
wired.traverse((obj) => {
const k = (obj.userData as { kind?: string } | undefined)?.kind
if (k) kinds.add(k)
})
expect(kinds.has('busbar')).toBe(true)
expect(kinds.has('feeder')).toBe(true)
expect(kinds.has('breaker')).toBe(true)
expect(kinds.has('wire-hot')).toBe(true)
expect(kinds.has('wire-neutral')).toBe(true)
expect(kinds.has('wire-ground')).toBe(true)
// Panel Cover On (deadfront) adds flush steel cover shields, trim, screws & spare twist-out blanks over cover-off interior
const coverOn = buildWiredPanelInteriorCad({ schedule: sched, viewMode: 'panel-cover-on' })
expect(coverOn.children.length).toBeGreaterThan(wired.children.length + 20)
// R2-BUG-02: Coincident / zero-length conduit path returns empty group safely without throwing NaN TubeGeometry
const degenerateRun = ElectricalConduitRunNode.parse({
id: 'eleccond_degen',
type: 'electrical:conduit-run',
path: [
[1, 1.5, 0],
[1, 1.5, 0],
],
})
const degenGeo = buildElectricalConduitRunGeometry(degenerateRun, dummyCtx)
expect(degenGeo.children).toHaveLength(0)
})
test('R4-BUG-01 & R4-OPT-01: Dense 42-space 3P panel keeps all gutter wires behind Panel Cover On and skips occluded cover meshes in closed mode', () => {
const panel42 = ElectricalPanelNode.parse({
id: 'elecpanel_42p',
type: 'electrical:panel',
name: 'LP-42',
occupancy: 'commercial',
voltageSystem: '120/208V-3P',
busRatingAmps: 400,
mainBreakerAmps: 400,
spaces: 42,
coverRemoved: true,
})
const devices: Array<Record<string, unknown>> = []
for (let ckt = 1; ckt <= 36; ckt++) {
devices.push({
id: `dev_${ckt}`,
type: 'electrical:device',
name: `Branch ${ckt}`,
panelId: 'elecpanel_42p',
circuitNumber: ckt,
poles: 1,
breakerAmps: 20,
loadVa: 1400,
wireGauge: '12',
})
}
// Add 3-pole 60A circuit at bottom of panel (high lane index + multi-pole)
devices.push({
id: 'dev_3p_bottom',
type: 'electrical:device',
name: 'RTU Chiller 3P',
panelId: 'elecpanel_42p',
circuitNumber: 37,
poles: 3,
breakerAmps: 60,
loadVa: 14000,
wireGauge: '6',
})
const sched = buildPanelSchedule(panel42, devices as never)
const depth = 0.145
const zCover = -depth / 2 + 0.004 + 0.062
const coverOn = buildWiredPanelInteriorCad({ schedule: sched, depth, viewMode: 'panel-cover-on' })
let wireMeshesChecked = 0
coverOn.updateMatrixWorld(true)
coverOn.traverse((obj) => {
const kind = (obj.userData as { kind?: string } | undefined)?.kind
if (kind === 'wire-hot' || kind === 'wire-neutral' || kind === 'wire-ground') {
const box = new Box3().setFromObject(obj)
expect(box.max.z).toBeLessThan(zCover - 0.002)
wireMeshesChecked += 1
}
})
expect(wireMeshesChecked).toBeGreaterThanOrEqual(80)
const closed = buildWiredPanelInteriorCad({ schedule: sched, depth, viewMode: 'closed' })
expect(closed.children.length).toBeLessThan(coverOn.children.length - 10)
})
test('R4-BUG-06 & R4-CON-02: electrical_place_device sizes 25A (#10) and 60A (#4 NM-B) per NEC 240.4(D) & electrical_route_circuit supports wallPlacement', () => {
const nodes: Record<string, AnyNode> = {
level_0: { object: 'node', id: 'level_0', type: 'level', name: 'L1', parentId: null, visible: true, metadata: {} } as unknown as AnyNode,
wall_n: { object: 'node', id: 'wall_n', type: 'wall', parentId: 'level_0', start: [0, 0], end: [6, 0], height: 2.5, thickness: 0.14 } as unknown as AnyNode,
}
const ctx = { activeLevelId: 'level_0' }
const pOut = electricalAgentOperations.electrical_place_panel(
nodes,
{ name: 'MDP-1', position: [0.5, 1.52, 0.08], occupancy: 'residential' },
ctx,
)
const panel = pOut.changes!.create![0]!.node
nodes[panel.id] = panel
const d25 = electricalAgentOperations.electrical_place_device(
nodes,
{ name: 'Compressor 25A', position: [2, 0.38, 0.08], breakerAmps: 25, poles: 2, loadVa: 4000, autoWire: false },
ctx,
)
expect((d25.changes!.create![0]!.node as unknown as { wireGauge: string }).wireGauge).toBe('10')
const d60 = electricalAgentOperations.electrical_place_device(
nodes,
{ name: 'Subfeed 100A', position: [4, 0.38, 0.08], breakerAmps: 100, poles: 2, loadVa: 19200, autoWire: false },
ctx,
)
const d60Node = d60.changes!.create![0]!.node
// 100A breaker (80A load) in NM-B (60°C column): #3 AWG Cu (85A) or larger, never capped at #6!
expect(['3', '2', '1']).toContain((d60Node as unknown as { wireGauge: string }).wireGauge)
nodes[d60Node.id] = d60Node
const surfaceRoute = electricalAgentOperations.electrical_route_circuit(
nodes,
{ panelId: panel.id, targetDeviceId: d60Node.id, wallPlacement: 'surface' },
ctx,
)
expect(surfaceRoute.result.conduitRunId).toBeTruthy()
const runNode = surfaceRoute.changes!.create![0]!.node as unknown as { path: [number, number, number][] }
// Surface mode preserves wall face Z offset (0.08) instead of pulling to centerline (0)
expect(Math.abs(runNode.path[0]![2])).toBeGreaterThan(0.04)
})
test('All 14 expanded CAD equipment & smart device kinds render 3D+2D, auto-wire cleanly, pass NEC audit, and price in BOM takeoff', () => {
const newKinds = [
'solar-inverter',
'battery-ess',
'transfer-switch',
'generator-inlet',
'rapid-shutdown',
'spd',
'vfd',
'dimmer',
'usb-duplex',
'weatherproof-gfci',
'floor-box',
'smoke-co',
'ceiling-fan',
'under-cabinet-led',
] as const
for (const kind of newKinds) {
const parsed = ElectricalDeviceNode.parse({
id: `elecdev_${kind.replace(/-/g, '_')}`,
type: 'electrical:device',
name: kind,
deviceKind: kind,
})
const geo = buildElectricalDeviceGeometry(parsed, dummyCtx)
expect(geo.children.length).toBeGreaterThan(0)
const fp = buildElectricalDeviceFloorplan(parsed, dummyCtx)
expect(fp.kind).toBe('group')
}
// End-to-end live placement + auto-wiring + BOM takeoff pricing for solar/ESS + smart devices
const nodes: Record<string, AnyNode> = {
level_0: { object: 'node', id: 'level_0', type: 'level', name: 'L1', parentId: null, height: 2.5, visible: true, metadata: {} } as unknown as AnyNode,
wall_n: { object: 'node', id: 'wall_n', type: 'wall', parentId: 'level_0', start: [0, 0], end: [6, 0], height: 2.5, thickness: 0.14 } as unknown as AnyNode,
wall_e: { object: 'node', id: 'wall_e', type: 'wall', parentId: 'level_0', start: [6, 0], end: [6, 4], height: 2.5, thickness: 0.14 } as unknown as AnyNode,
wall_s: { object: 'node', id: 'wall_s', type: 'wall', parentId: 'level_0', start: [6, 4], end: [0, 4], height: 2.5, thickness: 0.14 } as unknown as AnyNode,
wall_w: { object: 'node', id: 'wall_w', type: 'wall', parentId: 'level_0', start: [0, 4], end: [0, 0], height: 2.5, thickness: 0.14 } as unknown as AnyNode,
zone_living: { object: 'node', id: 'zone_living', type: 'zone', name: 'Living', parentId: 'level_0', polygon: [[0, 0], [6, 0], [6, 4], [0, 4]] } as unknown as AnyNode,
}
const ctx = { activeLevelId: 'level_0' }
const apply = (out: { changes?: { create?: Array<{ node: AnyNode }>; update?: Array<{ id: string; data: Partial<AnyNode> }> } }) => {
for (const c of out.changes?.create ?? []) nodes[c.node.id] = c.node
for (const u of out.changes?.update ?? []) {
if (nodes[u.id]) nodes[u.id] = { ...nodes[u.id], ...u.data } as AnyNode
}
}
const pOut = electricalAgentOperations.electrical_place_panel(
nodes,
{ name: 'MDP-1', position: [0.08, 1.52, 2.0], rotation: Math.PI / 2, occupancy: 'residential' },
ctx,
)
apply(pOut)
const panelId = pOut.result.panelId
// Place Solar PV Inverter (dedicated Home Run) + USB-C Duplex (room Home Run) + Smart Dimmer + Ceiling Fan (joins lighting!)
const pvOut = electricalAgentOperations.electrical_place_device(
nodes,
{ name: 'PV Inverter', position: [1.5, 1.45, 0.08], deviceKind: 'solar-inverter', panelId, poles: 2, breakerAmps: 40, loadVa: 7600, continuous: true },
ctx,
)
apply(pvOut)
expect(pvOut.result.connectionMode).toBe('new-home-run')
const usbOut = electricalAgentOperations.electrical_place_device(
nodes,
{ name: 'Living USB-C', position: [3.2, 0.38, 0.08], deviceKind: 'usb-duplex', panelId, breakerAmps: 20, loadVa: 180 },
ctx,
)
apply(usbOut)
expect(usbOut.result.connectionMode).toBe('new-home-run')
const fanPlace = findNextWallPlacement(nodes as unknown as Readonly<Record<string, SceneNodeLike>>, 'level_0', {
deviceKind: 'ceiling-fan',
preferredZoneId: 'zone_living',
})
expect(fanPlace.position[0]).toBeCloseTo(3.0, 2)
expect(fanPlace.position[2]).toBeCloseTo(2.0, 2)
expect(fanPlace.position[1]).toBeCloseTo(2.34, 2)
const floorBoxPlace = findNextWallPlacement(nodes as unknown as Readonly<Record<string, SceneNodeLike>>, 'level_0', {
deviceKind: 'floor-box',
preferredZoneId: 'zone_living',
})
expect(floorBoxPlace.position[1]).toBe(0)
const fanOut = electricalAgentOperations.electrical_place_device(
nodes,
{ name: 'Living Fan', position: [...fanPlace.position] as [number, number, number], deviceKind: 'ceiling-fan', panelId, breakerAmps: 15, loadVa: 120 },
ctx,
)
apply(fanOut)
const dimOut = electricalAgentOperations.electrical_place_device(
nodes,
{ name: 'Living Dimmer', position: [5.92, 1.22, 2.0], rotation: -Math.PI / 2, deviceKind: 'dimmer', panelId, breakerAmps: 15, loadVa: 150 },
ctx,
)
apply(dimOut)
expect(dimOut.result.connectionMode).toBe('joined-existing-run')
expect(dimOut.result.circuitNumber).toBe(fanOut.result.circuitNumber)
// Verify Priced BOM Takeoff includes solar-inverter, usb-duplex, ceiling-fan, and dimmer line items
const takeoff = buildElectricalTakeoff(nodes as unknown as Readonly<Record<string, SceneNodeLike>>)
expect(takeoff.pricing.lineItems.some((l) => l.cadSlug === 'solar-inverter-string')).toBe(true)
expect(takeoff.pricing.lineItems.some((l) => l.cadSlug === 'device-usb-duplex-20a')).toBe(true)
expect(takeoff.pricing.lineItems.some((l) => l.cadSlug === 'fixture-ceiling-fan-light')).toBe(true)
expect(takeoff.pricing.lineItems.some((l) => l.cadSlug === 'device-dimmer-smart')).toBe(true)
const auditOut = electricalAgentOperations.electrical_audit_nec(nodes)
expect(auditOut.result.errorCount).toBe(0)
})
test('ceiling-fan placement avoids doorway corners in concave overlapping Room N zones and keeps >= 1.15m wall clearance', () => {
const scene: Record<string, SceneNodeLike> = {
level_1: { id: 'level_1', type: 'level', height: 3.1 },
w_n: { id: 'w_n', type: 'wall', parentId: 'level_1', start: [-9.5, -6.5], end: [3.2, -6.5], height: 3.1, thickness: 0.16 },
w_s: { id: 'w_s', type: 'wall', parentId: 'level_1', start: [-3.5, -1.5], end: [3.2, -1.5], height: 3.1, thickness: 0.16 },
w_e: { id: 'w_e', type: 'wall', parentId: 'level_1', start: [3.2, -6.5], end: [3.2, -1.5], height: 3.1, thickness: 0.16 },
w_w: { id: 'w_w', type: 'wall', parentId: 'level_1', start: [-9.5, -6.5], end: [-9.5, 2.5], height: 3.1, thickness: 0.16 },
w_pantry: { id: 'w_pantry', type: 'wall', parentId: 'level_1', start: [-3.5, -6.5], end: [-3.5, -3.4], height: 3.1, thickness: 0.16 },
z_great: {
id: 'z_great',
type: 'zone',
parentId: 'level_1',
name: 'Great Room & Kitchen',
polygon: [[-3.5, -6.5], [3.2, -6.5], [3.2, -1.5], [-3.5, -1.5]],
},
// Overlapping auto-detected concave L-shaped Room 1 whose centroid sits near the doorway at (-3.3, -2.5)
z_auto: {
id: 'z_auto',
type: 'zone',
parentId: 'level_1',
name: 'Room 1',
polygon: [[-9.5, -6.5], [3.2, -6.5], [3.2, -1.5], [-3.5, -1.5], [-3.5, 2.5], [-9.5, 2.5]],
},
light_great: {
id: 'light_great',
type: 'electrical:device',
parentId: 'level_1',
deviceKind: 'luminaire',
position: [-0.15, 3.06, -4.0],
},
}
const placed = findNextWallPlacement(scene, 'level_1', { deviceKind: 'ceiling-fan' })
expect(placed.zoneName).toBe('Great Room & Kitchen')
// Must stay inside Great Room & Kitchen with >= 1.15 m clearance from Pantry Wall doorway (x = -3.5) and outer walls
expect(placed.position[0]).toBeGreaterThanOrEqual(-3.5 + 1.15)
expect(placed.position[0]).toBeLessThanOrEqual(3.2 - 1.15)
expect(placed.position[2]).toBeGreaterThanOrEqual(-6.5 + 1.15)
expect(placed.position[2]).toBeLessThanOrEqual(-1.5 - 1.15)
})
})