| name | add-test |
| description | Write tests for the secret-vault Worker and hfs CLI. Use when adding test coverage, fixing bugs with regression tests, or validating behavior. |
Add tests
28 tests exist across Worker (23) and CLI (5). Tests run in CI (ci.yml) and release (release.yml) workflows. This covers patterns and guidance for adding more.
CONVENTIONS
- ALWAYS test crypto round-trip (encrypt → decrypt = original)
- ALWAYS test auth rejection (missing JWT, unregistered token, wrong scope)
- ALWAYS test error cases (malformed JSON → 400, not 500)
- NEVER use real encryption keys or credentials in tests
- NEVER skip scope enforcement tests when adding new endpoints
WORKER TESTS (vitest + miniflare)
Setup
cd secret-vault
npm install -D vitest @cloudflare/vitest-pool-workers
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.jsonc" },
miniflare: {
d1Databases: ["DB"],
bindings: {
ENCRYPTION_KEY: "a".repeat(64),
ALLOWED_EMAILS: "test@example.com",
TEAM_DOMAIN: "https://test.cloudflareaccess.com",
POLICY_AUD: "test-aud",
},
},
},
},
},
});
Critical paths (test first)
- Encrypt → decrypt round-trip
- Hex key validation (non-hex ENCRYPTION_KEY → error)
- Auth rejects missing JWT (401)
- Auth rejects unregistered service token (401)
- Failed auth audit logging (method: "rejected", action: "auth_failed")
- Scope enforcement: read-only token can't write/delete (403)
- CRUD: create, read, update, delete a secret
- Body size limits (value >1MB → 400, key >256 chars → 400)
- Audit log records every operation
- Reserved keys
"export" and "import" rejected on PUT (400)
- Reserved key names rejected in bulk import
CLI TESTS (vitest)
cd hfs && npm install -D vitest
Critical paths
resolveAuth() returns correct mode for each env var combination
resolveAuth() throws on partial service token env vars
resolveAuth() throws on expired JWT
storeJwt() rejects malformed JWT
- VaultClient builds correct headers for each auth mode
REFERENCES
- Test patterns - code examples for Worker, crypto, CLI, and scope tests