Correccion de los test

This commit is contained in:
2026-03-18 12:03:53 +01:00
parent a470404046
commit e9c0549b7c
6 changed files with 81 additions and 29 deletions

View File

@@ -1,12 +1,12 @@
import test, { describe, before, after } from "node:test";
import assert from "node:assert";
import assert from "node:assert/strict";
import { httpclient } from "config/httpclient.config.js";
import { pgClient } from "config/pgclient.config.js";
import { NfcRepository } from "./Nfc.repository.js";
import type { ServerContext } from "domain/ServerContext.js";
import { uuidv7 } from "uuidv7";
describe("NfcRepository Integration Tests", () => {
await describe("NfcRepository Integration Tests", async () => {
const serverContext: ServerContext = {
PostgresClient: pgClient,
HttpClient: httpclient
@@ -16,6 +16,8 @@ describe("NfcRepository Integration Tests", () => {
const testCardId = uuidv7()
const testCode = "12345678";
let lastCodeGenerated: string | undefined = undefined;
// Clean up before and after tests to ensure isolation
const cleanup = async () => {
await pgClient.query("DELETE FROM activation_codes WHERE card_id = $1", [testCardId]);
@@ -29,7 +31,7 @@ describe("NfcRepository Integration Tests", () => {
await cleanup();
});
test("createActivationCode should insert a record into the database", async () => {
await test("createActivationCode should insert a record into the database", async () => {
const result = await repo.createActivationCode({
cardId: testCardId,
code: testCode
@@ -43,9 +45,11 @@ describe("NfcRepository Integration Tests", () => {
assert.strictEqual(result.data.card_id, testCardId);
assert.strictEqual(result.data.code_plain, testCode);
assert.ok(result.data.code_hash, "Hash should be generated");
lastCodeGenerated = result.data.code_plain;
});
test("findActivationCodes should retrieve the inserted record", async () => {
await test("findActivationCodes should retrieve the inserted record", async () => {
// We assume the previous test inserted the record, but lets be safe or just rely on sequence
const result = await repo.findActivationCodes(testCardId);
@@ -61,7 +65,7 @@ describe("NfcRepository Integration Tests", () => {
assert.strictEqual(found.card_id, testCardId);
});
test("findActivationCodes should return empty array if card has no codes", async () => {
await test("findActivationCodes should return empty array if card has no codes", async () => {
const nonExistentCard = uuidv7()
const result = await repo.findActivationCodes(nonExistentCard);
@@ -73,4 +77,50 @@ describe("NfcRepository Integration Tests", () => {
assert.ok(result.data, "Data should be returned");
assert.strictEqual(result.data.length, 0);
});
await test("createActivationCode should add a new activation even if other exists", async () => {
const newCode = "secondcode"
const creation = await repo.createActivationCode({
cardId: testCardId,
code: newCode,
});
const result = await repo.findActivationCodes(testCardId);
if (result.error) {
assert.fail(`createActivationCode failed: ${result.error}`);
}
assert.ok(result.error == undefined, "Creation must be sucessfull")
assert.ok(result.data, "Data must be non-empty")
assert.ok(result.data, "Data should be returned");
assert.ok(result.data.length > 1, "Code list should be at least 2");
const lastCode = result.data[0]
assert.ok(lastCode, "First value of the list shouldnt be undefined")
assert.strictEqual(lastCode.card_id, testCardId, `${lastCode.card_id} != ${testCardId}`);
assert.strictEqual(lastCode.code_plain, newCode, `${lastCode.code_plain} != ${newCode}`);
lastCodeGenerated = newCode;
});
await test("findActivationCodes should return the last valid code first", async () => {
// We assume the previous test inserted the record, but lets be safe or just rely on sequence
const result = await repo.findActivationCodes(testCardId);
if (result.error) {
assert.fail(`findActivationCodes failed: ${result.error}`);
}
assert.ok(result.data, "Data should be returned");
assert.ok(Array.isArray(result.data));
const first = result.data[0];
assert.ok(first, "At least 1 code should exist");
assert.strictEqual(first.card_id, testCardId);
assert.strictEqual(first.code_plain, lastCodeGenerated);
})
});