Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

21.8 KB
Raw Blame
// @vitest-environment node
import fs from "fs";
import path from "path";
import { PrismaClient } from "@prisma/client";
import { APP_CONSTANTS, CONTACT_ROLES, JOB_STAGES } from "@/lib/constants";
import { buildBackupZip } from "@/lib/backup/export";
import { importBackup } from "@/lib/backup/import";
import { listSnapshots, readSnapshot } from "@/lib/backup/snapshot";
import { seedAccount } from "./helpers/backupTestDb";

// Async so the helper can be pulled in with a dynamic import — `require` is
// not defined in a Vite-transformed ESM test file.
const ctx = await vi.hoisted(async () => {
  const helpers = await import("./helpers/backupTestDb");
  const { url, dir } = helpers.makeTestDbUrl();
  helpers.pushSchema(url);
  return { url, dir };
});

vi.mock("@/lib/db", async () => {
  const { PrismaClient } = await import("@prisma/client");
  return {
    default: new PrismaClient({ datasources: { db: { url: ctx.url } } }),
  };
});

// Never start the cron in a test run.
vi.mock("@/lib/scheduler", () => ({ syncSchedulerState: vi.fn() }));

const prisma = new PrismaClient({ datasources: { db: { url: ctx.url } } });

let userId: string;
let uploadsDir: string;
const originalUploads = APP_CONSTANTS.UPLOADS_DIR;

beforeAll(async () => {
  uploadsDir = path.join(ctx.dir, "uploads");
  fs.mkdirSync(path.join(uploadsDir, "files", "resumes"), { recursive: true });
  (APP_CONSTANTS as { UPLOADS_DIR: string }).UPLOADS_DIR = uploadsDir;

  userId = await seedAccount(prisma, "[email protected]");
  await seedFullAccount();
}, 120_000);

afterAll(async () => {
  (APP_CONSTANTS as { UPLOADS_DIR: string }).UPLOADS_DIR = originalUploads;
  await prisma.$disconnect();
  fs.rmSync(ctx.dir, { recursive: true, force: true });
});

