Skip to content

[Testing 06] test: unit tests for access control, storage, user API keys and chat doc resolution - #229

Merged
2 commits merged into
Open-Legal-Products:mainfrom
amal66:olp-pr/backend-unit-tests
Jul 21, 2026
Merged

[Testing 06] test: unit tests for access control, storage, user API keys and chat doc resolution#229
2 commits merged into
Open-Legal-Products:mainfrom
amal66:olp-pr/backend-unit-tests

Conversation

@amal66

@amal66 amal66 commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

Unit tests for the backend's core libraries — access control, storage path/key handling, user API keys, and chat document-resolution — plus a v8 coverage ratchet. Ported from the amal66 fork (reference index #205) and adapted to this repo's backend/ layout and current behavior. Stacks on #228 (vitest harness).

Changes

  • backend/src/lib/__tests__/access.test.ts — 7 tests for listAccessibleProjectIds / filterAccessibleDocumentIds (owner vs shared-with access).
  • backend/src/lib/__tests__/storage.test.ts — 25 tests for the storage helpers.
  • backend/src/lib/__tests__/userApiKeys.test.ts — 10 tests (env clearing added so shell-exported keys can't leak into assertions).
  • backend/src/lib/__tests__/chatTypes.test.ts — 9 tests for resolveDoc / resolveDocLabel in lib/chat/types.ts.
  • backend/vitest.config.mts — node environment, 20s timeouts, v8 coverage over src/lib/** with threshold floors set just below measured coverage (statements 2 / branches 2 / functions 4 / lines 2 — a no-regression floor, not a target; raise as coverage grows).
  • @vitest/coverage-v8 dev-dependency + test:coverage script.

New tests: 51 (63 total with the harness seed suite).

Why

These libs gate who can read which project and document — the highest-value unit coverage in the codebase — and today nothing exercises them. The coverage ratchet means future PRs can only move lib coverage up.

What was deliberately not ported (and why)

  • Fork's org-RBAC access cases — access.ts here has no org branch.
  • upload.test.ts — tested a magic-bytes helper that doesn't exist here; not testable without production changes, which this PR doesn't make.
  • userSettings.test.ts — tested a fork-only tabular-model fallback. Worth noting: the fork's version fixes a real issue (the default tabular model fails for users who only have Claude/OpenAI keys); this repo still has the pre-fix behavior. Happy to raise that as its own small PR.

Testing

  • npm test --prefix backend — 5 files, 63/63 passing.
  • npm run test:coverage --prefix backend — passes thresholds.
  • npm run build --prefix backend — passes.

No production code is modified. Includes #228's commit as its base until proper stacking is possible — review the top commit only.

🤖 Generated with Claude Code

QA Runner and others added 2 commits July 20, 2026 10:42
Ported from #24 onto current main; lockfiles regenerated against
this tree. Adds vitest as a dev dependency with a `test` script in both
packages, excludes test files from the backend tsc build, and seeds one
suite per package (backend: downloadTokens, 12 tests; frontend: cn() utils,
8 tests). Verified locally: backend 12/12, frontend 8/8 passing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Ported from the amal66 fork (see index Open-Legal-Products#205)
onto current main, adapted to this repo's backend/ layout
(apps/api/src/lib -> backend/src/lib), plus a v8 coverage ratchet.

Suites ported (51 new tests, verified locally):
- access.test.ts (7): owner/shared/private project access, doc access,
  review sharing, document-ID filtering. Dropped the fork's "org RBAC"
  describe block (7 cases) — org_id/org_members multi-tenancy and the
  role/canManage fields do not exist in this repo's access.ts.
- storage.test.ts (25): filename normalization/sanitization, RFC 5987
  encoding, Content-Disposition, storage key helpers. Dropped the fork's
  vi.mock of lib/env — this repo has no env module; storage reads
  process.env directly and the tested helpers are pure.
- userApiKeys.test.ts (10): normalizeApiKeyProvider + hasEnvApiKey.
  Added a beforeEach env clear so shell-exported API keys can't leak
  into assertions.
- chatTypes.test.ts (9): resolveDoc/resolveDocLabel, which live in
  lib/chat/types.ts here (the fork's lib/chatTools.ts equivalent).
  Dropped generateSpotlightNonce cases (2) — no such export here.

Suites dropped entirely (subject not present in this repo):
- upload.test.ts — tested hasMagicBytes; this repo's lib/upload.ts is
  only the multer middleware and exports no magic-byte checker.
- userSettings.test.ts — tested resolveTabularModel (fork-only keyed-
  provider fallback); this repo resolves tabular_model via
  resolveModel with a static default.

Coverage ratchet: vitest.config.mts adds v8 coverage over src/lib/**
with floors measured against this tree (2.58% stmts, 2.00% branches,
4.61% funcs, 2.58% lines -> floors 2/2/4/2). Full suite: 5 files,
63 tests passing (incl. the pre-existing 12 in downloadTokens.test.ts);
npm run test:coverage and npm run build both pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


QA Runner seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@amal66 amal66 changed the title test: unit tests for access control, storage, user API keys and chat doc resolution [Test 06] test: unit tests for access control, storage, user API keys and chat doc resolution Jul 21, 2026
@amal66 amal66 changed the title [Test 06] test: unit tests for access control, storage, user API keys and chat doc resolution [Testing 06] [Test 06] test: unit tests for access control, storage, user API keys and chat doc resolution Jul 21, 2026
@amal66 amal66 changed the title [Testing 06] [Test 06] test: unit tests for access control, storage, user API keys and chat doc resolution [Testing 06] test: unit tests for access control, storage, user API keys and chat doc resolution Jul 21, 2026
@willchen96 willchen96 closed this pull request by merging all changes into Open-Legal-Products:main in 948e9bd Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants