import test, { describe, before, after } from "node:test"; 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"; await describe("NfcRepository Integration Tests", async () => { const serverContext: ServerContext = { PostgresClient: pgClient, HttpClient: httpclient }; const repo = new NfcRepository(serverContext); const testCardId = "123456789"; 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]); }; before(async () => { await cleanup(); }); after(async () => { await cleanup(); }); await test("createActivationCode should insert a record into the database", async () => { const result = await repo.createActivationCode({ cardId: testCardId, code: testCode }); if (result.error) { assert.fail(`createActivationCode failed: ${result.error}`); } assert.ok(result.data, "Data should be returned"); 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; }); 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); if (result.error) { assert.fail(`findActivationCodes failed: ${result.error}`); } assert.ok(result.data, "Data should be returned"); assert.ok(Array.isArray(result.data)); const found = result.data.find(code => code.code_plain === testCode); assert.ok(found, "The inserted code should be found"); assert.strictEqual(found.card_id, testCardId); }); await test("findActivationCodes should return empty array if card has no codes", async () => { const nonExistentCard = "987654321"; const result = await repo.findActivationCodes(nonExistentCard); if (result.error) { assert.fail(`findActivationCodes failed: ${result.error}`); } 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); }) });