unsloth/studio/frontend/vitest.config.ts
Daniel Han a2ab0dda30 studio/frontend: add Vitest suite for auth + 422 helper
Pins the behavioural invariants that landed in PR #5490 (the frontend
half of the #5375-regression fix-up backlog) so a future hardening
pass cannot quietly undo them. Today these surfaces have only typecheck
+ bundle-build coverage; this lifts the floor.

What's pinned (35 tests across 3 files, ~360 ms):

- src/lib/__tests__/format-fastapi-error.test.ts (17)
  formatFastApiDetail + readFastApiError: string passthrough, array
  detail joined as "field: msg" / "; ", nested loc joined by ".",
  missing msg / missing loc / empty array / mixed valid+junk, plus
  Response shapes for 422 / 400 / 500 / 503 / non-JSON / object-detail
  + message fallback. Regression target: rendering `[object Object]`
  or `Request failed (422)` instead of "field: msg".

- src/features/auth/__tests__/session.test.ts (10)
  storeAuthTokens writes only the two JWT keys (never the must-change
  key -- the CodeQL data-flow pin); clearAuthTokens removes all three;
  setMustChangePassword writes the literal "1" / removes the key
  (presence-encoded so the stored value stays a constant, not a
  derived boolean); isOnboardingDone treats only the literal "true"
  as done.

- src/features/auth/__tests__/api.test.ts (8)
  refreshSession singleflight (5 racing callers -> 1 /api/auth/refresh
  request); in-flight slot clears so subsequent calls refetch;
  non-OK response clears tokens; no refresh token -> no fetch;
  logoutGeneration drops the new token pair when logout() lands
  mid-refresh; logout() clears local state on 204; logout() retries
  with a rotated access token on 401; logout() still clears state on
  network failure.

Wiring:
- vitest 4 + happy-dom 18 as devDependencies.
- vitest.config.ts mirrors the @/ alias from tsconfig.app.json.
- New `test` / `test:watch` scripts.

Workflow step (CI gating) is intentionally NOT included in this PR
because the publishing token lacks `workflow` scope. After merge, add
this between the Typecheck and Build steps in
.github/workflows/studio-frontend-ci.yml:

  - name: Unit tests (Vitest)
    run: npm run test

`npm run typecheck` / `test` / `build` all clean locally.
2026-05-18 08:07:45 +00:00

18 lines
507 B
TypeScript

// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import path from "node:path";
import { defineConfig } from "vitest/config";
export default defineConfig({
resolve: {
alias: { "@": path.resolve(__dirname, "./src") },
},
test: {
environment: "happy-dom",
globals: false,
include: ["src/**/__tests__/**/*.test.ts", "src/**/*.test.ts"],
restoreMocks: true,
clearMocks: true,
},
});