async function seedFullAccount() {
  const company = await prisma.company.create({
    data: {
      label: "Acme",
      value: "acme",
      createdBy: userId,
      watched: true,
      watchedAt: new Date("2026-09-01T10:00:00.000Z"),
      atsProvider: "greenhouse",
      atsToken: "acme",
      atsHost: null,
      websiteUrl: "https://acme.example.com",
      careersUrl: "https://acme.example.com/careers",
      industry: "Widgets",
      linkedinUrl: "https://www.linkedin.com/company/acme",
      size: "51-200",
      notes: "Strong engineering culture",
    },
  });
  const title = await prisma.jobTitle.create({
    data: { label: "Engineer", value: "engineer", createdBy: userId },
  });
  const tag = await prisma.tag.create({
    data: { label: "Remote", value: "remote", createdBy: userId },
  });
  const status = await prisma.jobStatus.findFirstOrThrow({ where: { value: "applied" } });

  const profile = await prisma.profile.create({ data: { userId } });

  const resumeFilePath = path.join(uploadsDir, "files", "resumes", "seed-resume.pdf");
  fs.writeFileSync(resumeFilePath, Buffer.from("%PDF-1.4 seed"));
  const file = await prisma.file.create({
    data: {
      fileName: "resume.pdf",
      filePath: resumeFilePath,
      fileType: "application/pdf",
    },
  });
  const resume = await prisma.resume.create({
    data: { profileId: profile.id, title: "My CV", FileId: file.id },
  });
  await prisma.contactInfo.create({
    data: {
      resumeId: resume.id,
      firstName: "A",
      lastName: "B",
      headline: "Engineer",
      email: "[email protected]",
      phone: "123",
    },
  });
  const summary = await prisma.summary.create({ data: { content: "Summary text" } });
  const section = await prisma.resumeSection.create({
    data: {
      resumeId: resume.id,
      sectionTitle: "Summary",
      sectionType: "summary",
      summaryId: summary.id,
    },
  });
  await prisma.skill.create({
    data: { tagId: tag.id, resumeSectionId: section.id, category: "Languages", order: 1 },
  });
  await prisma.user.update({
    where: { id: userId },
    data: { defaultResumeId: resume.id },
  });

  const job = await prisma.job.create({
    data: {
      userId,
      description: "A job",
      jobType: "full-time",
      createdAt: new Date(),
      statusId: status.id,
      jobTitleId: title.id,
      companyId: company.id,
      tags: { connect: { id: tag.id } },
    },
  });
  await prisma.note.create({
    data: { jobId: job.id, userId, content: "a note" },
  });

  const recruiter = await prisma.contactRole.findFirstOrThrow({
    where: { createdBy: userId, value: "recruiter" },
  });
  const contact = await prisma.contact.create({
    data: {
      name: "Pat Lee",
      email: "[email protected]",
      companyId: company.id,
      workedAtCompanyId: company.id,
      roleId: recruiter.id,
      createdBy: userId,
    },
  });
  await prisma.jobContact.create({
    data: { jobId: job.id, contactId: contact.id, roleId: recruiter.id },
  });

  const question = await prisma.question.create({
    data: { question: "Why us?", createdBy: userId, tags: { connect: { id: tag.id } } },
  });

  const onsiteType = await prisma.jobStageType.findFirstOrThrow({
    where: { createdBy: userId, value: "final / onsite interview" },
  });
  const stage = await prisma.jobStage.create({
    data: {
      jobId: job.id,
      stageTypeId: onsiteType.id,
      occurredAt: new Date("2026-09-24T14:00:00.000Z"),
      isCurrent: true,
      outcome: "scheduled",
      notes: "Bring laptop",
      durationMins: 90,
      format: "On-site",
      location: "Acme HQ, Bldg 3",
    },
  });
  await prisma.jobStageInterviewer.create({
    data: { stageId: stage.id, contactId: contact.id },
  });
  await prisma.jobStagePrepQuestion.create({
    data: { stageId: stage.id, questionId: question.id, asked: true, askedAt: new Date() },
  });

  const activityType = await prisma.activityType.create({
    data: { label: "Applying", value: "applying", createdBy: userId },
  });
  const task = await prisma.task.create({
    data: { userId, title: "Apply to Acme", activityTypeId: activityType.id },
  });
  await prisma.activity.create({
    data: {
      userId,
      activityName: "Applying",
      startTime: new Date(),
      activityTypeId: activityType.id,
      taskId: task.id,
    },
  });

  const automation = await prisma.automation.create({
    data: {
      userId,
      name: "Nightly",
      jobBoard: "greenhouse",
      keywords: "engineer",
      location: "Remote",
      resumeId: resume.id,
      scheduleHour: 9,
      nextRunAt: new Date("2020-01-01T09:00:00"),
      status: "active",
    },
  });
  // A run left mid-flight in the backup, which must not survive as running.
  await prisma.automationRun.create({
    data: { automationId: automation.id, status: "cancelling" },
  });

  // Rows the wipe must not touch, and one it must.
  await prisma.apiKey.create({
    data: { userId, provider: "openai", encryptedKey: "e", iv: "i", last4: "1234" },
  });
  await prisma.mcpAccessToken.create({
    data: {
      userId,
      name: "cli",
      tokenHash: "hash",
      tokenPrefix: "js_",
      scopes: "[]",
      expiresAt: new Date(Date.now() + 86_400_000),
    },
  });
  await prisma.chatConversation.create({
    data: { userId, messages: "[]" },
  });
}

