diff --git a/src/lib/utils/yaml.ts b/src/lib/utils/yaml.ts index fc64afbd..0e4aa8c7 100644 --- a/src/lib/utils/yaml.ts +++ b/src/lib/utils/yaml.ts @@ -56,7 +56,7 @@ export async function parseYaml(yamlString: string): Promise { /** * Order DeviceType fields according to schema v1.0.0 - * Field order: slug, manufacturer, model, part_number, u_height, is_full_depth, is_powered, + * Field order: slug, manufacturer, model, part_number, u_height, slot_width, is_full_depth, is_powered, * weight, weight_unit, airflow, front_image, rear_image, colour, category, tags, * notes, serial_number, asset_tag, links, custom_fields, interfaces, power_ports, * power_outlets, device_bays, inventory_items, subdevice_role, va_rating @@ -72,6 +72,7 @@ function orderDeviceTypeFields(dt: DeviceType): Record { // --- Physical Properties --- ordered.u_height = dt.u_height; + if (dt.slot_width !== undefined) ordered.slot_width = dt.slot_width; if (dt.is_full_depth !== undefined) ordered.is_full_depth = dt.is_full_depth; if (dt.is_powered !== undefined) ordered.is_powered = dt.is_powered; if (dt.weight !== undefined) ordered.weight = dt.weight; @@ -118,7 +119,7 @@ function orderDeviceTypeFields(dt: DeviceType): Record { /** * Order PlacedDevice fields according to schema v1.0.0 - * Field order: id, device_type, name, position, face, front_image, rear_image, + * Field order: id, device_type, name, position, face, slot_position, front_image, rear_image, * parent_device, device_bay, notes, custom_fields */ function orderPlacedDeviceFields( @@ -132,6 +133,8 @@ function orderPlacedDeviceFields( if (device.name !== undefined) ordered.name = device.name; ordered.position = device.position; ordered.face = device.face; + if (device.slot_position !== undefined) + ordered.slot_position = device.slot_position; // --- Placement Image Override --- if (device.front_image !== undefined) diff --git a/src/tests/yaml-roundtrip.test.ts b/src/tests/yaml-roundtrip.test.ts new file mode 100644 index 00000000..f6a93886 --- /dev/null +++ b/src/tests/yaml-roundtrip.test.ts @@ -0,0 +1,41 @@ +import { describe, it, expect } from "vitest"; +import { serializeLayoutToYaml, parseLayoutYaml } from "$lib/utils/yaml"; +import { + createTestDevice, + createTestDeviceType, + createTestLayout, + createTestRack, +} from "./factories"; + +describe("YAML layout round-trip", () => { + it("preserves half-width slot width and slot position", async () => { + const halfWidth = createTestDeviceType({ + slug: "half-width-device", + u_height: 1, + slot_width: 1, + }); + + const layout = createTestLayout({ + racks: [ + createTestRack({ + id: "rack-1", + devices: [ + createTestDevice({ + id: "placed-1", + device_type: halfWidth.slug, + position: 10, + slot_position: "left", + }), + ], + }), + ], + device_types: [halfWidth], + }); + + const yaml = await serializeLayoutToYaml(layout); + const restored = await parseLayoutYaml(yaml); + + expect(restored.device_types[0]?.slot_width).toBe(1); + expect(restored.racks[0]?.devices[0]?.slot_position).toBe("left"); + }); +});