import { INestApplication } from "@nestjs/common"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { agent, bearer, closeTestApp, createTestApp, loginSuperAdmin, type AuthedTokens } from "./setup.js"; /** * Protects the CNPJ digit-check invariant in * apps/api/src/companies/companies.service.ts (assertValidCnpjIfPresent -> * @eden/domain-shared's isValidCNPJ) — format validation only, per that * function's own doc comment ("never confirms a document actually exists * or belongs to whoever claims it"). * * The valid-CNPJ fixture uses 11.444.777/0001-61 — verified against the * same digit-check algorithm (packages/domain-shared/src/validators/ * br-documents.ts) before writing this test, and confirmed (by querying * the shared dev Postgres directly) NOT already used by any company row, * unlike 11.222.333/0001-81 which the seeded "Handix Telecom LTDA" company * already holds. * * Cleanup: there is no DELETE /v1/companies endpoint, so the company * created by the "valid CNPJ succeeds" test is deactivated * (PATCH isActive:false) in afterAll rather than deleted. */ describe("companies — CNPJ validation invariant", () => { let app: INestApplication; let admin: AuthedTokens; let createdCompanyId: string | undefined; beforeAll(async () => { app = await createTestApp(); admin = await loginSuperAdmin(app); }); afterAll(async () => { if (createdCompanyId) { await agent(app) .patch(`/api/v1/companies/${createdCompanyId}`) .set(...bearer(admin.accessToken)) .send({ isActive: false }) .expect(200); } await closeTestApp(app); }); it("rejects a CNPJ that fails the check-digit algorithm with 400", async () => { const res = await agent(app) .post("/api/v1/companies") .set(...bearer(admin.accessToken)) .send({ companyName: "qa-test- Empresa CNPJ Inválido", cnpj: "11.222.333/0001-00" }) .expect(400); expect(res.body.message).toContain("CNPJ inválido"); }); it("accepts a company with a valid, not-already-used CNPJ", async () => { const res = await agent(app) .post("/api/v1/companies") .set(...bearer(admin.accessToken)) .send({ companyName: "qa-test- Empresa CNPJ Válido", cnpj: "11.444.777/0001-61" }) .expect(201); expect(res.body.id).toBeTypeOf("string"); // Persisted digits-only, per companies.service.ts's onlyDigits(dto.cnpj). expect(res.body.cnpj).toBe("11444777000161"); createdCompanyId = res.body.id; }); });