describe("backup round trip", () => {
  it("restores every row, relation and file over an existing account", async () => {
    const { buffer } = await buildBackupZip(userId, "[email protected]");

    // Add a lookup the backup does not contain, so replace-not-merge is testable.
    await prisma.company.create({
      data: { label: "Ghost", value: "ghost", createdBy: userId },
    });

    // The in-flight run in the backup must not block the target-side guard, so
    // clear the target's own run first — the guard is asserted separately.
    await prisma.automationRun.updateMany({
      where: { status: { in: ["running", "cancelling"] } },
      data: { status: "failed", completedAt: new Date() },
    });

    const result = await importBackup(buffer, userId, "[email protected]", true);

    expect(result.counts.Job).toBe(1);
    expect(result.filesWritten).toBe(1);

    // The safety net exists on disk and is a real, readable backup.
    expect(result.snapshotPath).toBeTruthy();
    expect(fs.existsSync(result.snapshotPath!)).toBe(true);
    expect(result.snapshotPath!.startsWith(path.join(uploadsDir, "backups", userId))).toBe(
      true,
    );

    // Lookups are replaced, not merged.
    const companies = await prisma.company.findMany({ where: { createdBy: userId } });
    expect(companies.map((c) => c.value).sort()).toEqual(["acme"]);

    // Tag associations survive on both sides.
    const job = await prisma.job.findFirstOrThrow({
      where: { userId },
      include: { tags: true, Company: true, Status: true, Notes: true },
    });
    expect(job.tags.map((t) => t.value)).toEqual(["remote"]);
    expect(job.Company.label).toBe("Acme");
    expect(job.Status.value).toBe("applied");
    expect(job.Notes).toHaveLength(1);

    const question = await prisma.question.findFirstOrThrow({
      where: { createdBy: userId },
      include: { tags: true },
    });
    expect(question.tags.map((t) => t.value)).toEqual(["remote"]);

    // Summary ordering: the section imported with its summary attached.
    const section = await prisma.resumeSection.findFirstOrThrow({
      where: { Resume: { profile: { userId } } },
      include: { summary: true, skills: { include: { Tag: true } } },
    });
    expect(section.summary?.content).toBe("Summary text");
    expect(section.skills[0].Tag.value).toBe("remote");

    // defaultResumeId points at the new resume, not the old id.
    const user = await prisma.user.findUniqueOrThrow({ where: { id: userId } });
    const resume = await prisma.resume.findFirstOrThrow({
      where: { profile: { userId } },
    });
    expect(user.defaultResumeId).toBe(resume.id);

    // Activity -> Task survived.
    const activity = await prisma.activity.findFirstOrThrow({
      where: { userId },
      include: { task: true },
    });
    expect(activity.task?.title).toBe("Apply to Acme");

    // In-flight state normalized on the way in.
    const run = await prisma.automationRun.findFirstOrThrow({
      where: { automation: { userId } },
    });
    expect(run.status).toBe("failed");
    expect(run.errorMessage).toBe("interrupted");

    const automation = await prisma.automation.findFirstOrThrow({ where: { userId } });
    expect(automation.status).toBe("active");
    expect(automation.nextRunAt!.getTime()).toBeGreaterThan(Date.now());

    // The wipe set is honoured in both directions.
    expect(await prisma.apiKey.count({ where: { userId } })).toBe(1);
    expect(await prisma.mcpAccessToken.count({ where: { userId } })).toBe(1);
    expect(await prisma.chatConversation.count({ where: { userId } })).toBe(0);

    // filePath was recomputed and the bytes are actually there.
    const file = await prisma.file.findFirstOrThrow({
      where: { Resume: { profile: { userId } } },
    });
    expect(file.filePath.startsWith(path.join(uploadsDir, "files", "resumes"))).toBe(
      true,
    );
    expect(fs.existsSync(file.filePath)).toBe(true);
    expect(fs.readFileSync(file.filePath).toString()).toContain("seed");
  }, 120_000);

  it("restores every watchlist column on Company, watchedAt as a Date", async () => {
    const [restored] = await prisma.company.findMany({
      where: { createdBy: userId, value: "acme" },
    });

    expect(restored.watched).toBe(true);
    expect(restored.watchedAt).toBeInstanceOf(Date);
    expect(restored.watchedAt?.toISOString()).toBe("2026-09-01T10:00:00.000Z");
    expect(restored.atsProvider).toBe("greenhouse");
    expect(restored.atsToken).toBe("acme");
    expect(restored.atsHost).toBeNull();
    expect(restored.websiteUrl).toBe("https://acme.example.com");
    expect(restored.careersUrl).toBe("https://acme.example.com/careers");
    expect(restored.industry).toBe("Widgets");
    expect(restored.linkedinUrl).toBe("https://www.linkedin.com/company/acme");
    expect(restored.size).toBe("51-200");
    expect(restored.notes).toBe("Strong engineering culture");
  });

  it("restores a contact with its company, standing role and job link remapped", async () => {
    const contact = await prisma.contact.findFirstOrThrow({
      where: { createdBy: userId },
      include: {
        Company: true,
        WorkedAtCompany: true,
        Role: true,
        jobLinks: { include: { Job: true, Role: true } },
      },
    });

    expect(contact.Company?.value).toBe("acme");
    expect(contact.WorkedAtCompany?.value).toBe("acme");
    expect(contact.Role?.value).toBe("recruiter");
    expect(contact.jobLinks).toHaveLength(1);
    expect(contact.jobLinks[0].Job.userId).toBe(userId);
    expect(contact.jobLinks[0].Role.id).toBe(contact.Role?.id);

    // The roles came back once, not doubled on top of anything.
    expect(await prisma.contactRole.count({ where: { createdBy: userId } })).toBe(5);
  });

  it("round-trips a job's timeline, its interviewer and its prep question", async () => {
    const restored = await prisma.jobStage.findFirstOrThrow({
      where: { Job: { userId } },
      include: {
        StageType: { include: { Status: true } },
        interviewers: { include: { Contact: true } },
        prepQuestions: { include: { Question: true } },
        Job: true,
      },
    });

    expect(restored.StageType.value).toBe("final / onsite interview");
    expect(restored.StageType.Status.value).toBe("interview");
    expect(restored.isCurrent).toBe(true);
    expect(restored.durationMins).toBe(90);
    expect(restored.location).toBe("Acme HQ, Bldg 3");
    expect(restored.Job.userId).toBe(userId);
    expect(restored.interviewers[0].Contact.name).toBe("Pat Lee");
    expect(restored.prepQuestions[0].asked).toBe(true);
    expect(restored.prepQuestions[0].Question.question).toBe("Why us?");

    // Lookup, like ContactRole: the seeded types came back once, not doubled.
    expect(
      await prisma.jobStageType.count({ where: { createdBy: userId } }),
    ).toBe(14);
  });

  // D3: the file carries no JobStatus ids, so every restored type has to have
  // been re-pointed at this database's own status rows.
  it("re-points every restored stage type at a real local status", async () => {
    const types = await prisma.jobStageType.findMany({
      where: { createdBy: userId },
      include: { Status: true },
    });
    expect(types).toHaveLength(14);
    for (const type of types) {
      expect(type.Status).not.toBeNull();
    }
    const withdrawn = types.find((t) => t.value === "withdrawn")!;
    // Withdrawn is the case the export's widened jobStatuses query exists for:
    // no job holds it, so it would otherwise never reach the file.
    expect(withdrawn.Status.value).toBe("withdrawn");
  });

  it("reseeds the default contact roles when restoring a pre-contacts backup", async () => {
    const { buffer } = await buildBackupZip(userId, "[email protected]");

    // Reshape into what v1.1.19 wrote: no contact groups, no counts for them.
    // The stage groups go too — that release predates stages as well, and
    // JobStageInterviewer.contactId is a required FK, so leaving it behind
    // would abort the import on a contact the file no longer carries.
    const JSZip = (await import("jszip")).default;
    const zip = await JSZip.loadAsync(buffer);
    const data = JSON.parse(await zip.file("data.json")!.async("string"));
    const manifest = JSON.parse(await zip.file("manifest.json")!.async("string"));
    for (const group of [
      "Contact",
      "ContactRole",
      "JobContact",
      "JobStageType",
      "JobStage",
      "JobStageInterviewer",
      "JobStagePrepQuestion",
    ]) {
      delete data[group];
      delete manifest.counts[group];
    }
    zip.file("data.json", JSON.stringify(data));
    zip.file("manifest.json", JSON.stringify(manifest));
    const legacy = await zip.generateAsync({ type: "nodebuffer" });

    await importBackup(legacy, userId, "[email protected]", true);

    const roles = await prisma.contactRole.findMany({ where: { createdBy: userId } });
    expect(roles.map((r) => r.value).sort()).toEqual(
      CONTACT_ROLES.map((r) => r.value).sort(),
    );
  }, 120_000);

  it("reseeds the default stage types when restoring a pre-stages backup", async () => {
    const { buffer } = await buildBackupZip(userId, "[email protected]");

    const JSZip = (await import("jszip")).default;
    const zip = await JSZip.loadAsync(buffer);
    const data = JSON.parse(await zip.file("data.json")!.async("string"));
    const manifest = JSON.parse(await zip.file("manifest.json")!.async("string"));
    for (const group of [
      "JobStageType",
      "JobStage",
      "JobStageInterviewer",
      "JobStagePrepQuestion",
    ]) {
      delete data[group];
      delete manifest.counts[group];
    }
    zip.file("data.json", JSON.stringify(data));
    zip.file("manifest.json", JSON.stringify(manifest));
    const legacy = await zip.generateAsync({ type: "nodebuffer" });

    await importBackup(legacy, userId, "[email protected]", true);

    const types = await prisma.jobStageType.findMany({ where: { createdBy: userId } });
    expect(types.map((t) => t.value).sort()).toEqual(
      JOB_STAGES.map((s) => s.value).sort(),
    );
    // Decision 22: restored jobs come back stageless, and stay that way.
    expect(await prisma.jobStage.count({ where: { Job: { userId } } })).toBe(0);
  }, 120_000);

  it("does not demand confirmWipe on a freshly signed-up account", async () => {
    const freshUserId = await seedAccount(prisma, "[email protected]");
    const { buffer } = await buildBackupZip(userId, "[email protected]");

    // No confirmWipe: the seeded JobSource rows must not count as data.
    const result = await importBackup(
      buffer,
      freshUserId,
      "[email protected]",
      false,
    );
    // Nothing to snapshot on an empty account.
    expect(result.snapshotPath).toBeNull();
  }, 120_000);

  it("treats an account holding only contacts as data worth confirming", async () => {
    const contactsOnlyId = await seedAccount(prisma, "[email protected]");
    await prisma.contact.create({
      data: { name: "Only Contact", createdBy: contactsOnlyId },
    });
    const { buffer } = await buildBackupZip(userId, "[email protected]");

    await expect(
      importBackup(buffer, contactsOnlyId, "[email protected]", false),
    ).rejects.toThrow(/already holds data/);
    expect(await prisma.contact.count({ where: { createdBy: contactsOnlyId } })).toBe(1);
  }, 120_000);

  it("rolls back to the snapshot an import took", async () => {
    const rollbackUserId = await seedAccount(prisma, "[email protected]");
    // A content row, not just the lookup below: EMPTINESS_MODELS deliberately
    // excludes lookup models, so a Company alone would read as an empty target
    // and importBackup would skip the snapshot this test is asserting on.
    await prisma.profile.create({ data: { userId: rollbackUserId } });
    await prisma.company.create({
      data: { label: "Before", value: "before", createdBy: rollbackUserId },
    });

    // Import someone else's backup over it, then undo that.
    const { buffer } = await buildBackupZip(userId, "[email protected]");
    const imported = await importBackup(
      buffer,
      rollbackUserId,
      "[email protected]",
      true,
    );
    expect(imported.snapshotPath).toBeTruthy();
    expect(
      (await prisma.company.findMany({ where: { createdBy: rollbackUserId } }))
        .map((c) => c.value),
    ).not.toContain("before");

    const snapshots = await listSnapshots(rollbackUserId);
    const bytes = await readSnapshot(rollbackUserId, snapshots[0].id);
    await importBackup(bytes, rollbackUserId, "[email protected]", true);

    const after = await prisma.company.findMany({
      where: { createdBy: rollbackUserId },
    });
    expect(after.map((c) => c.value)).toEqual(["before"]);
  }, 120_000);

  // The regression test for the file gate. A tampered backup is the realistic
  // hostile input for this feature: the zip is structurally valid and the rows
  // are fine, but a file entry has been swapped for something that is not a
  // resume. The row must survive and the bytes must not reach disk.
  it("drops a file entry whose bytes are not a resume, without failing the import", async () => {
    const { buffer } = await buildBackupZip(userId, "[email protected]");

    const JSZip = (await import("jszip")).default;
    const zip = await JSZip.loadAsync(buffer);
    // Directory entries (e.g. "files/") also start with the prefix and sort
    // before the real file entry in JSZip's insertion order — exclude them.
    const entryName = Object.keys(zip.files).find(
      (n) => n.startsWith("files/") && !zip.files[n].dir,
    )!;
    zip.file(entryName, Buffer.from("MZ\x90\x00 definitely not a PDF"));
    const tampered = await zip.generateAsync({ type: "nodebuffer" });

    await prisma.automationRun.updateMany({
      where: { status: { in: ["running", "cancelling"] } },
      data: { status: "failed", completedAt: new Date() },
    });

    const result = await importBackup(tampered, userId, "[email protected]", true);

    expect(result.filesWritten).toBe(0);
    expect(result.counts.File).toBe(1);

    const file = await prisma.file.findFirstOrThrow({
      where: { Resume: { profile: { userId } } },
    });
    expect(fs.existsSync(file.filePath)).toBe(false);

    // Nothing outside the resumes directory was created either.
    const resumesDir = path.join(uploadsDir, "files", "resumes");
    expect(file.filePath.startsWith(resumesDir)).toBe(true);
    expect(
      fs.readdirSync(resumesDir).some((n) => n.includes("MZ") || n.endsWith(".exe")),
    ).toBe(false);
  }, 120_000);

  it("refuses before any write when the target has an active run", async () => {
    const automation = await prisma.automation.findFirstOrThrow({ where: { userId } });
    await prisma.automationRun.create({
      data: { automationId: automation.id, status: "running" },
    });

    const { buffer } = await buildBackupZip(userId, "[email protected]");
    const jobsBefore = await prisma.job.count({ where: { userId } });

    await expect(
      importBackup(buffer, userId, "[email protected]", true),
    ).rejects.toThrow(/wait/i);
    expect(await prisma.job.count({ where: { userId } })).toBe(jobsBefore);
  }, 120_000);
});