-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.test.js
More file actions
155 lines (137 loc) · 4.97 KB
/
Copy pathcore.test.js
File metadata and controls
155 lines (137 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* core.test.js — CASTÚO-SYSTEM contract & schema tests (Node.js built-in runner)
*
* Validates:
* - JSON schema files are well-formed
* - TRACES API contract: response.estado contains "Compliant"
* - Legal notice present in all document responses
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const SCHEMAS_DIR = join(__dirname, "config", "schemas");
// ---------------------------------------------------------------------------
// Schema integrity tests
// ---------------------------------------------------------------------------
describe("JSON schema files", () => {
const schemaFiles = [
"traces.schema.json",
"siex.schema.json",
"pac.schema.json",
"sigpac.schema.json",
"regepa.schema.json",
];
for (const file of schemaFiles) {
it(`${file} is valid JSON`, () => {
const raw = readFileSync(join(SCHEMAS_DIR, file), "utf-8");
const schema = JSON.parse(raw);
assert.ok(schema, `${file} should parse to a truthy object`);
assert.equal(
typeof schema,
"object",
`${file} root should be an object`,
);
});
it(`${file} has required JSON Schema fields`, () => {
const schema = JSON.parse(readFileSync(join(SCHEMAS_DIR, file), "utf-8"));
assert.ok(
"$schema" in schema || "type" in schema || "properties" in schema,
`${file} should have at least one of: $schema, type, properties`,
);
});
}
});
// ---------------------------------------------------------------------------
// TRACES API contract tests (static contract, no live server required)
// ---------------------------------------------------------------------------
describe("TRACES API contract", () => {
/**
* Simulates the DocumentResponse shape returned by the FastAPI endpoint.
* Mirrors api/main.py DocumentResponse for contract verification.
*/
function buildTracesResponse({ estado = "✅ TRACES Compliant" } = {}) {
return {
tipo_documento: "TRACES Certificado Sanitario",
estado,
payload: {
explotacion: { rega: "ES123456789", nombre: "Test Farm" },
animales: { especie: "bovino", raza: "Retinta", cantidad: 10 },
movimiento: {
tipo: "EXPORT",
destino_pais: "DE",
destino_explotacion: "DE987654321",
},
certificado: { numero: "TRACES-ES-20260101-ES123456789" },
firma: {
pendiente_firma: true,
aviso: "Documento generado para REVISIÓN y FIRMA del productor",
},
},
aviso: "Documento generado para REVISIÓN y FIRMA del productor",
generado_en: new Date().toISOString(),
};
}
it("estado field contains 'Compliant'", () => {
const response = buildTracesResponse();
assert.ok(
response.estado.includes("Compliant"),
`estado should contain 'Compliant', got: ${response.estado}`,
);
});
it("certificado.numero starts with 'TRACES-ES-'", () => {
const response = buildTracesResponse();
assert.ok(
response.payload.certificado.numero.startsWith("TRACES-ES-"),
"certificado.numero should start with 'TRACES-ES-'",
);
});
it("pendiente_firma is true", () => {
const response = buildTracesResponse();
assert.equal(response.payload.firma.pendiente_firma, true);
});
it("mandatory legal notice present", () => {
const response = buildTracesResponse();
const expected = "Documento generado para REVISIÓN y FIRMA del productor";
assert.equal(response.aviso, expected);
assert.equal(response.payload.firma.aviso, expected);
});
});
// ---------------------------------------------------------------------------
// General document response contract
// ---------------------------------------------------------------------------
describe("DocumentResponse contract", () => {
const REQUIRED_FIELDS = [
"tipo_documento",
"estado",
"payload",
"aviso",
"generado_en",
];
function sampleResponse(overrides = {}) {
return {
tipo_documento: "Test Document",
estado: "✅ Test Compliant",
payload: { firma: { pendiente_firma: true } },
aviso: "Documento generado para REVISIÓN y FIRMA del productor",
generado_en: new Date().toISOString(),
...overrides,
};
}
for (const field of REQUIRED_FIELDS) {
it(`response includes required field: ${field}`, () => {
const response = sampleResponse();
assert.ok(field in response, `response must have field '${field}'`);
});
}
it("estado for compliant documents contains 'Compliant'", () => {
const response = sampleResponse();
assert.ok(response.estado.includes("Compliant"));
});
it("payload.firma.pendiente_firma is always true", () => {
const response = sampleResponse();
assert.equal(response.payload.firma.pendiente_firma, true);
});
});