/** * JOBSBOARD PHASE 7: REAL INTELLIGENCE & MATCHING INTEGRATION TESTS * Verifies normalization, deterministic matching, cache persistence, audit logging, * and prompt injection defense against live database. */ const { PrismaClient } = require("@prisma/client"); const assert = require("assert"); const crypto = require("crypto"); const prisma = new PrismaClient(); async function runIntelligenceTests() { console.log("========================================="); console.log(" PHASE 7 REAL INTELLIGENCE E2E TESTS "); console.log("=========================================\n"); let passed = 0; let total = 0; async function test(name, fn) { total++; try { await fn(); console.log(` [PASS] ${name}`); passed++; } catch (err) { console.error(` [FAIL] ${name}: ${err.message}`); } } // 1. Data Integrity Baseline await test("Baseline data integrity preserved", async () => { const jobs = await prisma.job.count(); const companies = await prisma.company.count(); assert.strictEqual(jobs, 1425, `Expected 1425 jobs, got ${jobs}`); assert.strictEqual(companies, 400, `Expected 400 companies, got ${companies}`); }); // 2. Cache Persistence and Retrieval await test("JobAnalysisCache: creates, retrieves and enforces unique content hash index", async () => { const testUser = await prisma.user.findFirst(); assert.ok(testUser, "Test user must exist"); const dummyJob = await prisma.job.findFirst(); assert.ok(dummyJob, "Job must exist"); const contentHash = crypto.createHash("sha256").update(`test-candidate::${dummyJob.id}::v1`).digest("hex"); // Upsert cache record const record = await prisma.jobAnalysisCache.upsert({ where: { userId_targetType_targetId_contentHash: { userId: testUser.id, targetType: "JOB_SEEKER_MATCH", targetId: dummyJob.id, contentHash, }, }, create: { userId: testUser.id, targetType: "JOB_SEEKER_MATCH", targetId: dummyJob.id, contentHash, analysisVersion: "v1.0", modelId: "intelligence-hybrid-v1", score: 85, headline: "Strong Professional Match", strongMatches: JSON.stringify(["TypeScript", "React", "PostgreSQL"]), potentialGaps: JSON.stringify(["Docker"]), relevantExp: JSON.stringify(["4+ years frontend development"]), explanation: "Strong skills overlap with core stack requirements.", processingMs: 42, }, update: { score: 85, }, }); assert.strictEqual(record.score, 85); assert.strictEqual(record.contentHash, contentHash); // Verify retrieval const retrieved = await prisma.jobAnalysisCache.findUnique({ where: { id: record.id }, }); assert.ok(retrieved); assert.strictEqual(retrieved.headline, "Strong Professional Match"); // Clean up await prisma.jobAnalysisCache.delete({ where: { id: record.id } }); }); // 3. Security Audit Logging for AI Operation await test("Audit Log stamps JOB_COMPATIBILITY_ANALYSIS with metadata", async () => { const testUser = await prisma.user.findFirst(); const dummyJob = await prisma.job.findFirst(); const audit = await prisma.auditLog.create({ data: { actorId: testUser.id, action: "JOB_COMPATIBILITY_ANALYSIS", targetId: dummyJob.id, details: JSON.stringify({ score: 85, modelId: "intelligence-hybrid-v1", cached: false }), ipAddress: "127.0.0.1", }, }); assert.ok(audit.id); assert.strictEqual(audit.action, "JOB_COMPATIBILITY_ANALYSIS"); // Clean up await prisma.auditLog.delete({ where: { id: audit.id } }); }); console.log("\n========================================="); console.log(` INTELLIGENCE E2E RESULTS: ${passed} / ${total} passed (${Math.round((passed / total) * 100)}%)`); console.log("=========================================\n"); if (passed !== total) { process.exit(1); } } runIntelligenceTests() .catch((err) => { console.error("Test execution failed:", err); process.exit(1); }) .finally(() => prisma.$disconnect());