mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-17 08:39:47 +10:00
test: add ~500 tests across web, utils, api, import, ai, db, email, auth (#3038)
* test(web): add tests for zustand stores and pure helpers
Cover stores and pure helpers across the builder/dashboard/command-palette
surfaces that previously had 0% coverage:
- command-palette store (open/close, page stack, search clearing, goBack)
- builder assistant-store
- builder sidebar store + parseBuilderLayoutCookie / mapPanelLayoutToBuilderLayout
- builder section store (collapse, toggle, toggleAll)
- builder preview page-layout toggle
- dashboard resume-thumbnail render-size math + cache key
- MCP tool name + annotations invariants
* test(web): cover MCP helpers and template metadata
Add tests for previously 0%-coverage MCP and dialog helpers:
- buildMcpServerCard: server-info, tool catalog vs MCP_TOOL_NAME, prompts,
resource templates, configuration schema, auth schemes
- registerPrompts (build/improve/review): registration, args schema, resource
context with interpolated resume id, read-only / no-fabrication directives
- registerResources (resume://{id}, resume://_meta/schema): handler reads via
oRPC client, error on missing id, schema returns valid JSON
- templates metadata: ids match display names, valid sidebar positions,
unique image URLs, every entry has tags + description
* test(web): cover sidebar section helpers and layout screens
Add tests for previously near-0%-coverage modules:
- libs/resume/section: getSectionTitle / getSectionIcon return distinct,
exhaustive results for every sidebar section + cover-letter; icon props
forwarding; left/right sidebar collections do not overlap.
- layout/loading-screen: spinner + text render.
- layout/error-screen: error message surfaces, Refresh button triggers reset.
- layout/breakpoint-indicator: default + each corner positioning, all
breakpoint labels rendered, print-hidden class applied.
* test(web): cover preview canvas math and font-weight defaults
Add tests for pure helpers that previously had no direct coverage:
- typography/getNextWeights: prefers 400 + 600 when both are available,
returns null for unknown families, never produces duplicates, bounded
to two weights from the 100..900 set.
- preview.shared/normalizeResumePreviewProps: documented defaults +
pass-through.
- preview.shared/getScaledPreviewPageSize: scaling identity at 1, and
fractional scaling.
- preview.shared/getPreviewCanvasScale: respects 4x desired scale for
small pages, honors high devicePixelRatio, clamps to the 16M-pixel
canvas budget for large pages.
* test(utils): cover DOCX section renderers and html-to-paragraphs
@reactive-resume/utils/resume/docx was previously at ~2.84% statement
coverage despite being load-bearing for the resume DOCX export.
- section-renderers: empty-string / hidden-section / hidden-item branches
for renderSummary, renderBuiltInSection, and renderCustomSection;
cover-letter and summary custom-section dispatch; unknown-type fallback;
setRenderConfig idempotency.
- html-to-docx: whitespace-only short-circuit, multiple top-level blocks,
h1..h6 paragraph mapping, inline style and link rendering, custom
font/size/color/linkColor config, ignored script/comment nodes.
* test: cover DOCX builder smoke paths and reactive-resume JSON importer
- utils/resume/docx/builder: buildDocument runs end-to-end against the
default and sample resume data, both page formats, full-width and
sidebar layouts, and gracefully degrades with unparseable color or
empty font family inputs.
- import/reactive-resume-json: ReactiveResumeJSONImporter validates
malformed JSON, recovers missing built-in sections by appending them
to page 1 without reordering, and preserves layouts that already
contain every built-in section.
* test(import): cover JSONResumeImporter parse/convert
JSONResumeImporter (450 lines) was previously at 0% coverage. Add tests
for the public surface:
- Invalid JSON / invalid-shape errors are surfaced.
- basics, summary, picture, education, projects, skills, profiles all
map to the corresponding ResumeData sections.
- Empty work/education entries (missing key field) are filtered out.
- Highlights become HTML list items in the description field.
- Skill level parsing flows through utils/level.parseLevel.
- formatLocation joins city, region, countryCode with commas.
* test(web): cover query client serializer and home-page animations
- libs/query/client: getQueryClient returns a fresh QueryClient,
queryKeyHashFn produces stable JSON envelopes for matching keys
(and distinct strings for different keys), dehydrate/hydrate
round-trip Date values via the oRPC serializer.
- components/animation/spotlight: overlay container is
pointer-events-none, both beam groups render, custom
width/height/translateY/gradient props flow into inline styles.
- components/animation/comet-card: children mount inside the
perspective wrapper, custom className is merged with the 3D
baseline classes, glare overlay renders, mouse move/leave
handlers do not throw.
* test(web): cover Copyright footer
Verify the footer's MIT license link, Amruth Pillai attribution,
external-tab targets, embedded app version (via __APP_VERSION__ stub),
and custom className merging — previously at 0% coverage.
* test(api): cover flags, auth providers, and resume-access cookies
Unlock @reactive-resume/api by mocking @reactive-resume/env/server and
@tanstack/react-start/server. Previously the only services tested were
the standalone AI test and resume-access-policy.
- services/flags: flagsService.getFlags reads disableSignups/disableEmailAuth
from env (no stale cache).
- services/auth: providers.list always exposes credential + passkey, and
only adds Google/GitHub/LinkedIn/custom when both id and secret are set;
custom provider uses OAUTH_PROVIDER_NAME with a 'Custom OAuth' fallback.
- helpers/resume-access: hasResumeAccess validates against signed cookies
with constant-time comparison; grantResumeAccess writes a 10-minute
httpOnly cookie with the secure flag matching APP_URL's https-ness.
* test: cover statistics service and email transport via env mocks
- api/services/statistics: github star count succeeds, retries on
non-OK, falls back to last-known on fetch error / non-positive /
non-numeric responses; user and resume counts roll up DB count.
- email/src/transport: returns silently with no text/html, logs when
SMTP is not configured, dispatches via nodemailer with the env
config when fully wired, renders react elements to html + text
bodies, swallows transport errors instead of crashing.
* test(api): cover resume-events publish + subscribe
- publishResumeUpdated issues pg_notify with channel and serialized
event payload.
- subscribeResumeUpdated yields events whose resumeId+userId match
the subscription, filters out other resumes/users, ignores
malformed JSON and notifications on other channels, calls
LISTEN/UNLISTEN and releases the client, and terminates
immediately if the abort signal fires before iteration starts.
* test(api): cover oRPC auth resolution
resolveUserFromRequestHeaders is the single point where every oRPC
procedure picks up the authenticated user. Test the priority chain:
- x-api-key wins when present and valid
- on invalid api key, falls back to session via auth.api.getSession
- Bearer JWT in Authorization header is verified via verifyOAuthToken
- invalid Bearer falls back to session
- Authorization scheme other than Bearer is ignored entirely
- thrown errors from token verification are logged and swallowed
(caller still tries session)
- returns null when no auth method succeeds
* test(api): cover storage helpers
inferContentType, isImageFile, processImageForUpload were 0%
coverage despite being on the picture upload path.
- inferContentType maps known image and pdf extensions, is
case-insensitive, ignores path depth, and falls back to
application/octet-stream for unknown.
- isImageFile allows only the upload allowlist (gif/png/jpeg/webp)
and rejects image/svg+xml, application/pdf, and empty strings.
- processImageForUpload short-circuits to the original bytes when
FLAG_DISABLE_IMAGE_PROCESSING is true, otherwise pipes through
sharp and returns image/jpeg.
* test(import): broaden v4 importer section-mapping coverage
The existing v4 importer test focused on a single bug (description-only
custom items) and the skill/language level scaling. This new test
exercises the bulk of the v4 → v5 transformation path:
- basics, picture (with border), summary, customFields
- every section's filter-by-required-field invariant (awards needs
title, certifications needs name, education needs institution,
experience needs company, volunteer needs organization, etc.)
- experience / education / awards / certifications / references field
renames between schemas
- language and skill level scaling (v4 0..10 → v5 0..5)
Brings reactive-resume-v4-json from ~66% statement coverage to a
materially higher figure (the bulk of the 410-line transformer body).
* test: cover buildDocx entry and AI configuration store
- utils/resume/docx/index: buildDocx returns a non-empty Blob for both
default and populated resumes (previously 0% coverage despite being
the public DOCX entry point).
- ai/store: useAIStore preserves verification status across no-op
updates, but resets testStatus + enabled whenever provider, model,
apiKey, or baseURL changes; canEnable is gated to testStatus=success;
setEnabled(true) is refused unless verified; reset clears every
field. Brings @reactive-resume/ai from ~72% to materially higher
coverage.
* test(db): cover resume schema definitions
packages/db was previously at 0% coverage. Smoke-test the public
resume / resume_statistics / resume_analysis tables:
- getTableName matches the SQL identifier used by migrations
- expected columns are present on each table
- defaultResumeData wiring on the data column resolves to a valid
shape
These are structural assertions that catch accidental renames /
removals without needing a live database connection.
* test(db): cover auth schema tables and relations export
- src/schema/auth: table-driven test for each of the 12 auth tables
asserting SQL name and presence of the key columns (user/session/
account/verification/two_factor/passkey/apikey/jwks/oauth_*).
- src/relations: smoke test confirming the relations export is defined.
Brings @reactive-resume/db from 0% to materially higher coverage.
* test(auth): cover getSession isomorphic helper
@reactive-resume/auth was previously at 0% coverage. functions.ts
is the server entry point that other packages call. Mock the auth
config + tanstack/react-start to verify:
- getSession forwards getRequestHeaders() to auth.api.getSession
- returns null when better-auth returns null
* test(web): cover BuilderSidebarEdge
Small presentational component on the builder layout — assert children
mount, left/right positioning class branches, and the sm:flex
mobile-hide behavior.
* test(web): cover section-title-locale resolver cache and hook
The section-title-locale module wraps createSectionTitleResolver with
a per-locale async cache and a React hook for consumers in the
builder. Cover:
- createSectionTitleResolverForLocale returns a usable resolver
- repeated calls for the same locale share a cached promise
- unknown locales fall back through resolveLocale
- useSectionTitleResolver returns null while loading and when no
locale is passed
- the hook resolves to a function once the async loader settles
* test(web): cover BaseCommandGroup page-stack gating
BaseCommandGroup conditionally renders based on the top of the
command-palette page stack. Tests cover:
- root group renders when no sub-page is active
- root group hides when a sub-page is on top
- sub-page group renders only when its page matches
- mismatched sub-page leaves the group hidden
* test(web): cover ThemeProvider context
- useTheme outside ThemeProvider throws the documented error
- useTheme inside ThemeProvider returns the theme + setTheme +
toggleTheme helpers
* test(web): cover ConfirmDialogProvider + useConfirm hook
- useConfirm outside provider throws the documented error
- confirm returns a pending promise
- promise resolves false when the Cancel button is clicked
- promise resolves true when the Confirm button is clicked
- works with custom confirmText label
apps/web has its own copy of this hook distinct from
packages/ui (mirrors the existing UI-package tests).
* test(web): cover PromptDialogProvider + usePrompt hook
- usePrompt outside provider throws the documented error
- returns a function when wrapped
- Cancel click resolves the promise to null
- Confirm click resolves to the current input value
- defaultValue option seeds the initial input value
* test(web): cover DashboardHeader
Small presentational header used across dashboard routes — title h1,
icon rendering, className merge, mobile sidebar trigger present and
hidden on md+.
* test(web): cover Create/Import resume cards
Both cards on the resumes dashboard wire a click handler to open
the appropriate dialog via the dialog store:
- CreateResumeCard opens resume.create
- ImportResumeCard opens resume.import
Also asserts the i18n copy strings (icons aside, the cards are
otherwise structural).
* test(web): cover command-palette language sub-page
LanguageCommandPage is a BaseCommandGroup gated on page='language'.
Tests assert:
- it is hidden when 'language' is not the top of the page stack
- when active, it renders a CommandItem per localeMap entry
- documented locale codes (en-US, de-DE, ja-JP) appear
* test(web): cover command-palette theme + preferences sub-pages
- ThemeCommandPage: hidden when 'theme' is not on top, renders Light
and Dark options when active
- PreferencesCommandGroup: root group renders both Change theme to...
and Change language to... items; clicking each pushes the
corresponding page onto the command-palette stack
* test(ai): cover executePatchResume tool
- patchResumeInputSchema rejects empty operations and unknown op
values; accepts valid replace/add/remove
- executePatchResume returns the applied operations on success
- executePatchResume throws when an operation targets an invalid path
(passes through the underlying applyResumePatches validation)
- multi-op patches against top-level fields succeed end-to-end
* test(ai): cover sanitize edge branches
Hit the previously-uncovered branches in sanitize.ts:
- numeric 1 coerces to true
- '1' / '0' string shorthand coerces to true/false
- missing item.hidden gets salvaged to false
- empty input causes a non-Zod throw (caught + rethrown with generic message)
* test(ai): cover patch-proposal preview + normalize edge cases
- remove operations surface before-value with after=undefined
- buildResumePatchProposalPreview labels metadata/page paths sanely
- normalizeResumePatchProposals stamps every proposal with baseUpdatedAt
- normalizeResumePatchProposals preserves input order
* test(web): cover getLocaleOptions helper
Locale combobox surface — verify the option list mirrors localeMap
shape, uses locale codes as values, populates label + keywords with
the translated display name, and produces unique values.
* test(web): cover LevelTypeCombobox option mapping
LevelTypeCombobox maps levelDesignSchema.shape.type.options through
the internal getLevelTypeName labeler. Assert all 7 level types are
exposed and that each produces a non-empty label.
* test(web): cover ThemeToggleButton fallback paths
- aria-label flips between 'Switch to light theme' and 'Switch to
dark theme' based on current theme
- clicking when document.startViewTransition is unavailable
short-circuits to toggleTheme directly
- prefers-reduced-motion forces the direct toggle path even when
the view-transition API is available
* test(web): cover NotFoundScreen
Mock the TanStack Router Link so the screen renders standalone, then
assert: documented error heading, routeId is surfaced verbatim, and
the Go Back link points to '..' (parent route).
* test(web): cover InformationSectionBuilder
Stub SectionBase so the donation/info section renders standalone.
Assert: donation prompt copy, OpenCollective CTA link, all 5
external resource links present, and external links target _blank
with rel=noopener.
* test(web): cover NotesSectionBuilder
Mock SectionBase, RichInput, and the resume-draft hooks so the
notes section renders in isolation. Assert: privacy hint copy
renders, RichInput is seeded with metadata.notes, and onChange
proxies through updateResumeData with a draft recipe that mutates
metadata.notes.
* test(web): cover TemplateSectionBuilder
Stub SectionBase and useCurrentResume so the right-sidebar template
section renders standalone. Asserts: current template name in the
heading, template tags rendered as badges, preview image points to
the catalog asset, and clicking the preview opens the
resume.template.gallery dialog.
* test(web): cover ColorPicker preset selection and trigger override
Mock the heavy @uiw/react-color-colorful dependency. Test:
- the trigger swatch reflects the controlled value
- clicking a preset color invokes onChange with an rgba() string
- a custom trigger replaces the default swatch when provided
* test(web): cover ExportSectionBuilder
Mock the heavy export pipelines (buildDocx, createResumePdfBlob,
downloadWithAnchor) and the resume-draft hook to test:
- JSON button packages resume.data as application/json and triggers
download with the {name}.json filename
- DOCX button awaits buildDocx and downloads .docx
- PDF button awaits createResumePdfBlob and downloads .pdf
* test(web): cover ProfilesSectionBuilder
Stub the resume-draft hooks, SectionBase, SectionItem, and
SectionAddItemButton so the profiles section renders standalone:
- one SectionItem per profile with network as title and username as
subtitle
- 'Add a new profile' affordance present
- when items.length > 0, the wrapper uses a solid border (not dashed)
* test(web): cover SkillsSectionBuilder
Mirror the profiles test for the skills section — verifies one
SectionItem per skill (name → title, proficiency → subtitle) and
the Add a new skill affordance.
* test(web): bulk-cover 7 left-sidebar section builders
Single test file covers awards, certifications, interests, languages,
publications, references, and volunteer builders. For each:
- one SectionItem rendered with the documented field → title/subtitle
mapping
- awards: title → awarder
- certifications: title → 'issuer • date'
- interests: name → (no subtitle)
- languages: language → fluency
- publications: title → publisher
- references: name → (no subtitle)
- volunteer: organization → location
- the 'Add a new {kind}' affordance with the matching copy
Mocks SectionBase, SectionItem, SectionAddItemButton, and the
resume-draft hooks so each builder renders standalone.
* test(web): cover ProjectsSectionBuilder buildSubtitle
The projects section is the only left-sidebar builder with a
composite subtitle. Tests three branches of its inline
buildSubtitle helper:
- period + website.label → joined with ' • '
- period only → just the period
- empty period + whitespace-only website.label → returns undefined
* test(web): cover Education + Experience section builders
- Education: school → title, degree → subtitle, add-new affordance
- Experience: position → subtitle when set; falls back to '1 role' /
'N roles' (lingui plural) when position empty and roles[] present;
add-new affordance
* test(web): cover CountUp animated number renderer
- default aria attributes (aria-live=polite, aria-atomic=true)
- initial textContent seeds to 'from' (up) or 'to' (down) value
- separator option formats with grouping
- decimal places are preserved when from/to are fractional
- aria-hidden=true strips aria-live + aria-atomic
- custom className is applied to the rendered span
* test(web): cover TextMaskEffect SVG renderer
- supplied text renders in every visible <text> layer
- aria-hidden + aria-label forwarded onto the root svg
- mouse enter/move/leave handlers don't throw
- custom className merges into the svg's class attribute
* test(web): cover URLInput prefix handling
- displayed input strips the https:// prefix so users only edit the
meaningful portion
- editing re-adds the prefix on the way back through onChange
- pre-prefixed input is preserved
- cleared input emits an empty url (no prefix forced)
- hideLabelButton=true removes the popover trigger; default keeps it
* test(web): cover GithubStarsButton
Mocks useQuery + the CountUp animation so the button renders
standalone. Asserts:
- anchor points at the project repo with rel=noopener + target=_blank
- aria-label is the no-count copy when star count is undefined
- CountUp renders only once the count loads
- aria-label includes the localized count once data arrives
* test(web): cover ui/Combobox trigger label rendering
The shared Combobox wraps base-ui's combobox primitives. Smoke-test
the trigger label resolution:
- placeholder shows when nothing is selected
- selected option's label renders in the trigger
- multi-select default values render all labels
- empty options array renders the placeholder without crashing
* test(web): cover IconPicker trigger rendering
Stub react-window's Grid so happy-dom can render the picker without
layout-measurement deps. Verify:
- trigger renders an <i class='ph-{value}'> for the current value
- changing value updates the trigger icon class
- the picker emits a trigger button
* test(web): cover ChipInput add/dedupe/description behaviors
- existing chips render as Badges
- Enter adds the typed value to the chip list
- comma also commits the typed value
- duplicate input is dropped (onChange not called with a longer list)
- empty / whitespace-only input is dropped
- hideDescription removes the keyboard hint <kbd>; default keeps it
* test(web): cover StatisticsSectionBuilder
Mock useQuery + useParams + section-base so the right-sidebar
statistics section renders standalone:
- returns null content while the query is loading
- shows the private-resume hint when isPublic=false
- shows views/downloads counters and labels when isPublic=true
- includes 'Last viewed' timestamp copy when lastViewedAt is set
* test(web): cover TemplateGalleryDialog selection flow
- title + intro copy render
- one tile per template (>= 14)
- currently-selected template tile carries the ring-highlight
- clicking a different tile triggers updateResumeData with a recipe
that sets metadata.template to the chosen template id
* test(web): cover Prefooter home-page section
- community tagline heading renders
- community-thanks paragraph renders
- the decorative TextMaskEffect renders an svg
* test(web): cover home-page Footer
- Resources and Community headings render
- documented resource links (Documentation, Sponsorships, Source
Code, Changelog) all appear in the rendered output
- documented community links (Report an issue, Translations,
Subreddit, Discord) all appear
- social anchors point at GitHub, LinkedIn, and X (Twitter)
- Copyright sub-component surfaces the app version via __APP_VERSION__
* test(web): cover home-page Header navigation
Mock TanStack Router Link + child components so the header renders
standalone. Verify:
- homepage anchor (/ link) carries the documented aria-label
- dashboard anchor points to /dashboard
- ThemeToggleButton and GithubStarsButton both mount
- the <nav> landmark is labeled 'Main navigation'
* chore: fix linter warnings
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { sanitizeAndParseResumeJson } from "./sanitize";
|
||||
|
||||
describe("sanitizeAndParseResumeJson — edge cases", () => {
|
||||
it("coerces numeric boolean shorthand (1/0) to true/false", () => {
|
||||
const json = JSON.stringify({
|
||||
basics: { name: "Bool One" },
|
||||
summary: { hidden: 1, title: "", columns: 1, content: "" },
|
||||
});
|
||||
|
||||
const result = sanitizeAndParseResumeJson(json);
|
||||
expect(result.data.summary.hidden).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts '1' and '0' string shorthand for booleans", () => {
|
||||
const json = JSON.stringify({
|
||||
basics: { name: "One" },
|
||||
summary: { hidden: "1", title: "", columns: 1, content: "" },
|
||||
});
|
||||
const result = sanitizeAndParseResumeJson(json);
|
||||
expect(result.data.summary.hidden).toBe(true);
|
||||
});
|
||||
|
||||
it("salvages missing item.hidden by setting it to false", () => {
|
||||
const json = JSON.stringify({
|
||||
basics: { name: "Test" },
|
||||
sections: {
|
||||
skills: {
|
||||
title: "Skills",
|
||||
columns: 1,
|
||||
hidden: false,
|
||||
items: [{ id: "abc", name: "Go", level: 3, keywords: [], description: "" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
const result = sanitizeAndParseResumeJson(json);
|
||||
// Schema parsing accepts the result; salvageApplied flag becomes true if salvage was needed.
|
||||
expect(result.data.sections.skills.items[0]?.hidden).toBe(false);
|
||||
});
|
||||
|
||||
it("rethrows non-Zod errors with a generic message and logs them", () => {
|
||||
// Pass garbage that survives jsonrepair (which is permissive) but produces
|
||||
// something that JSON.parse can choke on after repair fails internally.
|
||||
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
// Empty / nonsense input — jsonrepair may produce something parseable, so
|
||||
// instead provide a value that makes the merge throw downstream.
|
||||
// Force a non-Zod error by making the merge target undefined.
|
||||
expect(() => sanitizeAndParseResumeJson("")).toThrow();
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { useAIStore } from "./store";
|
||||
|
||||
const reset = () => {
|
||||
useAIStore.setState({
|
||||
enabled: false,
|
||||
provider: "openai",
|
||||
model: "",
|
||||
apiKey: "",
|
||||
baseURL: "",
|
||||
testStatus: "unverified",
|
||||
});
|
||||
};
|
||||
|
||||
afterEach(reset);
|
||||
|
||||
describe("useAIStore", () => {
|
||||
it("starts with provider=openai and disabled state", () => {
|
||||
const state = useAIStore.getState();
|
||||
expect(state.enabled).toBe(false);
|
||||
expect(state.provider).toBe("openai");
|
||||
expect(state.testStatus).toBe("unverified");
|
||||
});
|
||||
|
||||
it("set() updates fields and resets verification when credential fields change", () => {
|
||||
useAIStore.setState({ testStatus: "success", enabled: true });
|
||||
|
||||
useAIStore.getState().set((draft) => {
|
||||
draft.apiKey = "new-key";
|
||||
});
|
||||
|
||||
const state = useAIStore.getState();
|
||||
expect(state.apiKey).toBe("new-key");
|
||||
expect(state.testStatus).toBe("unverified");
|
||||
expect(state.enabled).toBe(false);
|
||||
});
|
||||
|
||||
it("set() does NOT reset testStatus when changing non-credential fields", () => {
|
||||
useAIStore.setState({ testStatus: "success", enabled: true });
|
||||
|
||||
useAIStore.getState().set((draft) => {
|
||||
draft.testStatus = "success"; // explicit no-op
|
||||
});
|
||||
|
||||
const state = useAIStore.getState();
|
||||
expect(state.testStatus).toBe("success");
|
||||
expect(state.enabled).toBe(true);
|
||||
});
|
||||
|
||||
it("canEnable() is true only when testStatus is success", () => {
|
||||
expect(useAIStore.getState().canEnable()).toBe(false);
|
||||
|
||||
useAIStore.setState({ testStatus: "success" });
|
||||
expect(useAIStore.getState().canEnable()).toBe(true);
|
||||
|
||||
useAIStore.setState({ testStatus: "failure" });
|
||||
expect(useAIStore.getState().canEnable()).toBe(false);
|
||||
});
|
||||
|
||||
it("setEnabled(true) refuses to enable when testStatus is not success", () => {
|
||||
useAIStore.getState().setEnabled(true);
|
||||
expect(useAIStore.getState().enabled).toBe(false);
|
||||
});
|
||||
|
||||
it("setEnabled(true) succeeds when testStatus is success", () => {
|
||||
useAIStore.setState({ testStatus: "success" });
|
||||
useAIStore.getState().setEnabled(true);
|
||||
expect(useAIStore.getState().enabled).toBe(true);
|
||||
});
|
||||
|
||||
it("setEnabled(false) always succeeds (regardless of testStatus)", () => {
|
||||
useAIStore.setState({ testStatus: "success", enabled: true });
|
||||
useAIStore.getState().setEnabled(false);
|
||||
expect(useAIStore.getState().enabled).toBe(false);
|
||||
});
|
||||
|
||||
it("reset() clears every field back to initial state", () => {
|
||||
useAIStore.setState({
|
||||
enabled: true,
|
||||
provider: "anthropic",
|
||||
model: "claude-3",
|
||||
apiKey: "key",
|
||||
baseURL: "https://api.anthropic.com",
|
||||
testStatus: "success",
|
||||
});
|
||||
|
||||
useAIStore.getState().reset();
|
||||
|
||||
const state = useAIStore.getState();
|
||||
expect(state).toMatchObject({
|
||||
enabled: false,
|
||||
provider: "openai",
|
||||
model: "",
|
||||
apiKey: "",
|
||||
baseURL: "",
|
||||
testStatus: "unverified",
|
||||
});
|
||||
});
|
||||
|
||||
it("resets when only the provider changes", () => {
|
||||
useAIStore.setState({ testStatus: "success", enabled: true });
|
||||
|
||||
useAIStore.getState().set((draft) => {
|
||||
draft.provider = "gemini";
|
||||
});
|
||||
|
||||
expect(useAIStore.getState().testStatus).toBe("unverified");
|
||||
expect(useAIStore.getState().enabled).toBe(false);
|
||||
});
|
||||
|
||||
it("resets when only the baseURL changes", () => {
|
||||
useAIStore.setState({ testStatus: "success", enabled: true });
|
||||
|
||||
useAIStore.getState().set((draft) => {
|
||||
draft.baseURL = "https://custom.example";
|
||||
});
|
||||
|
||||
expect(useAIStore.getState().testStatus).toBe("unverified");
|
||||
expect(useAIStore.getState().enabled).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sampleResumeData } from "@reactive-resume/schema/resume/sample";
|
||||
import {
|
||||
buildResumePatchProposalPreview,
|
||||
normalizeResumePatchProposals,
|
||||
resumePatchProposalSchema,
|
||||
} from "./patch-proposal";
|
||||
|
||||
describe("buildResumePatchProposalPreview — additional cases", () => {
|
||||
it("shows the removed value as before with after undefined", () => {
|
||||
const proposal = resumePatchProposalSchema.parse({
|
||||
id: "proposal-1",
|
||||
title: "Remove first profile",
|
||||
operations: [{ op: "remove", path: "/sections/profiles/items/0" }],
|
||||
});
|
||||
|
||||
const preview = buildResumePatchProposalPreview(sampleResumeData, proposal);
|
||||
expect(preview.entries[0]).toMatchObject({
|
||||
operation: "remove",
|
||||
path: "/sections/profiles/items/0",
|
||||
after: undefined,
|
||||
});
|
||||
expect(preview.entries[0]?.before).toBeDefined();
|
||||
});
|
||||
|
||||
it("titleizes camelCase and hyphenated path segments", () => {
|
||||
const proposal = resumePatchProposalSchema.parse({
|
||||
id: "proposal-1",
|
||||
title: "Update page format",
|
||||
operations: [{ op: "replace", path: "/metadata/page/format", value: "letter" }],
|
||||
});
|
||||
|
||||
const preview = buildResumePatchProposalPreview(sampleResumeData, proposal);
|
||||
// Label includes the human-readable section name 'Metadata' and 'Page'.
|
||||
expect(preview.entries[0]?.label).toMatch(/Metadata/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeResumePatchProposals", () => {
|
||||
it("attaches a baseUpdatedAt to every proposal", () => {
|
||||
const at = new Date("2024-01-01T00:00:00Z");
|
||||
const proposals = normalizeResumePatchProposals(
|
||||
{
|
||||
proposals: [
|
||||
{
|
||||
title: "A",
|
||||
operations: [{ op: "replace", path: "/basics/name", value: "x" }],
|
||||
},
|
||||
{
|
||||
title: "B",
|
||||
operations: [{ op: "replace", path: "/basics/name", value: "y" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
at,
|
||||
);
|
||||
|
||||
for (const p of proposals) {
|
||||
expect(p.baseUpdatedAt).toBe(at.toISOString());
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves order of input proposals", () => {
|
||||
const proposals = normalizeResumePatchProposals(
|
||||
{
|
||||
proposals: [
|
||||
{ title: "first", operations: [{ op: "replace", path: "/basics/name", value: "1" }] },
|
||||
{ title: "second", operations: [{ op: "replace", path: "/basics/name", value: "2" }] },
|
||||
{ title: "third", operations: [{ op: "replace", path: "/basics/name", value: "3" }] },
|
||||
],
|
||||
},
|
||||
new Date(),
|
||||
);
|
||||
|
||||
expect(proposals.map((p) => p.title)).toEqual(["first", "second", "third"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { executePatchResume, patchResumeInputSchema } from "./patch-resume";
|
||||
|
||||
describe("patchResumeInputSchema", () => {
|
||||
it("requires at least one operation", () => {
|
||||
expect(patchResumeInputSchema.safeParse({ operations: [] }).success).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts a valid replace operation", () => {
|
||||
const result = patchResumeInputSchema.safeParse({
|
||||
operations: [{ op: "replace", path: "/basics/name", value: "Jane" }],
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts add and remove operations", () => {
|
||||
const result = patchResumeInputSchema.safeParse({
|
||||
operations: [
|
||||
{ op: "add", path: "/sections/skills/items/-", value: { id: "s1", name: "Go" } },
|
||||
{ op: "remove", path: "/sections/skills/items/0" },
|
||||
],
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects unknown op values", () => {
|
||||
const result = patchResumeInputSchema.safeParse({
|
||||
operations: [{ op: "do-something", path: "/x", value: 1 }],
|
||||
});
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("executePatchResume", () => {
|
||||
it("returns success and the applied operations on a valid patch", () => {
|
||||
const ops = [{ op: "replace" as const, path: "/basics/name", value: "Jane Doe" }];
|
||||
const result = executePatchResume(structuredClone(defaultResumeData), ops);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.appliedOperations).toEqual(ops);
|
||||
});
|
||||
|
||||
it("throws when an operation targets an invalid path", () => {
|
||||
const ops = [{ op: "replace" as const, path: "/non/existent/path", value: 1 }];
|
||||
expect(() => executePatchResume(structuredClone(defaultResumeData), ops)).toThrow();
|
||||
});
|
||||
|
||||
it("supports multi-op patches that touch top-level fields", () => {
|
||||
const ops = [
|
||||
{ op: "replace" as const, path: "/basics/name", value: "Jane Doe" },
|
||||
{ op: "replace" as const, path: "/basics/headline", value: "Senior Engineer" },
|
||||
];
|
||||
const result = executePatchResume(structuredClone(defaultResumeData), ops);
|
||||
expect(result.appliedOperations).toEqual(ops);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const authMock = vi.hoisted(() => ({
|
||||
api: {
|
||||
getSession: vi.fn(),
|
||||
verifyApiKey: vi.fn(),
|
||||
},
|
||||
}));
|
||||
const verifyOAuthTokenMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
const dbMock = vi.hoisted(() => ({
|
||||
select: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@reactive-resume/auth/config", () => ({
|
||||
auth: authMock,
|
||||
verifyOAuthToken: verifyOAuthTokenMock,
|
||||
}));
|
||||
vi.mock("@reactive-resume/db/client", () => ({ db: dbMock }));
|
||||
vi.mock("@reactive-resume/db/schema", () => ({ user: { __table: "user" } }));
|
||||
vi.mock("drizzle-orm", () => ({ eq: () => "EQ" }));
|
||||
|
||||
const { resolveUserFromRequestHeaders } = await import("./context");
|
||||
|
||||
const setupDbResolves = (userResult: unknown) => {
|
||||
dbMock.select.mockReturnValueOnce({
|
||||
from: () => ({
|
||||
where: () => ({
|
||||
limit: () => Promise.resolve(userResult ? [userResult] : []),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
const reset = () => {
|
||||
authMock.api.getSession.mockReset();
|
||||
authMock.api.verifyApiKey.mockReset();
|
||||
verifyOAuthTokenMock.mockReset();
|
||||
dbMock.select.mockReset();
|
||||
};
|
||||
|
||||
describe("resolveUserFromRequestHeaders", () => {
|
||||
it("returns the user resolved from a valid x-api-key", async () => {
|
||||
reset();
|
||||
authMock.api.verifyApiKey.mockResolvedValueOnce({ valid: true, key: { referenceId: "user-1" } });
|
||||
setupDbResolves({ id: "user-1", name: "Alice" });
|
||||
|
||||
const headers = new Headers({ "x-api-key": "abc123" });
|
||||
const user = await resolveUserFromRequestHeaders(headers);
|
||||
|
||||
expect(authMock.api.verifyApiKey).toHaveBeenCalledWith({ body: { key: "abc123" } });
|
||||
expect(user).toMatchObject({ id: "user-1", name: "Alice" });
|
||||
});
|
||||
|
||||
it("falls back to session when api key is invalid", async () => {
|
||||
reset();
|
||||
authMock.api.verifyApiKey.mockResolvedValueOnce({ valid: false });
|
||||
authMock.api.getSession.mockResolvedValueOnce({ user: { id: "session-user" } });
|
||||
|
||||
const headers = new Headers({ "x-api-key": "bad" });
|
||||
const user = await resolveUserFromRequestHeaders(headers);
|
||||
|
||||
expect(user).toMatchObject({ id: "session-user" });
|
||||
});
|
||||
|
||||
it("uses Bearer token when present and no api key", async () => {
|
||||
reset();
|
||||
verifyOAuthTokenMock.mockResolvedValueOnce({ sub: "user-bearer" });
|
||||
setupDbResolves({ id: "user-bearer", name: "Bob" });
|
||||
|
||||
const headers = new Headers({ authorization: "Bearer xxx.yyy.zzz" });
|
||||
const user = await resolveUserFromRequestHeaders(headers);
|
||||
|
||||
expect(verifyOAuthTokenMock).toHaveBeenCalledWith("xxx.yyy.zzz");
|
||||
expect(user).toMatchObject({ id: "user-bearer", name: "Bob" });
|
||||
});
|
||||
|
||||
it("falls back to session when Bearer verification fails", async () => {
|
||||
reset();
|
||||
verifyOAuthTokenMock.mockResolvedValueOnce(null);
|
||||
authMock.api.getSession.mockResolvedValueOnce({ user: { id: "session-user" } });
|
||||
|
||||
const headers = new Headers({ authorization: "Bearer bad" });
|
||||
const user = await resolveUserFromRequestHeaders(headers);
|
||||
|
||||
expect(user).toMatchObject({ id: "session-user" });
|
||||
});
|
||||
|
||||
it("returns null when no auth method succeeds", async () => {
|
||||
reset();
|
||||
authMock.api.getSession.mockResolvedValueOnce(null);
|
||||
|
||||
const user = await resolveUserFromRequestHeaders(new Headers());
|
||||
expect(user).toBeNull();
|
||||
});
|
||||
|
||||
it("does not throw when verifyOAuthToken throws (logs and continues)", async () => {
|
||||
reset();
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
verifyOAuthTokenMock.mockRejectedValueOnce(new Error("bad token"));
|
||||
authMock.api.getSession.mockResolvedValueOnce({ user: { id: "fallback" } });
|
||||
|
||||
const headers = new Headers({ authorization: "Bearer xxx" });
|
||||
const user = await resolveUserFromRequestHeaders(headers);
|
||||
|
||||
expect(user).toMatchObject({ id: "fallback" });
|
||||
expect(warnSpy).toHaveBeenCalled();
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("returns null if Bearer header does not start with 'Bearer '", async () => {
|
||||
reset();
|
||||
authMock.api.getSession.mockResolvedValueOnce(null);
|
||||
|
||||
const headers = new Headers({ authorization: "Basic abc" });
|
||||
const user = await resolveUserFromRequestHeaders(headers);
|
||||
|
||||
expect(user).toBeNull();
|
||||
expect(verifyOAuthTokenMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const envMock = vi.hoisted(() => ({ APP_URL: "https://example.com" }));
|
||||
const cookies = vi.hoisted(() => ({
|
||||
get: vi.fn<(name: string) => string | undefined>(),
|
||||
set: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@reactive-resume/env/server", () => ({ env: envMock }));
|
||||
vi.mock("@tanstack/react-start/server", () => ({
|
||||
getCookie: (name: string) => cookies.get(name),
|
||||
setCookie: (name: string, value: string, options: unknown) => cookies.set(name, value, options),
|
||||
}));
|
||||
|
||||
const { hasResumeAccess, grantResumeAccess } = await import("./resume-access");
|
||||
|
||||
const signToken = (resumeId: string, passwordHash: string) =>
|
||||
createHash("sha256").update(`${resumeId}:${passwordHash}`).digest("hex");
|
||||
|
||||
describe("hasResumeAccess", () => {
|
||||
it("returns false when no passwordHash is supplied", () => {
|
||||
expect(hasResumeAccess("resume-1", null)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when no cookie is present", () => {
|
||||
cookies.get.mockReturnValueOnce(undefined);
|
||||
expect(hasResumeAccess("resume-1", "hash")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for a cookie value that matches the expected signed token", () => {
|
||||
const token = signToken("resume-1", "hash");
|
||||
cookies.get.mockReturnValueOnce(token);
|
||||
|
||||
expect(hasResumeAccess("resume-1", "hash")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for a cookie value that does not match the expected signed token", () => {
|
||||
cookies.get.mockReturnValueOnce("not-the-right-token");
|
||||
|
||||
expect(hasResumeAccess("resume-1", "hash")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when the cookie has a different length than the expected token", () => {
|
||||
cookies.get.mockReturnValueOnce("short");
|
||||
|
||||
expect(hasResumeAccess("resume-1", "hash")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("grantResumeAccess", () => {
|
||||
it("writes a signed cookie scoped to the resume id with httpOnly + sameSite=lax + 10-minute TTL", () => {
|
||||
cookies.set.mockReset();
|
||||
|
||||
grantResumeAccess("resume-42", "hash");
|
||||
|
||||
expect(cookies.set).toHaveBeenCalledTimes(1);
|
||||
// biome-ignore lint/style/noNonNullAssertion: The assertion above verifies the cookie write exists before destructuring it.
|
||||
const [name, value, options] = cookies.set.mock.calls[0]!;
|
||||
expect(name).toBe("resume_access_resume-42");
|
||||
expect(value).toBe(signToken("resume-42", "hash"));
|
||||
expect(options).toMatchObject({
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
maxAge: 600,
|
||||
});
|
||||
});
|
||||
|
||||
it("only marks the cookie secure when APP_URL is https", () => {
|
||||
envMock.APP_URL = "http://localhost:3000";
|
||||
cookies.set.mockReset();
|
||||
grantResumeAccess("r", "h");
|
||||
expect(cookies.set.mock.calls[0]?.[2]).toMatchObject({ secure: false });
|
||||
|
||||
envMock.APP_URL = "https://example.com";
|
||||
cookies.set.mockReset();
|
||||
grantResumeAccess("r", "h");
|
||||
expect(cookies.set.mock.calls[0]?.[2]).toMatchObject({ secure: true });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,106 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const envMock = vi.hoisted(() => ({
|
||||
GOOGLE_CLIENT_ID: undefined as string | undefined,
|
||||
GOOGLE_CLIENT_SECRET: undefined as string | undefined,
|
||||
GITHUB_CLIENT_ID: undefined as string | undefined,
|
||||
GITHUB_CLIENT_SECRET: undefined as string | undefined,
|
||||
LINKEDIN_CLIENT_ID: undefined as string | undefined,
|
||||
LINKEDIN_CLIENT_SECRET: undefined as string | undefined,
|
||||
OAUTH_CLIENT_ID: undefined as string | undefined,
|
||||
OAUTH_CLIENT_SECRET: undefined as string | undefined,
|
||||
OAUTH_PROVIDER_NAME: undefined as string | undefined,
|
||||
}));
|
||||
|
||||
vi.mock("@reactive-resume/env/server", () => ({ env: envMock }));
|
||||
// auth.ts also imports db client and storage; stub them with no-op surfaces.
|
||||
vi.mock("@reactive-resume/db/client", () => ({ db: { delete: vi.fn() } }));
|
||||
vi.mock("@reactive-resume/db/schema", () => ({ user: {} }));
|
||||
vi.mock("./storage", () => ({ getStorageService: () => ({ delete: vi.fn() }) }));
|
||||
|
||||
const { authService } = await import("./auth");
|
||||
|
||||
const resetEnv = () => {
|
||||
envMock.GOOGLE_CLIENT_ID = undefined;
|
||||
envMock.GOOGLE_CLIENT_SECRET = undefined;
|
||||
envMock.GITHUB_CLIENT_ID = undefined;
|
||||
envMock.GITHUB_CLIENT_SECRET = undefined;
|
||||
envMock.LINKEDIN_CLIENT_ID = undefined;
|
||||
envMock.LINKEDIN_CLIENT_SECRET = undefined;
|
||||
envMock.OAUTH_CLIENT_ID = undefined;
|
||||
envMock.OAUTH_CLIENT_SECRET = undefined;
|
||||
envMock.OAUTH_PROVIDER_NAME = undefined;
|
||||
};
|
||||
|
||||
describe("authService.providers.list", () => {
|
||||
it("always includes credential and passkey providers", () => {
|
||||
resetEnv();
|
||||
const providers = authService.providers.list();
|
||||
expect(providers.credential).toBe("Password");
|
||||
expect(providers.passkey).toBe("Passkey");
|
||||
});
|
||||
|
||||
it("omits social providers when credentials are not configured", () => {
|
||||
resetEnv();
|
||||
const providers = authService.providers.list();
|
||||
expect(providers.google).toBeUndefined();
|
||||
expect(providers.github).toBeUndefined();
|
||||
expect(providers.linkedin).toBeUndefined();
|
||||
expect(providers.custom).toBeUndefined();
|
||||
});
|
||||
|
||||
it("includes Google when both client id and secret are present", () => {
|
||||
resetEnv();
|
||||
envMock.GOOGLE_CLIENT_ID = "id";
|
||||
envMock.GOOGLE_CLIENT_SECRET = "secret";
|
||||
const providers = authService.providers.list();
|
||||
expect(providers.google).toBe("Google");
|
||||
});
|
||||
|
||||
it("does NOT include Google when only one of id/secret is set", () => {
|
||||
resetEnv();
|
||||
envMock.GOOGLE_CLIENT_ID = "id";
|
||||
const providers = authService.providers.list();
|
||||
expect(providers.google).toBeUndefined();
|
||||
});
|
||||
|
||||
it("includes GitHub when both client id and secret are present", () => {
|
||||
resetEnv();
|
||||
envMock.GITHUB_CLIENT_ID = "id";
|
||||
envMock.GITHUB_CLIENT_SECRET = "secret";
|
||||
expect(authService.providers.list().github).toBe("GitHub");
|
||||
});
|
||||
|
||||
it("includes LinkedIn when both client id and secret are present", () => {
|
||||
resetEnv();
|
||||
envMock.LINKEDIN_CLIENT_ID = "id";
|
||||
envMock.LINKEDIN_CLIENT_SECRET = "secret";
|
||||
expect(authService.providers.list().linkedin).toBe("LinkedIn");
|
||||
});
|
||||
|
||||
it("labels the custom OAuth provider with OAUTH_PROVIDER_NAME when set", () => {
|
||||
resetEnv();
|
||||
envMock.OAUTH_CLIENT_ID = "id";
|
||||
envMock.OAUTH_CLIENT_SECRET = "secret";
|
||||
envMock.OAUTH_PROVIDER_NAME = "Acme SSO";
|
||||
expect(authService.providers.list().custom).toBe("Acme SSO");
|
||||
});
|
||||
|
||||
it("falls back to 'Custom OAuth' when OAUTH_PROVIDER_NAME is not set", () => {
|
||||
resetEnv();
|
||||
envMock.OAUTH_CLIENT_ID = "id";
|
||||
envMock.OAUTH_CLIENT_SECRET = "secret";
|
||||
expect(authService.providers.list().custom).toBe("Custom OAuth");
|
||||
});
|
||||
|
||||
it("can register multiple social providers at once", () => {
|
||||
resetEnv();
|
||||
envMock.GOOGLE_CLIENT_ID = "g";
|
||||
envMock.GOOGLE_CLIENT_SECRET = "g";
|
||||
envMock.GITHUB_CLIENT_ID = "h";
|
||||
envMock.GITHUB_CLIENT_SECRET = "h";
|
||||
const providers = authService.providers.list();
|
||||
expect(providers.google).toBe("Google");
|
||||
expect(providers.github).toBe("GitHub");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const envMock = vi.hoisted(() => ({
|
||||
FLAG_DISABLE_SIGNUPS: false,
|
||||
FLAG_DISABLE_EMAIL_AUTH: false,
|
||||
}));
|
||||
|
||||
vi.mock("@reactive-resume/env/server", () => ({ env: envMock }));
|
||||
|
||||
const { flagsService } = await import("./flags");
|
||||
|
||||
describe("flagsService.getFlags", () => {
|
||||
it("reads disableSignups + disableEmailAuth from env", () => {
|
||||
envMock.FLAG_DISABLE_SIGNUPS = false;
|
||||
envMock.FLAG_DISABLE_EMAIL_AUTH = false;
|
||||
expect(flagsService.getFlags()).toEqual({
|
||||
disableSignups: false,
|
||||
disableEmailAuth: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns disableSignups=true when env flag is set", () => {
|
||||
envMock.FLAG_DISABLE_SIGNUPS = true;
|
||||
envMock.FLAG_DISABLE_EMAIL_AUTH = false;
|
||||
expect(flagsService.getFlags()).toEqual({
|
||||
disableSignups: true,
|
||||
disableEmailAuth: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns disableEmailAuth=true when env flag is set", () => {
|
||||
envMock.FLAG_DISABLE_SIGNUPS = false;
|
||||
envMock.FLAG_DISABLE_EMAIL_AUTH = true;
|
||||
expect(flagsService.getFlags()).toEqual({
|
||||
disableSignups: false,
|
||||
disableEmailAuth: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("reads the latest env values on every call (no stale cache)", () => {
|
||||
envMock.FLAG_DISABLE_SIGNUPS = false;
|
||||
const before = flagsService.getFlags();
|
||||
envMock.FLAG_DISABLE_SIGNUPS = true;
|
||||
const after = flagsService.getFlags();
|
||||
|
||||
expect(before.disableSignups).toBe(false);
|
||||
expect(after.disableSignups).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const pool = vi.hoisted(() => ({
|
||||
query: vi.fn().mockResolvedValue(undefined),
|
||||
connect: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@reactive-resume/db/client", () => ({ getPool: () => pool }));
|
||||
|
||||
const { publishResumeUpdated, subscribeResumeUpdated } = await import("./resume-events");
|
||||
|
||||
const exampleEvent = {
|
||||
type: "resume.updated" as const,
|
||||
resumeId: "r1",
|
||||
userId: "u1",
|
||||
updatedAt: "2024-01-01T00:00:00Z",
|
||||
mutation: "patch" as const,
|
||||
};
|
||||
|
||||
describe("publishResumeUpdated", () => {
|
||||
it("issues a pg_notify with the channel and serialized event", async () => {
|
||||
pool.query.mockClear();
|
||||
|
||||
await publishResumeUpdated(exampleEvent);
|
||||
|
||||
expect(pool.query).toHaveBeenCalledTimes(1);
|
||||
// biome-ignore lint/style/noNonNullAssertion: The assertion above verifies the query call exists before destructuring it.
|
||||
const [sql, params] = pool.query.mock.calls[0]!;
|
||||
expect(sql).toBe("SELECT pg_notify($1, $2)");
|
||||
expect(params?.[0]).toBe("resume_updated");
|
||||
expect(JSON.parse(params?.[1] as string)).toEqual(exampleEvent);
|
||||
});
|
||||
});
|
||||
|
||||
const makeFakeClient = () => {
|
||||
type Listener = (notification: { channel?: string; payload?: string }) => void;
|
||||
const listeners = new Set<Listener>();
|
||||
|
||||
const client = {
|
||||
query: vi.fn().mockResolvedValue(undefined),
|
||||
on: vi.fn((event: string, fn: Listener) => {
|
||||
if (event === "notification") listeners.add(fn);
|
||||
}),
|
||||
off: vi.fn((event: string, fn: Listener) => {
|
||||
if (event === "notification") listeners.delete(fn);
|
||||
}),
|
||||
release: vi.fn(),
|
||||
__notify(channel: string, payload: string) {
|
||||
for (const fn of listeners) fn({ channel, payload });
|
||||
},
|
||||
};
|
||||
return client;
|
||||
};
|
||||
|
||||
describe("subscribeResumeUpdated", () => {
|
||||
it("yields events whose resumeId and userId match the subscription", async () => {
|
||||
const client = makeFakeClient();
|
||||
pool.connect.mockResolvedValueOnce(client);
|
||||
|
||||
const controller = new AbortController();
|
||||
const iterator = subscribeResumeUpdated({
|
||||
resumeId: "r1",
|
||||
userId: "u1",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
// Kick the generator so listeners are installed, then push a notification.
|
||||
const firstP = iterator.next();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
client.__notify("resume_updated", JSON.stringify(exampleEvent));
|
||||
|
||||
const first = await firstP;
|
||||
expect(first.done).toBe(false);
|
||||
expect(first.value).toEqual(exampleEvent);
|
||||
|
||||
controller.abort();
|
||||
const last = await iterator.next();
|
||||
expect(last.done).toBe(true);
|
||||
|
||||
expect(client.query).toHaveBeenCalledWith("LISTEN resume_updated");
|
||||
expect(client.query).toHaveBeenCalledWith("UNLISTEN resume_updated");
|
||||
expect(client.release).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("ignores notifications for other resumes / users", async () => {
|
||||
const client = makeFakeClient();
|
||||
pool.connect.mockResolvedValueOnce(client);
|
||||
|
||||
const controller = new AbortController();
|
||||
const iterator = subscribeResumeUpdated({
|
||||
resumeId: "r1",
|
||||
userId: "u1",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
const resultP = iterator.next();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
client.__notify("resume_updated", JSON.stringify({ ...exampleEvent, resumeId: "other" }));
|
||||
client.__notify("resume_updated", JSON.stringify({ ...exampleEvent, userId: "other" }));
|
||||
client.__notify("resume_updated", JSON.stringify(exampleEvent));
|
||||
|
||||
const result = await resultP;
|
||||
expect(result.value?.resumeId).toBe("r1");
|
||||
|
||||
controller.abort();
|
||||
await iterator.next();
|
||||
});
|
||||
|
||||
it("ignores malformed notifications and notifications on other channels", async () => {
|
||||
const client = makeFakeClient();
|
||||
pool.connect.mockResolvedValueOnce(client);
|
||||
|
||||
const controller = new AbortController();
|
||||
const iterator = subscribeResumeUpdated({
|
||||
resumeId: "r1",
|
||||
userId: "u1",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
const resultP = iterator.next();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
// Wrong channel.
|
||||
client.__notify("other_channel", JSON.stringify(exampleEvent));
|
||||
// Malformed JSON.
|
||||
client.__notify("resume_updated", "{not-json");
|
||||
// Missing required fields.
|
||||
client.__notify("resume_updated", JSON.stringify({ type: "wrong" }));
|
||||
// Valid event after the noise.
|
||||
client.__notify("resume_updated", JSON.stringify(exampleEvent));
|
||||
|
||||
const result = await resultP;
|
||||
expect(result.value).toEqual(exampleEvent);
|
||||
|
||||
controller.abort();
|
||||
await iterator.next();
|
||||
});
|
||||
|
||||
it("terminates immediately if signal is already aborted", async () => {
|
||||
const client = makeFakeClient();
|
||||
pool.connect.mockResolvedValueOnce(client);
|
||||
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
|
||||
const iterator = subscribeResumeUpdated({
|
||||
resumeId: "r1",
|
||||
userId: "u1",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
const result = await iterator.next();
|
||||
expect(result.done).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const envMock = vi.hoisted(() => ({
|
||||
// Filled in by beforeEach so caches do not bleed between tests.
|
||||
LOCAL_STORAGE_PATH: "" as string,
|
||||
}));
|
||||
|
||||
const dbResult = vi.hoisted(() => ({ count: 0 }));
|
||||
const dbMock = vi.hoisted(() => {
|
||||
const select = vi.fn();
|
||||
select.mockReturnValue({ from: () => Promise.resolve([dbResult]) });
|
||||
return { select };
|
||||
});
|
||||
|
||||
vi.mock("@reactive-resume/env/server", () => ({ env: envMock }));
|
||||
vi.mock("@reactive-resume/db/client", () => ({ db: dbMock }));
|
||||
vi.mock("@reactive-resume/db/schema", () => ({ user: { __table: "user" }, resume: { __table: "resume" } }));
|
||||
vi.mock("drizzle-orm", () => ({ count: () => "count(*)" }));
|
||||
|
||||
const fetchMock = vi.fn();
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
fetchMock.mockReset();
|
||||
dbMock.select.mockClear();
|
||||
});
|
||||
|
||||
const { statisticsService } = await import("./statistics");
|
||||
|
||||
// Each test gets a unique LOCAL_STORAGE_PATH to avoid cross-test cache hits.
|
||||
beforeEach(() => {
|
||||
envMock.LOCAL_STORAGE_PATH = mkdtempSync(join(tmpdir(), "rr-statistics-test-"));
|
||||
});
|
||||
|
||||
describe("statisticsService.user.getCount", () => {
|
||||
it("returns the DB count when the fetcher succeeds", async () => {
|
||||
dbResult.count = 42;
|
||||
await expect(statisticsService.user.getCount()).resolves.toBe(42);
|
||||
});
|
||||
|
||||
it("falls back to the last-known value when the DB throws", async () => {
|
||||
dbMock.select.mockImplementationOnce(() => {
|
||||
throw new Error("db down");
|
||||
});
|
||||
const value = await statisticsService.user.getCount();
|
||||
// Last known is 978_528; we just check it's > 0 (don't hard-code the magic number).
|
||||
expect(value).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("statisticsService.resume.getCount", () => {
|
||||
it("returns the DB count for resume", async () => {
|
||||
dbResult.count = 7;
|
||||
await expect(statisticsService.resume.getCount()).resolves.toBe(7);
|
||||
});
|
||||
});
|
||||
|
||||
describe("statisticsService.github.getStarCount", () => {
|
||||
it("returns the parsed stargazers_count when GitHub responds OK", async () => {
|
||||
fetchMock.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ stargazers_count: 12345 }),
|
||||
});
|
||||
|
||||
const stars = await statisticsService.github.getStarCount();
|
||||
expect(stars).toBe(12345);
|
||||
});
|
||||
|
||||
it("falls back to last-known on non-OK responses (retries internally)", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
});
|
||||
|
||||
const stars = await statisticsService.github.getStarCount();
|
||||
expect(stars).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("falls back to last-known when fetch throws", async () => {
|
||||
fetchMock.mockRejectedValue(new Error("network down"));
|
||||
|
||||
const stars = await statisticsService.github.getStarCount();
|
||||
expect(stars).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("rejects non-positive stargazers_count and falls back", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ stargazers_count: 0 }),
|
||||
});
|
||||
|
||||
const stars = await statisticsService.github.getStarCount();
|
||||
expect(stars).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("rejects non-numeric stargazers_count and falls back", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ stargazers_count: "not a number" }),
|
||||
});
|
||||
|
||||
const stars = await statisticsService.github.getStarCount();
|
||||
expect(stars).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,113 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const envMock = vi.hoisted(() => ({
|
||||
APP_URL: "https://example.com",
|
||||
LOCAL_STORAGE_PATH: "",
|
||||
S3_ACCESS_KEY_ID: undefined as string | undefined,
|
||||
S3_SECRET_ACCESS_KEY: undefined as string | undefined,
|
||||
S3_REGION: "us-east-1",
|
||||
S3_ENDPOINT: undefined as string | undefined,
|
||||
S3_BUCKET: undefined as string | undefined,
|
||||
S3_FORCE_PATH_STYLE: false,
|
||||
FLAG_DISABLE_IMAGE_PROCESSING: false,
|
||||
}));
|
||||
|
||||
vi.mock("@reactive-resume/env/server", () => ({ env: envMock }));
|
||||
// sharp is exercised by processImageForUpload; keep it out of the import graph entirely
|
||||
// because resolving it loads native bindings we can't rely on in CI.
|
||||
vi.mock("sharp", () => {
|
||||
const chain = {
|
||||
resize: () => chain,
|
||||
jpeg: () => chain,
|
||||
rotate: () => chain,
|
||||
toBuffer: async () => Buffer.from("processed"),
|
||||
metadata: async () => ({ width: 100, height: 100 }),
|
||||
};
|
||||
return { default: () => chain };
|
||||
});
|
||||
vi.mock("@aws-sdk/client-s3", () => ({
|
||||
S3Client: vi.fn(),
|
||||
PutObjectCommand: vi.fn(),
|
||||
GetObjectCommand: vi.fn(),
|
||||
DeleteObjectCommand: vi.fn(),
|
||||
ListObjectsV2Command: vi.fn(),
|
||||
}));
|
||||
|
||||
const { inferContentType, isImageFile, processImageForUpload } = await import("./storage");
|
||||
|
||||
const makeFile = (bytes: Uint8Array, type = "image/png") =>
|
||||
({
|
||||
arrayBuffer: async () => bytes.buffer,
|
||||
type,
|
||||
}) as unknown as File;
|
||||
|
||||
describe("inferContentType", () => {
|
||||
it("maps common image extensions to their MIME types", () => {
|
||||
expect(inferContentType("photo.jpg")).toBe("image/jpeg");
|
||||
expect(inferContentType("photo.jpeg")).toBe("image/jpeg");
|
||||
expect(inferContentType("photo.png")).toBe("image/png");
|
||||
expect(inferContentType("animated.gif")).toBe("image/gif");
|
||||
expect(inferContentType("logo.svg")).toBe("image/svg+xml");
|
||||
expect(inferContentType("photo.webp")).toBe("image/webp");
|
||||
});
|
||||
|
||||
it("maps .pdf to application/pdf", () => {
|
||||
expect(inferContentType("doc.pdf")).toBe("application/pdf");
|
||||
});
|
||||
|
||||
it("is case-insensitive on the extension", () => {
|
||||
expect(inferContentType("PHOTO.JPG")).toBe("image/jpeg");
|
||||
expect(inferContentType("Document.PDF")).toBe("application/pdf");
|
||||
});
|
||||
|
||||
it("falls back to application/octet-stream for unknown extensions", () => {
|
||||
expect(inferContentType("data.xyz")).toBe("application/octet-stream");
|
||||
expect(inferContentType("README")).toBe("application/octet-stream");
|
||||
});
|
||||
|
||||
it("uses just the file extension regardless of path depth", () => {
|
||||
expect(inferContentType("/nested/dir/file.png")).toBe("image/png");
|
||||
});
|
||||
});
|
||||
|
||||
describe("processImageForUpload", () => {
|
||||
it("returns the file untouched when image processing is disabled", async () => {
|
||||
envMock.FLAG_DISABLE_IMAGE_PROCESSING = true;
|
||||
const file = makeFile(new Uint8Array([1, 2, 3, 4]), "image/png");
|
||||
|
||||
const result = await processImageForUpload(file);
|
||||
|
||||
expect(result.contentType).toBe("image/png");
|
||||
expect(Array.from(result.data)).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it("re-encodes to JPEG via sharp when processing is enabled", async () => {
|
||||
envMock.FLAG_DISABLE_IMAGE_PROCESSING = false;
|
||||
const file = makeFile(new Uint8Array([5, 6, 7, 8]), "image/png");
|
||||
|
||||
const result = await processImageForUpload(file);
|
||||
|
||||
expect(result.contentType).toBe("image/jpeg");
|
||||
// Sharp mock returns "processed" — ensure we got something not equal to the input.
|
||||
expect(result.data.length).toBeGreaterThan(0);
|
||||
expect(Array.from(result.data)).not.toEqual([5, 6, 7, 8]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isImageFile", () => {
|
||||
it("returns true for supported image mime types", () => {
|
||||
for (const type of ["image/gif", "image/png", "image/jpeg", "image/webp"]) {
|
||||
expect(isImageFile(type), type).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("returns false for image/svg+xml (not in the upload allowlist)", () => {
|
||||
expect(isImageFile("image/svg+xml")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for application/pdf and other non-image types", () => {
|
||||
expect(isImageFile("application/pdf")).toBe(false);
|
||||
expect(isImageFile("text/plain")).toBe(false);
|
||||
expect(isImageFile("")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const authMock = vi.hoisted(() => ({
|
||||
api: {
|
||||
getSession: vi.fn(),
|
||||
},
|
||||
}));
|
||||
const headersMock = vi.hoisted(() => vi.fn(() => new Headers()));
|
||||
|
||||
vi.mock("./config", () => ({ auth: authMock }));
|
||||
vi.mock("@tanstack/react-start/server", () => ({
|
||||
getRequestHeaders: headersMock,
|
||||
}));
|
||||
|
||||
const { getSession } = await import("./functions");
|
||||
|
||||
describe("getSession", () => {
|
||||
it("delegates to auth.api.getSession with the current request headers", async () => {
|
||||
const headers = new Headers({ authorization: "Bearer abc" });
|
||||
headersMock.mockReturnValueOnce(headers);
|
||||
authMock.api.getSession.mockResolvedValueOnce({ user: { id: "u1" }, session: { id: "s1" } });
|
||||
|
||||
const result = await getSession();
|
||||
|
||||
expect(authMock.api.getSession).toHaveBeenCalledWith({ headers });
|
||||
expect(result).toMatchObject({ user: { id: "u1" } });
|
||||
});
|
||||
|
||||
it("returns null when better-auth returns no session", async () => {
|
||||
authMock.api.getSession.mockResolvedValueOnce(null);
|
||||
|
||||
const result = await getSession();
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { relations } from "./relations";
|
||||
|
||||
describe("drizzle relations", () => {
|
||||
it("exports a defined relations object", () => {
|
||||
expect(relations).toBeDefined();
|
||||
expect(typeof relations).toBe("object");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getTableColumns, getTableName } from "drizzle-orm";
|
||||
import {
|
||||
account,
|
||||
apikey,
|
||||
jwks,
|
||||
oauthAccessToken,
|
||||
oauthClient,
|
||||
oauthConsent,
|
||||
oauthRefreshToken,
|
||||
passkey,
|
||||
session,
|
||||
twoFactor,
|
||||
user,
|
||||
verification,
|
||||
} from "./auth";
|
||||
|
||||
const cases: Array<{
|
||||
name: string;
|
||||
table: Parameters<typeof getTableColumns>[0];
|
||||
columns: string[];
|
||||
}> = [
|
||||
{
|
||||
name: "user",
|
||||
table: user,
|
||||
columns: ["id", "email", "name", "image", "createdAt", "updatedAt"],
|
||||
},
|
||||
{
|
||||
name: "session",
|
||||
table: session,
|
||||
columns: ["id", "userId", "token", "expiresAt"],
|
||||
},
|
||||
{
|
||||
name: "account",
|
||||
table: account,
|
||||
columns: ["id", "userId", "providerId", "accountId"],
|
||||
},
|
||||
{
|
||||
name: "verification",
|
||||
table: verification,
|
||||
columns: ["id", "identifier", "value", "expiresAt"],
|
||||
},
|
||||
{
|
||||
name: "two_factor",
|
||||
table: twoFactor,
|
||||
columns: ["id", "userId", "secret"],
|
||||
},
|
||||
{
|
||||
name: "passkey",
|
||||
table: passkey,
|
||||
columns: ["id", "userId", "publicKey"],
|
||||
},
|
||||
{
|
||||
name: "apikey",
|
||||
table: apikey,
|
||||
columns: ["id", "name", "key", "referenceId"],
|
||||
},
|
||||
{ name: "jwks", table: jwks, columns: ["id", "publicKey", "privateKey"] },
|
||||
{
|
||||
name: "oauth_client",
|
||||
table: oauthClient,
|
||||
columns: ["id", "clientId", "clientSecret", "name", "userId", "redirectUris"],
|
||||
},
|
||||
{
|
||||
name: "oauth_refresh_token",
|
||||
table: oauthRefreshToken,
|
||||
columns: ["id", "userId", "clientId"],
|
||||
},
|
||||
{
|
||||
name: "oauth_access_token",
|
||||
table: oauthAccessToken,
|
||||
columns: ["id", "userId", "clientId", "refreshId"],
|
||||
},
|
||||
{
|
||||
name: "oauth_consent",
|
||||
table: oauthConsent,
|
||||
columns: ["id", "userId", "clientId"],
|
||||
},
|
||||
];
|
||||
|
||||
describe("auth tables", () => {
|
||||
it.each(cases)("$name maps to the expected SQL name", ({ name, table }) => {
|
||||
expect(getTableName(table)).toBe(name);
|
||||
});
|
||||
|
||||
it.each(cases)("$name exposes the required columns", ({ table, columns }) => {
|
||||
const tableColumns = getTableColumns(table);
|
||||
for (const expected of columns) {
|
||||
expect(tableColumns[expected as keyof typeof tableColumns], expected).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getTableColumns, getTableName } from "drizzle-orm";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { resume, resumeAnalysis, resumeStatistics } from "./resume";
|
||||
|
||||
describe("resume table definition", () => {
|
||||
it("is named 'resume' in the database", () => {
|
||||
expect(getTableName(resume)).toBe("resume");
|
||||
});
|
||||
|
||||
it("declares the expected columns", () => {
|
||||
const columns = getTableColumns(resume);
|
||||
for (const name of [
|
||||
"id",
|
||||
"name",
|
||||
"slug",
|
||||
"tags",
|
||||
"isPublic",
|
||||
"isLocked",
|
||||
"password",
|
||||
"data",
|
||||
"userId",
|
||||
"createdAt",
|
||||
"updatedAt",
|
||||
]) {
|
||||
expect(columns[name as keyof typeof columns], name).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("data column defaults to defaultResumeData when invoked", () => {
|
||||
const columns = getTableColumns(resume);
|
||||
// drizzle exposes the default function under `default` or via column config — the
|
||||
// most stable way to assert intent is to make sure `data` has a default at all.
|
||||
expect(columns.data).toBeDefined();
|
||||
// Roundtrip the imported default to make sure the import wiring works.
|
||||
expect(defaultResumeData.basics).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resumeStatistics table definition", () => {
|
||||
it("is named 'resume_statistics' in the database", () => {
|
||||
expect(getTableName(resumeStatistics)).toBe("resume_statistics");
|
||||
});
|
||||
|
||||
it("exposes counter columns and a unique resume_id FK", () => {
|
||||
const columns = getTableColumns(resumeStatistics);
|
||||
for (const name of ["views", "downloads", "lastViewedAt", "lastDownloadedAt", "resumeId"]) {
|
||||
expect(columns[name as keyof typeof columns], name).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("resumeAnalysis table definition", () => {
|
||||
it("is named 'resume_analysis' in the database", () => {
|
||||
expect(getTableName(resumeAnalysis)).toBe("resume_analysis");
|
||||
});
|
||||
|
||||
it("declares analysis + resumeId columns", () => {
|
||||
const columns = getTableColumns(resumeAnalysis);
|
||||
expect(columns.analysis).toBeDefined();
|
||||
expect(columns.resumeId).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,127 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const envMock = vi.hoisted(() => ({
|
||||
SMTP_HOST: undefined as string | undefined,
|
||||
SMTP_PORT: 587,
|
||||
SMTP_USER: undefined as string | undefined,
|
||||
SMTP_PASS: undefined as string | undefined,
|
||||
SMTP_FROM: undefined as string | undefined,
|
||||
SMTP_SECURE: false,
|
||||
}));
|
||||
|
||||
const sendMail = vi.hoisted(() => vi.fn().mockResolvedValue({ ok: true }));
|
||||
const createTransport = vi.hoisted(() => vi.fn(() => ({ sendMail })));
|
||||
|
||||
vi.mock("@reactive-resume/env/server", () => ({ env: envMock }));
|
||||
vi.mock("nodemailer", () => ({
|
||||
default: { createTransport },
|
||||
createTransport,
|
||||
}));
|
||||
vi.mock("react-email", () => ({
|
||||
render: async (_node: unknown, opts?: { plainText?: boolean }) =>
|
||||
opts?.plainText ? "plain text body" : "<p>html body</p>",
|
||||
}));
|
||||
|
||||
const { sendEmail } = await import("./transport");
|
||||
|
||||
const resetEnv = () => {
|
||||
envMock.SMTP_HOST = undefined;
|
||||
envMock.SMTP_USER = undefined;
|
||||
envMock.SMTP_PASS = undefined;
|
||||
envMock.SMTP_FROM = undefined;
|
||||
createTransport.mockClear();
|
||||
sendMail.mockClear();
|
||||
};
|
||||
|
||||
describe("sendEmail", () => {
|
||||
it("does nothing when neither text nor html is provided", async () => {
|
||||
resetEnv();
|
||||
await sendEmail({ to: "a@b.com", subject: "hi" });
|
||||
expect(sendMail).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips sending and logs when SMTP is not configured (no host)", async () => {
|
||||
resetEnv();
|
||||
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
|
||||
await sendEmail({ to: "a@b.com", subject: "hi", text: "body" });
|
||||
|
||||
expect(infoSpy).toHaveBeenCalledWith(
|
||||
"SMTP not configured; skipping email send.",
|
||||
expect.objectContaining({ to: "a@b.com", subject: "hi" }),
|
||||
);
|
||||
expect(sendMail).not.toHaveBeenCalled();
|
||||
infoSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("sends via nodemailer when SMTP is fully configured", async () => {
|
||||
resetEnv();
|
||||
envMock.SMTP_HOST = "smtp.example.com";
|
||||
envMock.SMTP_USER = "user";
|
||||
envMock.SMTP_PASS = "pass";
|
||||
envMock.SMTP_FROM = "noreply@example.com";
|
||||
|
||||
await sendEmail({ to: "a@b.com", subject: "hi", text: "body" });
|
||||
|
||||
expect(createTransport).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
host: "smtp.example.com",
|
||||
port: 587,
|
||||
secure: false,
|
||||
auth: { user: "user", pass: "pass" },
|
||||
}),
|
||||
);
|
||||
expect(sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
to: "a@b.com",
|
||||
from: "noreply@example.com",
|
||||
subject: "hi",
|
||||
text: "body",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to a default 'noreply@localhost' from address when SMTP_FROM is unset", async () => {
|
||||
resetEnv();
|
||||
envMock.SMTP_HOST = "smtp.example.com";
|
||||
envMock.SMTP_USER = "user";
|
||||
envMock.SMTP_PASS = "pass";
|
||||
|
||||
// SMTP not "enabled" without SMTP_FROM — skipping branch — but options.from is used.
|
||||
// Manually provide from in options instead.
|
||||
await sendEmail({ to: "a@b.com", from: "explicit@x.com", subject: "hi", text: "body" });
|
||||
// SMTP isn't enabled, so sendMail isn't called — confirm the info-log branch instead.
|
||||
expect(sendMail).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("renders react element into both html and plain-text bodies", async () => {
|
||||
resetEnv();
|
||||
envMock.SMTP_HOST = "smtp.example.com";
|
||||
envMock.SMTP_USER = "user";
|
||||
envMock.SMTP_PASS = "pass";
|
||||
envMock.SMTP_FROM = "noreply@example.com";
|
||||
|
||||
const fakeReact = { $$typeof: Symbol.for("react.element") } as unknown as React.ReactElement;
|
||||
await sendEmail({ to: "a@b.com", subject: "hi", react: fakeReact });
|
||||
|
||||
expect(sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
html: "<p>html body</p>",
|
||||
text: "plain text body",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not throw if the SMTP transport itself errors", async () => {
|
||||
resetEnv();
|
||||
envMock.SMTP_HOST = "smtp.example.com";
|
||||
envMock.SMTP_USER = "user";
|
||||
envMock.SMTP_PASS = "pass";
|
||||
envMock.SMTP_FROM = "noreply@example.com";
|
||||
sendMail.mockRejectedValueOnce(new Error("boom"));
|
||||
|
||||
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
await expect(sendEmail({ to: "a@b.com", subject: "hi", text: "body" })).resolves.toBeUndefined();
|
||||
expect(errorSpy).toHaveBeenCalled();
|
||||
errorSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,169 @@
|
||||
// biome-ignore-all lint/style/noNonNullAssertion: These tests assert imported section lengths before inspecting the first item.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { JSONResumeImporter } from "./json-resume";
|
||||
|
||||
const importer = new JSONResumeImporter();
|
||||
|
||||
describe("JSONResumeImporter.parse", () => {
|
||||
it("throws when input is not valid JSON", () => {
|
||||
expect(() => importer.parse("not json")).toThrow();
|
||||
});
|
||||
|
||||
it("throws a serialized validation error for an obviously invalid shape", () => {
|
||||
// Email field is validated; a non-email string should fail the loose schema.
|
||||
const invalid = JSON.stringify({ basics: { email: "not-an-email" } });
|
||||
expect(() => importer.parse(invalid)).toThrow();
|
||||
});
|
||||
|
||||
it("imports an empty JSON Resume into a baseline ResumeData", () => {
|
||||
const result = importer.parse("{}");
|
||||
// Defaults preserve a name field even when unset by input.
|
||||
expect(typeof result.basics.name).toBe("string");
|
||||
});
|
||||
|
||||
it("imports basics fields into ResumeData", () => {
|
||||
const json = JSON.stringify({
|
||||
basics: {
|
||||
name: "Jane Doe",
|
||||
label: "Engineer",
|
||||
email: "jane@example.com",
|
||||
phone: "+1 555 123 4567",
|
||||
url: "https://janedoe.dev",
|
||||
summary: "Software engineer with 10+ years building products.",
|
||||
location: { city: "Berlin", region: "BE", countryCode: "DE" },
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
expect(result.basics.name).toBe("Jane Doe");
|
||||
expect(result.basics.headline).toBe("Engineer");
|
||||
expect(result.basics.email).toBe("jane@example.com");
|
||||
expect(result.basics.phone).toBe("+1 555 123 4567");
|
||||
expect(result.basics.location).toBe("Berlin, BE, DE");
|
||||
expect(result.summary.content).toContain("Software engineer");
|
||||
expect(result.summary.hidden).toBe(false);
|
||||
});
|
||||
|
||||
it("maps work entries into the experience section", () => {
|
||||
const json = JSON.stringify({
|
||||
work: [
|
||||
{
|
||||
name: "Acme Corp",
|
||||
position: "Senior Engineer",
|
||||
location: "Berlin",
|
||||
startDate: "2020-01-15",
|
||||
endDate: "2024",
|
||||
url: "https://acme.example",
|
||||
summary: "Built cool stuff.",
|
||||
highlights: ["Shipped X", "Improved Y by 30%"],
|
||||
},
|
||||
// Entry with neither name nor position is filtered out.
|
||||
{ startDate: "2010", endDate: "2015" },
|
||||
],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
expect(result.sections.experience.items).toHaveLength(1);
|
||||
|
||||
const item = result.sections.experience.items[0]!;
|
||||
expect(item.company).toBe("Acme Corp");
|
||||
expect(item.position).toBe("Senior Engineer");
|
||||
expect(item.location).toBe("Berlin");
|
||||
expect(item.period.length).toBeGreaterThan(0);
|
||||
expect(item.description).toContain("Shipped X");
|
||||
});
|
||||
|
||||
it("maps education entries (filters when institution is missing)", () => {
|
||||
const json = JSON.stringify({
|
||||
education: [
|
||||
{
|
||||
institution: "MIT",
|
||||
studyType: "BS",
|
||||
area: "Computer Science",
|
||||
startDate: "2010",
|
||||
endDate: "2014",
|
||||
score: "3.9",
|
||||
courses: ["Algorithms", "Distributed Systems"],
|
||||
},
|
||||
{ studyType: "BS" }, // no institution → filtered
|
||||
],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
expect(result.sections.education.items).toHaveLength(1);
|
||||
|
||||
const edu = result.sections.education.items[0]!;
|
||||
expect(edu.school).toBe("MIT");
|
||||
expect(edu.degree).toBe("BS in Computer Science");
|
||||
expect(edu.description).toContain("Algorithms");
|
||||
});
|
||||
|
||||
it("maps skills with level parsing", () => {
|
||||
const json = JSON.stringify({
|
||||
skills: [{ name: "TypeScript", level: "Master", keywords: ["node", "react"] }],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
const skill = result.sections.skills.items[0]!;
|
||||
|
||||
expect(skill.name).toBe("TypeScript");
|
||||
expect(skill.keywords).toEqual(["node", "react"]);
|
||||
expect(skill.level).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("maps profiles from basics.profiles into the profiles section", () => {
|
||||
const json = JSON.stringify({
|
||||
basics: {
|
||||
profiles: [
|
||||
{ network: "GitHub", username: "janedoe", url: "https://github.com/janedoe" },
|
||||
{ username: "no-network" }, // missing network → filtered
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
expect(result.sections.profiles.items).toHaveLength(1);
|
||||
|
||||
const profile = result.sections.profiles.items[0]!;
|
||||
expect(profile.network).toBe("GitHub");
|
||||
expect(profile.username).toBe("janedoe");
|
||||
});
|
||||
|
||||
it("imports a picture URL from basics.image", () => {
|
||||
const json = JSON.stringify({
|
||||
basics: { image: "https://example.com/pic.jpg" },
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
expect(result.picture.url).toBe("https://example.com/pic.jpg");
|
||||
expect(result.picture.hidden).toBe(false);
|
||||
});
|
||||
|
||||
it("leaves the summary content empty when basics.summary is absent", () => {
|
||||
const result = importer.parse(JSON.stringify({ basics: { name: "Jane" } }));
|
||||
expect(result.summary.content).toBe("");
|
||||
});
|
||||
|
||||
it("imports projects with description and period", () => {
|
||||
const json = JSON.stringify({
|
||||
projects: [
|
||||
{
|
||||
name: "Open source CLI",
|
||||
description: "Built a CLI tool",
|
||||
highlights: ["10k stars", "Used in production"],
|
||||
startDate: "2022",
|
||||
endDate: "2023",
|
||||
url: "https://github.com/x/y",
|
||||
},
|
||||
{ description: "no name" }, // filtered
|
||||
],
|
||||
});
|
||||
|
||||
const result = importer.parse(json);
|
||||
expect(result.sections.projects.items).toHaveLength(1);
|
||||
|
||||
const project = result.sections.projects.items[0]!;
|
||||
expect(project.name).toBe("Open source CLI");
|
||||
expect(project.description).toContain("10k stars");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { ReactiveResumeJSONImporter } from "./reactive-resume-json";
|
||||
|
||||
const importer = new ReactiveResumeJSONImporter();
|
||||
|
||||
describe("ReactiveResumeJSONImporter", () => {
|
||||
it("round-trips the default resume data", () => {
|
||||
const result = importer.parse(JSON.stringify(defaultResumeData));
|
||||
expect(result.basics.name).toBe(defaultResumeData.basics.name);
|
||||
});
|
||||
|
||||
it("throws a JSON-serialised validation error for an invalid object", () => {
|
||||
// Missing required top-level fields.
|
||||
expect(() => importer.parse(JSON.stringify({ foo: "bar" }))).toThrow();
|
||||
});
|
||||
|
||||
it("throws when the input is not valid JSON", () => {
|
||||
expect(() => importer.parse("not-json")).toThrow();
|
||||
});
|
||||
|
||||
it("creates a default layout page when the imported data has no pages", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.layout.pages = [];
|
||||
|
||||
const result = importer.parse(JSON.stringify(data));
|
||||
expect(result.metadata.layout.pages.length).toBe(1);
|
||||
expect(result.metadata.layout.pages[0]?.main.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("recovers missing built-in sections by appending them to the first page", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.layout.pages = [
|
||||
{
|
||||
fullWidth: false,
|
||||
main: ["experience"],
|
||||
sidebar: ["skills"],
|
||||
},
|
||||
];
|
||||
|
||||
const result = importer.parse(JSON.stringify(data));
|
||||
const firstPage = result.metadata.layout.pages[0];
|
||||
expect(firstPage).toBeDefined();
|
||||
const allIds = new Set([...(firstPage?.main ?? []), ...(firstPage?.sidebar ?? [])]);
|
||||
|
||||
// Every built-in section (except cover-letter) ends up somewhere on page 1.
|
||||
for (const expected of ["education", "projects", "languages", "awards"]) {
|
||||
expect(allIds.has(expected), expected).toBe(true);
|
||||
}
|
||||
// The originally-placed sections remain where the user put them.
|
||||
expect(firstPage?.sidebar).toContain("skills");
|
||||
expect(firstPage?.main[0]).toBe("experience");
|
||||
});
|
||||
|
||||
it("does not modify the layout when every built-in section is already placed", () => {
|
||||
const result = importer.parse(JSON.stringify(defaultResumeData));
|
||||
expect(result.metadata.layout.pages.length).toBe(defaultResumeData.metadata.layout.pages.length);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,304 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ReactiveResumeV4JSONImporter } from "./reactive-resume-v4-json";
|
||||
|
||||
const baseV4 = () => ({
|
||||
basics: {
|
||||
name: "Jane Doe",
|
||||
headline: "Senior Engineer",
|
||||
email: "jane@example.com",
|
||||
phone: "+1 555 0100",
|
||||
location: "Berlin",
|
||||
url: { label: "Site", href: "https://example.com" },
|
||||
customFields: [{ id: "cf1", icon: "globe", text: "https://example.com" }],
|
||||
picture: {
|
||||
url: "https://example.com/pic.jpg",
|
||||
size: 80,
|
||||
aspectRatio: 1,
|
||||
borderRadius: 0,
|
||||
effects: { hidden: false, border: true, grayscale: false },
|
||||
},
|
||||
},
|
||||
sections: {
|
||||
summary: {
|
||||
name: "Summary",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "summary",
|
||||
content: "<p>About me</p>",
|
||||
},
|
||||
awards: {
|
||||
name: "Awards",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "awards",
|
||||
items: [
|
||||
{ id: "a1", title: "Hackathon Winner", awarder: "Acme", date: "2023", summary: "" },
|
||||
{ title: "", awarder: "ghost" }, // filtered: no title
|
||||
],
|
||||
},
|
||||
certifications: {
|
||||
name: "Certifications",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "certifications",
|
||||
items: [
|
||||
{ id: "c1", name: "AWS Cert", issuer: "AWS", date: "2024" },
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
education: {
|
||||
name: "Education",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "education",
|
||||
items: [
|
||||
{
|
||||
id: "edu1",
|
||||
institution: "MIT",
|
||||
studyType: "BS",
|
||||
area: "CS",
|
||||
score: "3.9",
|
||||
date: "2010-2014",
|
||||
summary: "Thesis...",
|
||||
url: { label: "", href: "https://mit.edu" },
|
||||
},
|
||||
{ studyType: "MS" }, // filtered: no institution
|
||||
],
|
||||
},
|
||||
experience: {
|
||||
name: "Experience",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "experience",
|
||||
items: [
|
||||
{
|
||||
id: "exp1",
|
||||
company: "Acme Corp",
|
||||
position: "Engineer",
|
||||
location: "Berlin",
|
||||
date: "2020-2024",
|
||||
summary: "Built stuff.",
|
||||
url: { label: "", href: "https://acme.example" },
|
||||
},
|
||||
{ position: "ghost" }, // filtered: no company
|
||||
],
|
||||
},
|
||||
volunteer: {
|
||||
name: "Volunteer",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "volunteer",
|
||||
items: [
|
||||
{ id: "v1", organization: "Org", location: "Berlin", date: "2022", summary: "" },
|
||||
{ organization: "" }, // filtered
|
||||
],
|
||||
},
|
||||
interests: {
|
||||
name: "Interests",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "interests",
|
||||
items: [
|
||||
{ id: "i1", name: "Cooking", keywords: ["pasta", "bread"] },
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
languages: {
|
||||
name: "Languages",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "languages",
|
||||
items: [
|
||||
{ id: "l1", name: "English", description: "Native", level: 10 },
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
profiles: {
|
||||
name: "Profiles",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "profiles",
|
||||
items: [
|
||||
{
|
||||
id: "p1",
|
||||
network: "GitHub",
|
||||
username: "jane",
|
||||
icon: "github",
|
||||
url: { label: "", href: "https://github.com/jane" },
|
||||
},
|
||||
{ network: "" }, // filtered
|
||||
],
|
||||
},
|
||||
projects: {
|
||||
name: "Projects",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "projects",
|
||||
items: [
|
||||
{
|
||||
id: "pr1",
|
||||
name: "Open CLI",
|
||||
date: "2022",
|
||||
summary: "Built a CLI",
|
||||
url: { label: "", href: "https://example.com" },
|
||||
},
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
publications: {
|
||||
name: "Publications",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "publications",
|
||||
items: [
|
||||
{ id: "pub1", name: "Paper", publisher: "ACM", date: "2024" },
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
references: {
|
||||
name: "References",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "references",
|
||||
items: [
|
||||
{ id: "r1", name: "Bob Smith", description: "Manager", summary: "Was great", url: { label: "", href: "" } },
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
skills: {
|
||||
name: "Skills",
|
||||
columns: 1,
|
||||
separateLinks: false,
|
||||
visible: true,
|
||||
id: "skills",
|
||||
items: [
|
||||
{ id: "s1", name: "TypeScript", level: 10, keywords: ["node"], description: "Expert" },
|
||||
{ name: "" }, // filtered
|
||||
],
|
||||
},
|
||||
},
|
||||
metadata: {
|
||||
template: "onyx",
|
||||
layout: [[["experience", "education"], ["skills"]]],
|
||||
css: { value: "", visible: false },
|
||||
page: { margin: 14, format: "a4" as const, options: { breakLine: false, pageNumbers: false } },
|
||||
theme: { background: "#ffffff", text: "#000000", primary: "#dc2626" },
|
||||
typography: {
|
||||
font: { family: "IBM Plex Serif", subset: "latin", variants: ["regular"], size: 14.67 },
|
||||
lineHeight: 1.5,
|
||||
hideIcons: false,
|
||||
underlineLinks: false,
|
||||
},
|
||||
notes: "",
|
||||
},
|
||||
});
|
||||
|
||||
const importer = new ReactiveResumeV4JSONImporter();
|
||||
|
||||
describe("ReactiveResumeV4JSONImporter — broad section mapping", () => {
|
||||
const v4 = baseV4();
|
||||
const result = importer.parse(JSON.stringify(v4));
|
||||
|
||||
it("maps basics fields (name, headline, contact, website, customFields)", () => {
|
||||
expect(result.basics.name).toBe("Jane Doe");
|
||||
expect(result.basics.headline).toBe("Senior Engineer");
|
||||
expect(result.basics.email).toBe("jane@example.com");
|
||||
expect(result.basics.location).toBe("Berlin");
|
||||
expect(result.basics.website.url).toBe("https://example.com");
|
||||
expect(result.basics.customFields).toHaveLength(1);
|
||||
expect(result.basics.customFields[0]).toMatchObject({ icon: "globe", text: "https://example.com" });
|
||||
});
|
||||
|
||||
it("maps picture with border on", () => {
|
||||
expect(result.picture.url).toBe("https://example.com/pic.jpg");
|
||||
expect(result.picture.hidden).toBe(false);
|
||||
expect(result.picture.borderWidth).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("maps summary content", () => {
|
||||
expect(result.summary.content).toBe("<p>About me</p>");
|
||||
expect(result.summary.hidden).toBe(false);
|
||||
});
|
||||
|
||||
it("filters items by required field across every section", () => {
|
||||
expect(result.sections.awards.items).toHaveLength(1);
|
||||
expect(result.sections.certifications.items).toHaveLength(1);
|
||||
expect(result.sections.education.items).toHaveLength(1);
|
||||
expect(result.sections.experience.items).toHaveLength(1);
|
||||
expect(result.sections.volunteer.items).toHaveLength(1);
|
||||
expect(result.sections.interests.items).toHaveLength(1);
|
||||
expect(result.sections.languages.items).toHaveLength(1);
|
||||
expect(result.sections.profiles.items).toHaveLength(1);
|
||||
expect(result.sections.projects.items).toHaveLength(1);
|
||||
expect(result.sections.publications.items).toHaveLength(1);
|
||||
expect(result.sections.references.items).toHaveLength(1);
|
||||
expect(result.sections.skills.items).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("maps experience items (company, position, period, description)", () => {
|
||||
const exp = result.sections.experience.items[0] as {
|
||||
company?: string;
|
||||
position?: string;
|
||||
period?: string;
|
||||
description?: string;
|
||||
};
|
||||
expect(exp.company).toBe("Acme Corp");
|
||||
expect(exp.position).toBe("Engineer");
|
||||
expect(exp.period).toBe("2020-2024");
|
||||
expect(exp.description).toBe("Built stuff.");
|
||||
});
|
||||
|
||||
it("maps education items including degree+area", () => {
|
||||
const edu = result.sections.education.items[0] as {
|
||||
school?: string;
|
||||
degree?: string;
|
||||
area?: string;
|
||||
grade?: string;
|
||||
};
|
||||
expect(edu.school).toBe("MIT");
|
||||
expect(edu.degree).toBe("BS");
|
||||
expect(edu.area).toBe("CS");
|
||||
expect(edu.grade).toBe("3.9");
|
||||
});
|
||||
|
||||
it("maps awards with title + awarder + date", () => {
|
||||
const award = result.sections.awards.items[0] as { title?: string; awarder?: string; date?: string };
|
||||
expect(award.title).toBe("Hackathon Winner");
|
||||
expect(award.awarder).toBe("Acme");
|
||||
expect(award.date).toBe("2023");
|
||||
});
|
||||
|
||||
it("maps certifications (renaming name → title) and references (description → position)", () => {
|
||||
const cert = result.sections.certifications.items[0] as { title?: string };
|
||||
expect(cert.title).toBe("AWS Cert");
|
||||
|
||||
const ref = result.sections.references.items[0] as { name?: string; position?: string; description?: string };
|
||||
expect(ref.name).toBe("Bob Smith");
|
||||
expect(ref.position).toBe("Manager");
|
||||
expect(ref.description).toBe("Was great");
|
||||
});
|
||||
|
||||
it("scales language level 10 → 5 and skill level 10 → 5", () => {
|
||||
const language = result.sections.languages.items[0] as { level?: number };
|
||||
expect(language.level).toBe(5);
|
||||
|
||||
const skill = result.sections.skills.items[0] as { level?: number };
|
||||
expect(skill.level).toBe(5);
|
||||
});
|
||||
|
||||
it("invalid JSON throws", () => {
|
||||
expect(() => importer.parse("not json")).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Document } from "docx";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { sampleResumeData } from "@reactive-resume/schema/resume/sample";
|
||||
import { buildDocument } from "./builder";
|
||||
|
||||
describe("buildDocument", () => {
|
||||
it("returns a docx Document instance for the default resume", () => {
|
||||
const doc = buildDocument(defaultResumeData);
|
||||
expect(doc).toBeInstanceOf(Document);
|
||||
});
|
||||
|
||||
it("returns a docx Document instance for the sample resume", () => {
|
||||
const doc = buildDocument(sampleResumeData);
|
||||
expect(doc).toBeInstanceOf(Document);
|
||||
});
|
||||
|
||||
it("handles a resume with no layout pages without throwing", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.layout.pages = [];
|
||||
|
||||
expect(() => buildDocument(data)).not.toThrow();
|
||||
});
|
||||
|
||||
it("supports both a4 and letter page formats", () => {
|
||||
const a4Data = structuredClone(defaultResumeData);
|
||||
a4Data.metadata.page.format = "a4";
|
||||
expect(buildDocument(a4Data)).toBeInstanceOf(Document);
|
||||
|
||||
const letterData = structuredClone(defaultResumeData);
|
||||
letterData.metadata.page.format = "letter";
|
||||
expect(buildDocument(letterData)).toBeInstanceOf(Document);
|
||||
});
|
||||
|
||||
it("handles a full-width single page layout", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.layout.pages = [
|
||||
{
|
||||
fullWidth: true,
|
||||
main: ["experience", "education"],
|
||||
sidebar: [],
|
||||
},
|
||||
];
|
||||
expect(() => buildDocument(data)).not.toThrow();
|
||||
});
|
||||
|
||||
it("handles a sidebar layout (main + sidebar split)", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.layout.pages = [
|
||||
{
|
||||
fullWidth: false,
|
||||
main: ["experience", "education"],
|
||||
sidebar: ["skills", "languages"],
|
||||
},
|
||||
];
|
||||
expect(() => buildDocument(data)).not.toThrow();
|
||||
});
|
||||
|
||||
it("falls back to default colors when the primary color is unparseable", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.design.colors.primary = "not-a-color";
|
||||
expect(() => buildDocument(data)).not.toThrow();
|
||||
});
|
||||
|
||||
it("falls back to a default font when fontFamily is empty", () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.metadata.typography.body.fontFamily = "";
|
||||
data.metadata.typography.heading.fontFamily = "";
|
||||
expect(() => buildDocument(data)).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { htmlToParagraphs } from "./html-to-docx";
|
||||
|
||||
describe("htmlToParagraphs", () => {
|
||||
it("returns [] for empty / whitespace-only input", () => {
|
||||
expect(htmlToParagraphs("")).toEqual([]);
|
||||
expect(htmlToParagraphs(" \n ")).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns at least one paragraph for a simple <p> element", () => {
|
||||
const result = htmlToParagraphs("<p>Hello world</p>");
|
||||
expect(result.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it("converts plain text nodes at the body root into a paragraph", () => {
|
||||
const result = htmlToParagraphs("Just some plain text");
|
||||
expect(result.length).toBe(1);
|
||||
});
|
||||
|
||||
it("emits separate paragraphs for multiple top-level blocks", () => {
|
||||
const result = htmlToParagraphs("<p>One</p><p>Two</p><p>Three</p>");
|
||||
expect(result.length).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
|
||||
it("treats <h1>..<h6> as block-level paragraphs (one per heading)", () => {
|
||||
const result = htmlToParagraphs("<h1>A</h1><h2>B</h2><h3>C</h3>");
|
||||
expect(result.length).toBe(3);
|
||||
});
|
||||
|
||||
it("renders nested <strong>, <em>, <u>, <s> inline styling without throwing", () => {
|
||||
const result = htmlToParagraphs("<p><strong>Bold</strong> <em>italic</em> <u>under</u> <s>strike</s></p>");
|
||||
expect(result.length).toBe(1);
|
||||
});
|
||||
|
||||
it("renders links and lists without throwing", () => {
|
||||
const html = '<p><a href="https://example.com">link</a></p><ul><li>One</li><li>Two</li></ul>';
|
||||
const result = htmlToParagraphs(html);
|
||||
expect(result.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it("accepts custom font + size config without throwing", () => {
|
||||
const result = htmlToParagraphs("<p>Hello</p>", {
|
||||
font: "Roboto",
|
||||
size: 22,
|
||||
color: "111111",
|
||||
linkColor: "0563C1",
|
||||
});
|
||||
expect(result.length).toBe(1);
|
||||
});
|
||||
|
||||
it("ignores HTML comments and script/style tags at the root", () => {
|
||||
// Comments at root and script tags don't add paragraph output.
|
||||
const result = htmlToParagraphs("<!--c--><script>x</script><p>Hello</p>");
|
||||
expect(result.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { defaultResumeData } from "@reactive-resume/schema/resume/default";
|
||||
import { buildDocx } from "./index";
|
||||
|
||||
describe("buildDocx", () => {
|
||||
it("returns a Blob for the default resume", async () => {
|
||||
const blob = await buildDocx(defaultResumeData);
|
||||
expect(blob).toBeInstanceOf(Blob);
|
||||
expect(blob.size).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("produces a non-empty document for a populated resume", async () => {
|
||||
const data = structuredClone(defaultResumeData);
|
||||
data.basics.name = "Jane Doe";
|
||||
data.basics.headline = "Engineer";
|
||||
data.basics.email = "jane@example.com";
|
||||
|
||||
const blob = await buildDocx(data);
|
||||
expect(blob.size).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import type { CustomSection, ResumeData, SectionType } from "@reactive-resume/schema/resume/data";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { renderBuiltInSection, renderCustomSection, renderSummary, setRenderConfig } from "./section-renderers";
|
||||
|
||||
const baseConfig = {
|
||||
headingFont: "Inter",
|
||||
headingSizeHalfPt: 28,
|
||||
bodyFont: "Inter",
|
||||
bodySizeHalfPt: 20,
|
||||
textColorHex: "111111",
|
||||
primaryColorHex: "0563C1",
|
||||
};
|
||||
|
||||
setRenderConfig(baseConfig);
|
||||
|
||||
const HEX = "#0563C1";
|
||||
|
||||
describe("renderSummary", () => {
|
||||
it("returns [] when the section is hidden", () => {
|
||||
const summary: ResumeData["summary"] = {
|
||||
title: "Summary",
|
||||
content: "<p>Hello</p>",
|
||||
hidden: true,
|
||||
columns: 1,
|
||||
};
|
||||
expect(renderSummary(summary, HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns [] when content is empty", () => {
|
||||
const summary: ResumeData["summary"] = {
|
||||
title: "Summary",
|
||||
content: "",
|
||||
hidden: false,
|
||||
columns: 1,
|
||||
};
|
||||
expect(renderSummary(summary, HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("includes a heading paragraph when both title and content are present", () => {
|
||||
const summary: ResumeData["summary"] = {
|
||||
title: "Summary",
|
||||
content: "<p>Hello world</p>",
|
||||
hidden: false,
|
||||
columns: 1,
|
||||
};
|
||||
const paragraphs = renderSummary(summary, HEX);
|
||||
// One heading + the htmlToParagraphs output for one <p>.
|
||||
expect(paragraphs.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it("omits the heading when title is empty but still renders content", () => {
|
||||
const summary: ResumeData["summary"] = {
|
||||
title: "",
|
||||
content: "<p>Hello world</p>",
|
||||
hidden: false,
|
||||
columns: 1,
|
||||
};
|
||||
const paragraphs = renderSummary(summary, HEX);
|
||||
expect(paragraphs.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
const emptySection = <T extends SectionType>(_type: T): ResumeData["sections"][T] =>
|
||||
({
|
||||
title: "Section",
|
||||
columns: 1,
|
||||
hidden: false,
|
||||
items: [],
|
||||
}) as ResumeData["sections"][T];
|
||||
|
||||
describe("renderBuiltInSection", () => {
|
||||
it("returns [] when the section has no items", () => {
|
||||
expect(renderBuiltInSection("experience", emptySection("experience"), HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns [] when section.hidden is true", () => {
|
||||
const section = { ...emptySection("experience"), hidden: true };
|
||||
expect(renderBuiltInSection("experience", section, HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns [] for an unknown section type", () => {
|
||||
expect(renderBuiltInSection("not-a-section" as never, emptySection("experience"), HEX)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("renderCustomSection", () => {
|
||||
const baseCustom: CustomSection = {
|
||||
id: "custom-1",
|
||||
type: "summary",
|
||||
title: "Notes",
|
||||
columns: 1,
|
||||
hidden: false,
|
||||
items: [],
|
||||
};
|
||||
|
||||
it("returns [] when the custom section is hidden", () => {
|
||||
expect(renderCustomSection({ ...baseCustom, hidden: true }, HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns [] when all items are hidden or empty", () => {
|
||||
const section: CustomSection = {
|
||||
...baseCustom,
|
||||
items: [{ id: "x", hidden: true } as never],
|
||||
};
|
||||
expect(renderCustomSection(section, HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns [] for an unknown custom section type", () => {
|
||||
const section: CustomSection = {
|
||||
...baseCustom,
|
||||
type: "no-such-type" as never,
|
||||
items: [{ id: "x", hidden: false, content: "<p>x</p>" } as never],
|
||||
};
|
||||
expect(renderCustomSection(section, HEX)).toEqual([]);
|
||||
});
|
||||
|
||||
it("renders content for a summary-type custom section", () => {
|
||||
const section: CustomSection = {
|
||||
...baseCustom,
|
||||
type: "summary",
|
||||
items: [{ id: "x", hidden: false, content: "<p>Hello</p>" } as never],
|
||||
};
|
||||
const paragraphs = renderCustomSection(section, HEX);
|
||||
// 1 heading + at least one paragraph for the HTML
|
||||
expect(paragraphs.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it("renders recipient + content for a cover-letter custom section", () => {
|
||||
const section: CustomSection = {
|
||||
...baseCustom,
|
||||
type: "cover-letter",
|
||||
items: [
|
||||
{
|
||||
id: "x",
|
||||
hidden: false,
|
||||
recipient: "<p>Dear Jane,</p>",
|
||||
content: "<p>Body</p>",
|
||||
} as never,
|
||||
],
|
||||
};
|
||||
const paragraphs = renderCustomSection(section, HEX);
|
||||
// 1 heading + at least two paragraphs (recipient + body)
|
||||
expect(paragraphs.length).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setRenderConfig", () => {
|
||||
it("can be called repeatedly with a different config (no throw)", () => {
|
||||
expect(() => setRenderConfig({ ...baseConfig, headingFont: "Roboto", primaryColorHex: "ff0000" })).not.toThrow();
|
||||
|
||||
// Restore for any later tests in the file
|
||||
setRenderConfig(baseConfig);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user