metis-desk/modules/resources/src/api/domain/global-resource.test.ts
2026-10-25 01:19:17 +02:00

68 lines
2.4 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import { UniqueID } from "@repo/rdx-ddd";
import { captureGlobalResourceSpecification } from "../application/global-resource-specification";
import { Resource } from "./resource.aggregate";
import { scopeFixture } from "./resource-scope.fixture";
import { ResourceValues } from "./value-objects/resource-values.vo";
const recordedAt = "2026-10-24T10:00:00.000Z";
test("global: crear exige UUID v7 y conserva identidad y propietario en edición e idempotencia", () => {
const { local, actor } = scopeFixture();
const id = UniqueID.generateNewID();
const created = Resource.createGlobal(id, local.specification, local.values, actor, recordedAt);
assert.ok(created.isSuccess);
assert.equal(created.data.scope, "global");
assert.equal(created.data.companyId, null);
assert.ok(created.data.id.equals(id));
assert.equal(created.data.history.length, 1);
const repeated = created.data.updateGlobal(local.values, actor, recordedAt);
assert.ok(repeated.isSuccess);
assert.equal(repeated.data, created.data);
const values = ResourceValues.create(
{ ...local.values.toPrimitive(), name: "Producto distinto" },
local.specification
);
assert.ok(values.isSuccess);
const changed = created.data.updateGlobal(values.data, actor, recordedAt);
assert.ok(changed.isSuccess);
assert.ok(changed.data.id.equals(id));
assert.equal(changed.data.history.length, 2);
assert.equal(created.data.history.length, 1);
assert.equal(changed.data.history[0], created.data.history[0]);
assert.equal(local.updateGlobal(values.data, actor, recordedAt).isSuccess, false);
const v4 = UniqueID.create("39820434-230b-4d31-b46e-ef12d2fe849a");
assert.ok(v4.isSuccess);
assert.equal(
Resource.createGlobal(v4.data, local.specification, local.values, actor, recordedAt).isSuccess,
false
);
});
test("global: captura por stockKey, sin depender del nombre traducido de la plantilla", () => {
const { catalog, local } = scopeFixture();
const eligible = captureGlobalResourceSpecification(
catalog,
local.values.toPrimitive().templateId,
{ existing: false }
);
assert.ok(eligible.isSuccess);
assert.equal(eligible.data.toPrimitive().templateName, "Nombre traducido y modificado");
assert.equal(
captureGlobalResourceSpecification(catalog, UniqueID.generateNewID().toPrimitive(), {
existing: false,
}).isSuccess,
false
);
});