All files / services profileStorage.ts

94.89% Statements 130/137
64.86% Branches 24/37
100% Functions 8/8
94.89% Lines 130/137

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 132x 132x 132x 132x 132x     132x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1x 114x 114x 104x 63x 63x 63x 101x 31x 101x       114x 31x 31x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 35x 36x 35x 35x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 2x 1x 2x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x  
import type { CandidateProfile } from "../types/job";
import {
  confirmedSkillLabel,
  NO_PROFILE_FOR_SKILL,
  type Preferences,
} from "../../shared/types/profile";
import { ApiError, apiFetch } from "./api/apiClient";
 
// Sized for a full pasted resume (a dense multi-page resume is ~15k chars).
// Must match PROFILE_TEXT_MAX in functions/src/models/user.ts — the server
// silently truncates at its own limit.
export const PROFILE_TEXT_MAX = 20_000;
 
/**
 * Candidate-profile storage. Same exported functions as the original
 * chrome.storage.local implementation — since 002 the profile lives in the
 * per-account server store (contracts/storage-api.md) and follows the
 * signed-in user across devices.
 */
 
async function throwUnexpected(response: Response): Promise<never> {
  let message = "The storage service rejected the request.";
  try {
    const body = (await response.json()) as { error?: { message?: string } };
    if (body.error?.message) message = body.error.message;
  } catch {
    // Keep the generic message.
  }
  throw new ApiError(response.status, "SERVICE_ERROR", message, false);
}
 
/**
 * Session-scoped copy of the current profile's fingerprint (006 R7), so the
 * panel can mark stale scores and react to a profile edit made elsewhere via
 * `chrome.storage.onChanged`. `value: null` means the account has no profile.
 */
export const FINGERPRINT_KEY = "profile:fingerprint";
 
interface CachedFingerprint {
  value: string | null;
  fetchedAt: number;
}
 
async function writeCachedFingerprint(value: string | null): Promise<void> {
  try {
    const entry: CachedFingerprint = { value, fetchedAt: Date.now() };
    await chrome.storage.session.set({ [FINGERPRINT_KEY]: entry });
  } catch {
    // The cache is an optimisation; a storage failure must not fail the call.
  }
}
 
/** `undefined` while unknown, `null` when the account has no profile. */
export async function getCachedFingerprint(): Promise<string | null | undefined> {
  try {
    const stored = await chrome.storage.session.get(FINGERPRINT_KEY);
    const entry = stored[FINGERPRINT_KEY] as CachedFingerprint | undefined;
    return entry ? entry.value : undefined;
  } catch {
    return undefined;
  }
}
 
export async function getProfile(): Promise<CandidateProfile | null> {
  const response = await apiFetch("/profile");
  if (response.status === 404) {
    await writeCachedFingerprint(null);
    return null;
  }
  if (!response.ok) await throwUnexpected(response);
  const profile = (await response.json()) as CandidateProfile;
  if (profile.text.length === 0) {
    await writeCachedFingerprint(null);
    return null;
  }
  await writeCachedFingerprint(profile.fingerprint ?? null);
  return profile;
}
 
export async function setProfile(input: {
  text: string;
  dealbreakers: string[];
  /** Omitted keeps the stored preferences, `null` clears them (merge PUT). */
  preferences?: Preferences | null;
  /** Omitted keeps the stored skills, `[]` clears them (merge PUT). */
  confirmedSkills?: string[];
}): Promise<CandidateProfile> {
  const response = await apiFetch("/profile", { method: "PUT", body: input });
  if (!response.ok) await throwUnexpected(response);
  const saved = (await response.json()) as CandidateProfile;
  await writeCachedFingerprint(saved.fingerprint ?? null);
  return saved;
}
 
/**
 * POST /api/profile/confirmed-skills ("I have this"). The server appends
 * atomically, so quick clicks on different skills can't overwrite each other.
 * The new fingerprint goes into the session cache, which marks every score
 * made against the old profile as out of date (FR-007).
 */
export async function confirmSkill(label: string): Promise<CandidateProfile> {
  const response = await apiFetch("/profile/confirmed-skills", {
    method: "POST",
    body: { label: confirmedSkillLabel(label) },
  });
  if (response.status === 404) {
    throw new ApiError(404, "SERVICE_ERROR", NO_PROFILE_FOR_SKILL, false);
  }
  if (!response.ok) await throwUnexpected(response);
  const saved = (await response.json()) as CandidateProfile;
  await writeCachedFingerprint(saved.fingerprint ?? null);
  return saved;
}
 
export async function clearProfile(): Promise<void> {
  const response = await apiFetch("/profile", { method: "DELETE" });
  if (!response.ok && response.status !== 204) await throwUnexpected(response);
  await writeCachedFingerprint(null);
}
 
/**
 * POST /api/profile/import: the text of a .pdf or .docx résumé. The file is
 * discarded server-side; nothing is stored until the user saves (FR-014).
 */
export async function importResume(file: File): Promise<string> {
  const form = new FormData();
  form.set("file", file);
  const response = await apiFetch("/profile/import", { method: "POST", rawBody: form });
  if (!response.ok) await throwUnexpected(response);
  return ((await response.json()) as { text: string }).text;
}
 
/** The shape analysis requests carry as `profileData`. */
export { toScoringProfile } from "../../shared/types/profile";
 
/** Serialized form sent with analysis requests (pure; shared with the web app). */
export { profileToPromptText } from "../../shared/types/profile";