Files
sf-nfc-server/src/infrastructure/Nfc.repository.test.ts

74 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-03-09 17:11:09 +01:00
import test, { describe, before, after } from "node:test";
import assert from "node:assert";
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";
describe("NfcRepository Integration Tests", () => {
const serverContext: ServerContext = {
PostgresClient: pgClient,
HttpClient: httpclient
};
const repo = new NfcRepository(serverContext);
const testCardId = "test-card-" + Date.now();
const testCode = "12345678";
// 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();
});
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");
});
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);
});
test("findActivationCodes should return empty array if card has no codes", async () => {
const result = await repo.findActivationCodes("non-existent-card");
if (result.error) {
assert.fail(`findActivationCodes failed: ${result.error}`);
}
assert.ok(result.data, "Data should be returned");
assert.strictEqual(result.data.length, 0);
});
});