107 lines
4.0 KiB
TypeScript
107 lines
4.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
import { WorkstationValues } from "./value-objects/workstation-values.vo";
|
|
import { Workstation } from "./workstation.aggregate";
|
|
import type { WorkstationInput } from "./workstation-values";
|
|
|
|
const assetIds = [UniqueID.generateNewID().toPrimitive(), UniqueID.generateNewID().toPrimitive()];
|
|
function input(): WorkstationInput {
|
|
return {
|
|
name: " Puesto Ana ",
|
|
active: true,
|
|
siteId: null,
|
|
personId: null,
|
|
sharedAssetIds: [],
|
|
assetIds,
|
|
};
|
|
}
|
|
function values(data = input()) {
|
|
const result = WorkstationValues.create(data);
|
|
assert.ok(result.isSuccess);
|
|
return result.data;
|
|
}
|
|
test("Puestos: composición opcional, referencias válidas y límites sin duplicados", () => {
|
|
assert.ok(WorkstationValues.create({ ...input(), assetIds: [] }).isSuccess);
|
|
assert.equal(values().name.toPrimitive(), "Puesto Ana");
|
|
assert.ok(Object.isFrozen(values().assetIds));
|
|
assert.ok(!WorkstationValues.create({ ...input(), name: " " }).isSuccess);
|
|
assert.ok(!WorkstationValues.create({ ...input(), personId: "invalid" }).isSuccess);
|
|
const duplicate = WorkstationValues.create({
|
|
...input(),
|
|
assetIds: [assetIds[0] ?? "", assetIds[0] ?? ""],
|
|
});
|
|
assert.ok(!duplicate.isSuccess);
|
|
assert.equal(duplicate.error.code, "INVALID_MEMBERS");
|
|
assert.ok(
|
|
!WorkstationValues.create({
|
|
...input(),
|
|
assetIds: Array.from({ length: 201 }, () => UniqueID.generateNewID().toPrimitive()),
|
|
}).isSuccess
|
|
);
|
|
assert.ok(values().equals(values({ ...input(), assetIds: [...assetIds].reverse() })));
|
|
});
|
|
test("Puestos: UUID estable e historial de composición, persona y estado; guardado idéntico idempotente", () => {
|
|
const company = UniqueID.generateNewID();
|
|
const actor = UniqueID.generateNewID();
|
|
const created = Workstation.create(company, values(), actor, "2026-10-04T10:00:00.000Z");
|
|
assert.ok(created.isSuccess);
|
|
const item = created.data;
|
|
assert.equal(item.id.toPrimitive().charAt(14), "7");
|
|
assert.equal(item.history[0]?.id.toPrimitive().charAt(14), "7");
|
|
const same = item.update(
|
|
values({ ...input(), assetIds: [...assetIds].reverse() }),
|
|
actor,
|
|
"2026-10-04T11:00:00.000Z"
|
|
);
|
|
assert.ok(same.isSuccess);
|
|
assert.equal(same.data, item);
|
|
const changed = item.update(
|
|
values({ ...input(), personId: actor.toPrimitive(), active: false, assetIds: [] }),
|
|
actor,
|
|
"2026-10-04T11:00:00.000Z"
|
|
);
|
|
assert.ok(changed.isSuccess);
|
|
assert.ok(changed.data.id.equals(item.id));
|
|
assert.equal(changed.data.history.length, 2);
|
|
assert.equal(changed.data.history[0]?.values.toPrimitive().personId, null);
|
|
assert.deepEqual(changed.data.history[0]?.values.toPrimitive().assetIds, assetIds);
|
|
assert.ok(!changed.data.update(values(), actor, "2026-10-03T10:00:00.000Z").isSuccess);
|
|
assert.equal(
|
|
Workstation.rehydrate(
|
|
{ companyId: company, values: changed.data.values, history: changed.data.history },
|
|
changed.data.id
|
|
).id.toPrimitive(),
|
|
item.id.toPrimitive()
|
|
);
|
|
});
|
|
|
|
test("Puestos: recursos compartidos separados, sin duplicados y cambios con historial", () => {
|
|
const sharedId = UniqueID.generateNewID().toPrimitive();
|
|
const original = values({ ...input(), sharedAssetIds: [sharedId] });
|
|
assert.ok(Object.isFrozen(original.sharedAssetIds));
|
|
assert.ok(
|
|
!WorkstationValues.create({ ...input(), sharedAssetIds: [assetIds[0] ?? ""] }).isSuccess
|
|
);
|
|
assert.ok(
|
|
!WorkstationValues.create({ ...input(), sharedAssetIds: [sharedId, sharedId] }).isSuccess
|
|
);
|
|
const created = Workstation.create(
|
|
UniqueID.generateNewID(),
|
|
original,
|
|
UniqueID.generateNewID(),
|
|
"2026-10-04T10:00:00.000Z"
|
|
);
|
|
assert.ok(created.isSuccess);
|
|
const changed = created.data.update(
|
|
values(),
|
|
UniqueID.generateNewID(),
|
|
"2026-10-04T11:00:00.000Z"
|
|
);
|
|
assert.ok(changed.isSuccess);
|
|
assert.deepEqual(changed.data.history[0]?.values.toPrimitive().sharedAssetIds, [sharedId]);
|
|
assert.equal(changed.data.history.length, 2);
|
|
});
|