2026-10-03 10:36:03 +00:00
|
|
|
import assert from "node:assert/strict";
|
|
|
|
|
import { test } from "node:test";
|
|
|
|
|
|
|
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
|
|
|
|
|
import { CompanyInputMapper } from "../application/company-input.mapper";
|
|
|
|
|
|
|
|
|
|
import { Company } from "./company.aggregate";
|
2026-10-03 11:13:15 +00:00
|
|
|
import { Person } from "./person.entity";
|
2026-10-03 10:36:03 +00:00
|
|
|
import { Site } from "./site.entity";
|
|
|
|
|
|
|
|
|
|
const input = new CompanyInputMapper();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
function companyFixture(name = "Empresa test") {
|
|
|
|
|
const props = input.company({ name, active: true });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(props.isSuccess);
|
|
|
|
|
const created = Company.create(props.data);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(created.isSuccess);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
return created.data;
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
test("compañía, contacto y sede tienen UUID v7 propios y se conserva la identidad al editar", () => {
|
|
|
|
|
const company = companyFixture();
|
|
|
|
|
const props = input.contact({
|
|
|
|
|
name: "Ana",
|
|
|
|
|
position: "Responsable",
|
|
|
|
|
email: "ANA@example.test",
|
|
|
|
|
phone: "+34 900 123 456",
|
|
|
|
|
active: true,
|
|
|
|
|
});
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(props.isSuccess);
|
2026-10-03 11:13:15 +00:00
|
|
|
const contact = Person.create({ ...props.data, companyId: company.id });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(contact.isSuccess);
|
2026-10-03 11:13:15 +00:00
|
|
|
const added = company.addPerson(contact.data);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(added.isSuccess);
|
|
|
|
|
assert.notEqual(company.id.toPrimitive(), contact.data.id.toPrimitive());
|
|
|
|
|
assert.equal(contact.data.id.toPrimitive().charAt(14), "7");
|
2026-10-03 11:13:15 +00:00
|
|
|
const changed = added.data.updatePerson(contact.data.id, { ...props.data, active: false });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(changed.isSuccess);
|
|
|
|
|
assert.ok(changed.data.id.equals(company.id));
|
|
|
|
|
assert.ok(changed.data.contacts[0]?.id.equals(contact.data.id));
|
|
|
|
|
assert.equal(changed.data.contacts[0]?.active, false);
|
|
|
|
|
assert.equal(added.data.contacts[0]?.active, true);
|
2026-10-03 11:13:15 +00:00
|
|
|
const repeated = changed.data.updatePerson(contact.data.id, { ...props.data, active: false });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(repeated.isSuccess);
|
|
|
|
|
assert.equal(repeated.data, changed.data);
|
|
|
|
|
});
|
|
|
|
|
test("la pertenencia impide asociar o editar entidades de otra compañía", () => {
|
|
|
|
|
const company = companyFixture();
|
|
|
|
|
const other = companyFixture("Otra");
|
|
|
|
|
const props = input.site({ name: "Central", address: null });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(props.isSuccess);
|
|
|
|
|
const site = Site.create({ ...props.data, companyId: other.id });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(site.isSuccess);
|
|
|
|
|
assert.equal(company.addSite(site.data).isSuccess, false);
|
|
|
|
|
const missing = company.updateSite(site.data.id, props.data);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.equal(missing.isSuccess, false);
|
2026-10-03 11:13:15 +00:00
|
|
|
const contact = company.updatePerson(UniqueID.generateNewID(), {
|
2026-10-03 10:36:03 +00:00
|
|
|
...inputContact(),
|
|
|
|
|
active: false,
|
|
|
|
|
});
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.equal(contact.isSuccess, false);
|
|
|
|
|
});
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
function inputContact() {
|
|
|
|
|
const props = input.contact({
|
|
|
|
|
name: "Ana",
|
|
|
|
|
position: "Responsable",
|
|
|
|
|
email: "ana@example.test",
|
|
|
|
|
phone: "900123456",
|
|
|
|
|
active: true,
|
|
|
|
|
});
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(props.isSuccess);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
return props.data;
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-10 20:22:28 +00:00
|
|
|
test("la dirección y el teléfono son optativos y se pueden borrar", () => {
|
2026-10-03 10:36:03 +00:00
|
|
|
const company = companyFixture();
|
2026-10-10 20:22:28 +00:00
|
|
|
const empty = input.site({ name: "Central", address: null, phone: null });
|
|
|
|
|
const full = input.site({
|
|
|
|
|
name: "Central",
|
|
|
|
|
address: "Calle Mayor, 10\n28001 Madrid",
|
|
|
|
|
phone: "+34 900 123 456",
|
|
|
|
|
});
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(empty.isSuccess && full.isSuccess);
|
|
|
|
|
const site = Site.create({ ...full.data, companyId: company.id });
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(site.isSuccess);
|
|
|
|
|
assert.equal(site.data.id.toPrimitive().charAt(14), "7");
|
2026-10-10 20:22:28 +00:00
|
|
|
assert.equal(site.data.phone.getOrUndefined()?.toPrimitive(), "+34 900 123 456");
|
2026-10-03 10:36:03 +00:00
|
|
|
const added = company.addSite(site.data);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(added.isSuccess);
|
|
|
|
|
const cleared = added.data.updateSite(site.data.id, empty.data);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(cleared.isSuccess);
|
|
|
|
|
assert.equal(cleared.data.sites[0]?.address.getOrUndefined(), undefined);
|
2026-10-10 20:22:28 +00:00
|
|
|
assert.equal(cleared.data.sites[0]?.phone.getOrUndefined(), undefined);
|
2026-10-03 10:36:03 +00:00
|
|
|
assert.ok(cleared.data.sites[0]?.id.equals(site.data.id));
|
|
|
|
|
});
|
|
|
|
|
test("nombres, email, teléfono y longitud de dirección se validan en los Value Objects", () => {
|
|
|
|
|
assert.equal(input.company({ name: " ", active: true }).isSuccess, false);
|
|
|
|
|
assert.equal(
|
|
|
|
|
input.contact({ name: "Ana", position: "", email: "invalid", phone: "abc", active: true })
|
|
|
|
|
.isSuccess,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
assert.equal(
|
|
|
|
|
input.contact({
|
|
|
|
|
name: "Ana",
|
|
|
|
|
position: "Responsable",
|
|
|
|
|
email: "ana@example.test",
|
|
|
|
|
phone: "abc",
|
|
|
|
|
active: true,
|
|
|
|
|
}).isSuccess,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
assert.equal(input.site({ name: "Central", address: "x".repeat(10001) }).isSuccess, false);
|
|
|
|
|
assert.equal(input.site({ name: "Central", address: "x".repeat(10000) }).isSuccess, true);
|
2026-10-10 20:22:28 +00:00
|
|
|
assert.equal(input.site({ name: "Central", address: null, phone: "abc" }).isSuccess, false);
|
2026-10-03 10:36:03 +00:00
|
|
|
});
|