108 lines
4.1 KiB
TypeScript
108 lines
4.1 KiB
TypeScript
|
|
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";
|
||
|
|
import { Contact } from "./contact.entity";
|
||
|
|
import { Site } from "./site.entity";
|
||
|
|
|
||
|
|
const input = new CompanyInputMapper();
|
||
|
|
function companyFixture(name = "Empresa test") {
|
||
|
|
const props = input.company({ name, active: true });
|
||
|
|
assert.ok(props.isSuccess);
|
||
|
|
const created = Company.create(props.data);
|
||
|
|
assert.ok(created.isSuccess);
|
||
|
|
return created.data;
|
||
|
|
}
|
||
|
|
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,
|
||
|
|
});
|
||
|
|
assert.ok(props.isSuccess);
|
||
|
|
const contact = Contact.create({ ...props.data, companyId: company.id });
|
||
|
|
assert.ok(contact.isSuccess);
|
||
|
|
const added = company.addContact(contact.data);
|
||
|
|
assert.ok(added.isSuccess);
|
||
|
|
assert.notEqual(company.id.toPrimitive(), contact.data.id.toPrimitive());
|
||
|
|
assert.equal(contact.data.id.toPrimitive().charAt(14), "7");
|
||
|
|
const changed = added.data.updateContact(contact.data.id, { ...props.data, active: false });
|
||
|
|
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);
|
||
|
|
const repeated = changed.data.updateContact(contact.data.id, { ...props.data, active: false });
|
||
|
|
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 });
|
||
|
|
assert.ok(props.isSuccess);
|
||
|
|
const site = Site.create({ ...props.data, companyId: other.id });
|
||
|
|
assert.ok(site.isSuccess);
|
||
|
|
assert.equal(company.addSite(site.data).isSuccess, false);
|
||
|
|
const missing = company.updateSite(site.data.id, props.data);
|
||
|
|
assert.equal(missing.isSuccess, false);
|
||
|
|
const contact = company.updateContact(UniqueID.generateNewID(), {
|
||
|
|
...inputContact(),
|
||
|
|
active: false,
|
||
|
|
});
|
||
|
|
assert.equal(contact.isSuccess, false);
|
||
|
|
});
|
||
|
|
function inputContact() {
|
||
|
|
const props = input.contact({
|
||
|
|
name: "Ana",
|
||
|
|
position: "Responsable",
|
||
|
|
email: "ana@example.test",
|
||
|
|
phone: "900123456",
|
||
|
|
active: true,
|
||
|
|
});
|
||
|
|
assert.ok(props.isSuccess);
|
||
|
|
return props.data;
|
||
|
|
}
|
||
|
|
test("la dirección es optativa, admite varias líneas y se puede borrar", () => {
|
||
|
|
const company = companyFixture();
|
||
|
|
const empty = input.site({ name: "Central", address: null });
|
||
|
|
const full = input.site({ name: "Central", address: "Calle Mayor, 10\n28001 Madrid" });
|
||
|
|
assert.ok(empty.isSuccess && full.isSuccess);
|
||
|
|
const site = Site.create({ ...full.data, companyId: company.id });
|
||
|
|
assert.ok(site.isSuccess);
|
||
|
|
assert.equal(site.data.id.toPrimitive().charAt(14), "7");
|
||
|
|
const added = company.addSite(site.data);
|
||
|
|
assert.ok(added.isSuccess);
|
||
|
|
const cleared = added.data.updateSite(site.data.id, empty.data);
|
||
|
|
assert.ok(cleared.isSuccess);
|
||
|
|
assert.equal(cleared.data.sites[0]?.address.getOrUndefined(), undefined);
|
||
|
|
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);
|
||
|
|
});
|