Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lib/utils/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function parseYaml<T = unknown>(yamlString: string): Promise<T> {

/**
* 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
Expand All @@ -72,6 +72,7 @@ function orderDeviceTypeFields(dt: DeviceType): Record<string, unknown> {

// --- 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;
Expand Down Expand Up @@ -118,7 +119,7 @@ function orderDeviceTypeFields(dt: DeviceType): Record<string, unknown> {

/**
* 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(
Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions src/tests/yaml-roundtrip.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});