hazo_testing 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGE_LOG.md +72 -0
- package/README.md +78 -5
- package/SETUP_CHECKLIST.md +33 -3
- package/dist/axe_helper.d.ts +2 -0
- package/dist/axe_helper.js +16 -0
- package/dist/factories/users.d.ts +14 -0
- package/dist/factories/users.js +62 -2
- package/dist/hooks/after_each_setup.vitest.d.ts +1 -0
- package/dist/hooks/after_each_setup.vitest.js +8 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/render_with_intl.d.ts +14 -0
- package/dist/render_with_intl.js +26 -0
- package/dist/test/server_only_stub.d.ts +1 -0
- package/dist/test/server_only_stub.js +1 -0
- package/dist/vitest-setup.d.ts +1 -0
- package/dist/vitest-setup.js +6 -0
- package/package.json +84 -26
- package/vitest-preset.mjs +49 -0
package/CHANGE_LOG.md
CHANGED
|
@@ -1,5 +1,77 @@
|
|
|
1
1
|
# hazo_testing changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0 — 2026-06-12
|
|
4
|
+
|
|
5
|
+
### Added — Permissioned user factory (FR-001)
|
|
6
|
+
|
|
7
|
+
`createTestUser` now wires a full permission/scope chain when `role`,
|
|
8
|
+
`permissions`, or `scopeId` opts are supplied. Bare calls (no chain opts) are
|
|
9
|
+
byte-for-byte backward compatible — the return shape and behavior are unchanged.
|
|
10
|
+
|
|
11
|
+
**New options:**
|
|
12
|
+
|
|
13
|
+
| Option | Type | Default | Description |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `permissions` | `string[]` | — | Permission names to grant via the role. Auto-created if absent. |
|
|
16
|
+
| `role` | `string` | auto (`role_<id prefix>`) | Role name. Auto-created if absent; reused if it already exists. |
|
|
17
|
+
| `scopeId` | `string` | — | Scope id to assign the user to. Auto-created if absent. |
|
|
18
|
+
| `scopeName` | `string` | `scopeId` | Name for the auto-created scope row. |
|
|
19
|
+
| `scopeLevel` | `string` | `"firm"` | `level` column of the auto-created scope row. |
|
|
20
|
+
|
|
21
|
+
**New return fields** (only present when a chain was seeded):
|
|
22
|
+
`roleId`, `roleName`, `permissions`, `scopeId`.
|
|
23
|
+
|
|
24
|
+
**Tables seeded:** `hazo_roles` → `hazo_user_roles` → `hazo_permissions` →
|
|
25
|
+
`hazo_role_permissions` → `hazo_scopes` → `hazo_user_scopes`.
|
|
26
|
+
|
|
27
|
+
**Idempotent:** if the same `role`/`permission`/`scopeId` is passed across
|
|
28
|
+
multiple `createTestUser` calls in one test DB, rows are reused (`INSERT OR
|
|
29
|
+
IGNORE` on junction tables, `findOne`-or-create on entity tables). No
|
|
30
|
+
UNIQUE-constraint errors.
|
|
31
|
+
|
|
32
|
+
**Resolves:** `hazo_testing/design/feature_requests/FR-001-permissioned-user-factory.md`
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 0.2.0 — 2026-06-11
|
|
37
|
+
|
|
38
|
+
### Added — Vitest-compatible surfaces (additive; Jest preset unchanged)
|
|
39
|
+
|
|
40
|
+
The package now ships Vitest-flavoured presets and component-test helpers
|
|
41
|
+
alongside the existing Jest infrastructure. All v0.1.x exports are unchanged —
|
|
42
|
+
`jest-preset.cjs`, `src/hooks/after_each_setup.ts`, and every existing source
|
|
43
|
+
file were left untouched. hazo_admin's Jest consumption is unaffected.
|
|
44
|
+
|
|
45
|
+
- **`hazo_testing/vitest-preset`** — `vitest-preset.mjs`, a Vitest config
|
|
46
|
+
factory mirroring `jest-preset.cjs`. Returns a `UserConfig` with
|
|
47
|
+
`test.environment: "node"` (consumers opt into jsdom per-file), wires
|
|
48
|
+
`setupFiles: ["hazo_testing/vitest-setup"]`, `testTimeout: 10000`, a
|
|
49
|
+
`server-only` → empty-stub `resolve.alias`, and `server.deps.inline: [/hazo_/]`
|
|
50
|
+
so hazo_* packages transform through Vite. Accepts `{ alias, setupFiles }`
|
|
51
|
+
overrides.
|
|
52
|
+
- **`hazo_testing/vitest-setup`** — `src/vitest-setup.ts` setup entry that wires
|
|
53
|
+
the afterEach reset-hook runner. Backed by a new
|
|
54
|
+
`src/hooks/after_each_setup.vitest.ts` (sibling to the Jest
|
|
55
|
+
`after_each_setup.ts`) that imports `afterEach` explicitly from `vitest`
|
|
56
|
+
rather than relying on a Jest-injected global.
|
|
57
|
+
- **`hazo_testing/render`** — `renderWithIntl(ui, { locale?, messages?,
|
|
58
|
+
renderOptions? })` wraps a component in next-intl's `NextIntlClientProvider`
|
|
59
|
+
and renders it via `@testing-library/react`. Both peers are optional and
|
|
60
|
+
resolved lazily via `await import(...)` (same idiom as `src/nextjs/index.ts`).
|
|
61
|
+
`locale` defaults to `"en"`, `messages` to `{}`.
|
|
62
|
+
- **`hazo_testing/axe`** — `expectNoAxeViolations(container, options?)` runs
|
|
63
|
+
axe-core against a DOM container and throws a formatted error listing
|
|
64
|
+
violations. Defaults to the WCAG 2.0/2.1 A + AA tag set. `axe-core` is a real
|
|
65
|
+
dependency (always present when a11y testing is used).
|
|
66
|
+
- **`server-only` test stub** — `src/test/server_only_stub.ts`, shipped in dist
|
|
67
|
+
so consumers don't need a local copy; referenced by `vitest-preset.mjs`.
|
|
68
|
+
|
|
69
|
+
### Dependencies
|
|
70
|
+
|
|
71
|
+
- Added `axe-core` to `dependencies`.
|
|
72
|
+
- Added `@testing-library/react`, `next-intl`, and `vitest` as **optional** peer
|
|
73
|
+
dependencies (`peerDependenciesMeta`).
|
|
74
|
+
|
|
3
75
|
## 0.1.1 — 2026-05-31
|
|
4
76
|
|
|
5
77
|
### Changed — consume hazo_llm_api 2.1.0 public API (FR-001 + FR-002 resolved)
|
package/README.md
CHANGED
|
@@ -105,20 +105,35 @@ These are thin shims over `createCrudService` from `hazo_connect`. They handle U
|
|
|
105
105
|
```ts
|
|
106
106
|
import { createTestUser } from "hazo_testing";
|
|
107
107
|
|
|
108
|
+
// Bare user — just a hazo_users row + session cookie (backward-compatible):
|
|
108
109
|
const user = await createTestUser(adapter, {
|
|
109
110
|
email: "alice@example.com",
|
|
110
111
|
name: "Alice", // optional — defaults to email prefix
|
|
111
|
-
columnOverrides: {
|
|
112
|
+
columnOverrides: { url_on_logon: "/home" }, // any nullable hazo_users column
|
|
112
113
|
});
|
|
113
|
-
|
|
114
|
-
// user.
|
|
115
|
-
// user.email — "alice@example.com"
|
|
114
|
+
// user.id — generated UUID
|
|
115
|
+
// user.email — "alice@example.com"
|
|
116
116
|
// user.sessionCookie — ready "Cookie:" header string
|
|
117
|
+
|
|
118
|
+
// Permissioned user — seeds the full role/permission/scope chain in one call:
|
|
119
|
+
const admin = await createTestUser(adapter, {
|
|
120
|
+
email: "admin@example.com",
|
|
121
|
+
role: "admin", // role name; auto-created if absent
|
|
122
|
+
permissions: ["system.access", "file.upload"], // permission names; auto-created
|
|
123
|
+
scopeId: "org-1", // scope id; auto-created if absent
|
|
124
|
+
scopeName: "Acme Corp", // optional — defaults to scopeId
|
|
125
|
+
scopeLevel: "firm", // optional — defaults to "firm"
|
|
126
|
+
});
|
|
127
|
+
// admin.roleId — id of the hazo_roles row
|
|
128
|
+
// admin.roleName — "admin"
|
|
129
|
+
// admin.permissions — ["system.access", "file.upload"]
|
|
130
|
+
// admin.scopeId — "org-1"
|
|
117
131
|
```
|
|
118
132
|
|
|
119
133
|
- Writes a real `hazo_users` row. Note: the column is `email_address` in the schema; the factory maps the `email` option automatically.
|
|
120
134
|
- Mints a session cookie using HS256 JWT (same algorithm as `hazo_auth`) signed with `process.env.JWT_SECRET`.
|
|
121
|
-
-
|
|
135
|
+
- When `role`, `permissions`, or `scopeId` are given, seeds the full chain across `hazo_roles`, `hazo_user_roles`, `hazo_permissions`, `hazo_role_permissions`, `hazo_scopes`, and `hazo_user_scopes`. All rows are reused-or-created, so multiple calls sharing the same role/scopeId in one DB are idempotent.
|
|
136
|
+
- Bare calls (no chain opts) return only `{ id, email, sessionCookie }` — unchanged from v0.2.x.
|
|
122
137
|
|
|
123
138
|
### `createTestScope`
|
|
124
139
|
|
|
@@ -304,6 +319,64 @@ Without `--experimental-vm-modules`, ESM imports (`hazo_notify`, `hazo_jobs`, `h
|
|
|
304
319
|
|
|
305
320
|
---
|
|
306
321
|
|
|
322
|
+
## Vitest preset (0.2.0)
|
|
323
|
+
|
|
324
|
+
`hazo_testing/vitest-preset` is the Vitest counterpart to the Jest preset — an
|
|
325
|
+
ESM config factory. Additive: the Jest preset and all v0.1.x exports are
|
|
326
|
+
unchanged.
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
// vitest.config.ts
|
|
330
|
+
import { defineConfig } from "vitest/config";
|
|
331
|
+
import hazoTestingVitest from "hazo_testing/vitest-preset";
|
|
332
|
+
|
|
333
|
+
export default defineConfig(
|
|
334
|
+
hazoTestingVitest({
|
|
335
|
+
// alias: { ... }, // optional — extra resolve.alias entries
|
|
336
|
+
// setupFiles: [ ... ], // optional — additional setup files
|
|
337
|
+
}),
|
|
338
|
+
);
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
- `test.environment: "node"` — opt into jsdom per-file with a
|
|
342
|
+
`// @vitest-environment jsdom` docblock.
|
|
343
|
+
- `setupFiles: ["hazo_testing/vitest-setup"]` — runs registered reset hooks in
|
|
344
|
+
`afterEach` (Vitest imports `afterEach` explicitly; it is not a global).
|
|
345
|
+
- `resolve.alias` maps `server-only` → an empty stub so server modules import
|
|
346
|
+
cleanly under Vitest.
|
|
347
|
+
- `server.deps.inline: [/hazo_/]` — hazo_* packages transform through Vite.
|
|
348
|
+
- `testTimeout: 10 000` ms.
|
|
349
|
+
- `vitest-preset.mjs` is ESM (Vitest reads config through Vite's ESM loader).
|
|
350
|
+
|
|
351
|
+
Install the optional peer: `npm install --save-dev vitest`.
|
|
352
|
+
|
|
353
|
+
### `renderWithIntl` — `hazo_testing/render`
|
|
354
|
+
|
|
355
|
+
```tsx
|
|
356
|
+
import { renderWithIntl } from "hazo_testing/render";
|
|
357
|
+
|
|
358
|
+
const { getByText } = await renderWithIntl(<MyComponent />, {
|
|
359
|
+
locale: "en", // default "en"
|
|
360
|
+
messages: { Greeting: { hi: "Hello" } }, // default {}
|
|
361
|
+
});
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Wraps `ui` in next-intl's `NextIntlClientProvider` and renders via
|
|
365
|
+
`@testing-library/react`. Both are optional peers, resolved lazily.
|
|
366
|
+
|
|
367
|
+
### `expectNoAxeViolations` — `hazo_testing/axe`
|
|
368
|
+
|
|
369
|
+
```ts
|
|
370
|
+
import { expectNoAxeViolations } from "hazo_testing/axe";
|
|
371
|
+
|
|
372
|
+
await expectNoAxeViolations(container); // WCAG 2.0/2.1 A + AA by default
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Throws a formatted error listing any axe-core violations. `axe-core` ships as a
|
|
376
|
+
direct dependency.
|
|
377
|
+
|
|
378
|
+
---
|
|
379
|
+
|
|
307
380
|
## Reset hooks
|
|
308
381
|
|
|
309
382
|
```ts
|
package/SETUP_CHECKLIST.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# Setup Checklist
|
|
2
2
|
|
|
3
|
-
## v0.1.0 — initial setup
|
|
4
|
-
|
|
5
3
|
### 1. Add the dev dependency
|
|
6
4
|
|
|
7
5
|
```bash
|
|
@@ -105,7 +103,39 @@ If you use `hazo_testing/nextjs` for in-process server tests, install the option
|
|
|
105
103
|
npm install --save-dev next
|
|
106
104
|
```
|
|
107
105
|
|
|
108
|
-
### 9.
|
|
106
|
+
### 9. (Optional) Use the Vitest preset instead of Jest (v0.2.0+)
|
|
107
|
+
|
|
108
|
+
`hazo_testing/vitest-preset` is an ESM config factory — the Vitest counterpart
|
|
109
|
+
to `hazo_testing/jest-preset`. Create `vitest.config.ts`:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
// vitest.config.ts
|
|
113
|
+
import { defineConfig } from "vitest/config";
|
|
114
|
+
import hazoTestingVitest from "hazo_testing/vitest-preset";
|
|
115
|
+
|
|
116
|
+
export default defineConfig(hazoTestingVitest());
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
It sets `test.environment: "node"` (opt into jsdom per-file with a
|
|
120
|
+
`// @vitest-environment jsdom` docblock), wires `hazo_testing/vitest-setup`
|
|
121
|
+
(the afterEach reset-hook runner), `testTimeout: 10000`, a `server-only` →
|
|
122
|
+
empty-stub alias, and `server.deps.inline: [/hazo_/]`. Pass `{ alias,
|
|
123
|
+
setupFiles }` to extend. Install the optional peer:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npm install --save-dev vitest
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
For component tests, two more optional helpers ship in 0.2.0:
|
|
130
|
+
|
|
131
|
+
- **`hazo_testing/render`** — `renderWithIntl(ui, { locale?, messages? })`
|
|
132
|
+
wraps a component in next-intl's `NextIntlClientProvider`. Needs the optional
|
|
133
|
+
peers `next-intl` and `@testing-library/react`.
|
|
134
|
+
- **`hazo_testing/axe`** — `expectNoAxeViolations(container, options?)` asserts a
|
|
135
|
+
DOM container has no axe-core violations (WCAG 2.0/2.1 A + AA by default).
|
|
136
|
+
`axe-core` ships as a direct dependency — no extra install.
|
|
137
|
+
|
|
138
|
+
### 10. Add `.gitignore` entries
|
|
109
139
|
|
|
110
140
|
`wireHazoLlmApi` calls `initialize_llm_api` internally, which creates a `prompt_library.sqlite` file in the current working directory. Add it to your consumer's `.gitignore`:
|
|
111
141
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/axe_helper.ts
|
|
2
|
+
// Purpose: assert that a rendered DOM container has no axe-core accessibility
|
|
3
|
+
// violations. `axe-core` is a real dependency (small, always needed when a11y
|
|
4
|
+
// testing is used), so it is imported statically rather than as an optional
|
|
5
|
+
// peer.
|
|
6
|
+
import axe from "axe-core";
|
|
7
|
+
const DEFAULT_OPTIONS = {
|
|
8
|
+
runOnly: { type: "tag", values: ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"] },
|
|
9
|
+
};
|
|
10
|
+
export async function expectNoAxeViolations(container, options = DEFAULT_OPTIONS) {
|
|
11
|
+
const results = await axe.run(container, options);
|
|
12
|
+
if (results.violations.length === 0)
|
|
13
|
+
return;
|
|
14
|
+
const msg = results.violations.map(v => `[${v.impact}] ${v.description}\n Nodes: ${v.nodes.map(n => n.html).join(", ")}`).join("\n");
|
|
15
|
+
throw new Error(`Axe violations found:\n${msg}`);
|
|
16
|
+
}
|
|
@@ -2,13 +2,27 @@ import type { AugmentedAdapter } from "../db/augmented_adapter.js";
|
|
|
2
2
|
export interface CreateTestUserOptions {
|
|
3
3
|
email: string;
|
|
4
4
|
name?: string;
|
|
5
|
+
/** Role name to assign. Auto-created if it doesn't exist yet. */
|
|
5
6
|
role?: string;
|
|
7
|
+
/** Scope id to assign the user to. Auto-created if it doesn't exist yet. */
|
|
6
8
|
scopeId?: string;
|
|
9
|
+
/** Name for the auto-created scope row (defaults to scopeId). */
|
|
10
|
+
scopeName?: string;
|
|
11
|
+
/** Level for the auto-created scope row (defaults to "firm"). */
|
|
12
|
+
scopeLevel?: string;
|
|
13
|
+
/** Permission names to grant via the role. Auto-created if they don't exist. */
|
|
14
|
+
permissions?: string[];
|
|
7
15
|
columnOverrides?: Record<string, unknown>;
|
|
8
16
|
}
|
|
9
17
|
export interface TestUser {
|
|
10
18
|
id: string;
|
|
11
19
|
email: string;
|
|
12
20
|
sessionCookie: string;
|
|
21
|
+
/** Populated only when role/permissions/scopeId were given. */
|
|
22
|
+
roleId?: string;
|
|
23
|
+
roleName?: string;
|
|
24
|
+
/** The permission names that were seeded (parallel to opts.permissions). */
|
|
25
|
+
permissions?: string[];
|
|
26
|
+
scopeId?: string;
|
|
13
27
|
}
|
|
14
28
|
export declare function createTestUser(adapter: AugmentedAdapter, opts: CreateTestUserOptions): Promise<TestUser>;
|
package/dist/factories/users.js
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
// Jest imports). We replicate the same HS256 logic.
|
|
9
9
|
// - Cookie string is built as "hazo_auth_session=<token>" without calling
|
|
10
10
|
// get_cookie_name (which requires a hazo_auth_config.ini with cookie_prefix).
|
|
11
|
+
// - Junction tables (hazo_user_roles, hazo_role_permissions, hazo_user_scopes)
|
|
12
|
+
// have composite PKs and no `id` column, so we bypass insertOne (which
|
|
13
|
+
// auto-injects an id) and use rawQuery with INSERT OR IGNORE instead.
|
|
11
14
|
import { SignJWT } from "jose";
|
|
12
15
|
/**
|
|
13
16
|
* Mint a JWT with the same algorithm/claims as hazo_auth's create_session_token.
|
|
@@ -27,6 +30,15 @@ async function mintSessionToken(user_id, email) {
|
|
|
27
30
|
.setExpirationTime(exp)
|
|
28
31
|
.sign(secret);
|
|
29
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Parameterized INSERT into a junction table that has no `id` column.
|
|
35
|
+
* INSERT OR IGNORE keeps calls idempotent against duplicate composite keys.
|
|
36
|
+
* Uses rawQuery (SqliteAdapter-specific; cast to any mirrors how execScript
|
|
37
|
+
* is accessed elsewhere in the package).
|
|
38
|
+
*/
|
|
39
|
+
async function insertJunction(adapter, sql, params) {
|
|
40
|
+
await adapter.rawQuery(sql, { params });
|
|
41
|
+
}
|
|
30
42
|
export async function createTestUser(adapter, opts) {
|
|
31
43
|
const row = await adapter.insertOne("hazo_users", {
|
|
32
44
|
// hazo_users uses email_address, not email
|
|
@@ -36,9 +48,57 @@ export async function createTestUser(adapter, opts) {
|
|
|
36
48
|
});
|
|
37
49
|
const token = await mintSessionToken(row.id, opts.email);
|
|
38
50
|
// Build a minimal session cookie string (no prefix — mirrors a default-prefix app).
|
|
39
|
-
// The test only asserts sessionCookie.toMatch(/=/) so this is sufficient.
|
|
40
51
|
const sessionCookie = `hazo_auth_session=${token}; ` +
|
|
41
52
|
`hazo_auth_user_id=${row.id}; ` +
|
|
42
53
|
`hazo_auth_user_email=${encodeURIComponent(opts.email)}`;
|
|
43
|
-
|
|
54
|
+
const base = { id: row.id, email: opts.email, sessionCookie };
|
|
55
|
+
// Short-circuit: if no role/permissions/scope requested, stay backward-compatible
|
|
56
|
+
// and return the same shape as v0.2.x (no extra fields on the result).
|
|
57
|
+
const needsChain = Boolean(opts.role || opts.permissions?.length || opts.scopeId);
|
|
58
|
+
if (!needsChain)
|
|
59
|
+
return base;
|
|
60
|
+
// --- Role (required whenever permissions or a scope are involved,
|
|
61
|
+
// because hazo_user_scopes.role_id is NOT NULL). ---
|
|
62
|
+
const roleName = opts.role ?? `role_${row.id.slice(0, 8)}`;
|
|
63
|
+
let roleRow = await adapter.findOne("hazo_roles", { role_name: roleName });
|
|
64
|
+
if (!roleRow) {
|
|
65
|
+
roleRow = await adapter.insertOne("hazo_roles", { role_name: roleName });
|
|
66
|
+
}
|
|
67
|
+
const roleId = roleRow.id;
|
|
68
|
+
await insertJunction(adapter, "INSERT OR IGNORE INTO hazo_user_roles (user_id, role_id) VALUES (?, ?)", [row.id, roleId]);
|
|
69
|
+
// --- Permissions ---
|
|
70
|
+
const seededPermissions = [];
|
|
71
|
+
for (const permName of opts.permissions ?? []) {
|
|
72
|
+
let permRow = await adapter.findOne("hazo_permissions", { permission_name: permName });
|
|
73
|
+
if (!permRow) {
|
|
74
|
+
permRow = await adapter.insertOne("hazo_permissions", { permission_name: permName });
|
|
75
|
+
}
|
|
76
|
+
await insertJunction(adapter, "INSERT OR IGNORE INTO hazo_role_permissions (role_id, permission_id) VALUES (?, ?)", [roleId, permRow.id]);
|
|
77
|
+
seededPermissions.push(permName);
|
|
78
|
+
}
|
|
79
|
+
// --- Scope ---
|
|
80
|
+
let resolvedScopeId;
|
|
81
|
+
if (opts.scopeId) {
|
|
82
|
+
const scopeId = opts.scopeId;
|
|
83
|
+
let scopeRow = await adapter.findOne("hazo_scopes", { id: scopeId });
|
|
84
|
+
if (!scopeRow) {
|
|
85
|
+
// Pass explicit id so the scope is addressable by the caller-supplied value.
|
|
86
|
+
// CrudService only generates an id when the field is absent, so this is honoured.
|
|
87
|
+
scopeRow = await adapter.insertOne("hazo_scopes", {
|
|
88
|
+
id: scopeId,
|
|
89
|
+
name: opts.scopeName ?? scopeId,
|
|
90
|
+
level: opts.scopeLevel ?? "firm",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
// root_scope_id == scope_id for a single-level (root) scope.
|
|
94
|
+
await insertJunction(adapter, "INSERT OR IGNORE INTO hazo_user_scopes (user_id, scope_id, root_scope_id, role_id) VALUES (?, ?, ?, ?)", [row.id, scopeId, scopeId, roleId]);
|
|
95
|
+
resolvedScopeId = scopeId;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
...base,
|
|
99
|
+
roleId,
|
|
100
|
+
roleName,
|
|
101
|
+
...(seededPermissions.length ? { permissions: seededPermissions } : {}),
|
|
102
|
+
...(resolvedScopeId !== undefined ? { scopeId: resolvedScopeId } : {}),
|
|
103
|
+
};
|
|
44
104
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// src/hooks/after_each_setup.vitest.ts
|
|
2
|
+
// Purpose: Vitest counterpart to after_each_setup.ts — runs all registered
|
|
3
|
+
// reset hooks after every test. Unlike Jest, Vitest does not inject `afterEach`
|
|
4
|
+
// as a global, so it is imported explicitly from "vitest". Sibling file; the
|
|
5
|
+
// Jest version (after_each_setup.ts) is left untouched.
|
|
6
|
+
import { afterEach } from "vitest";
|
|
7
|
+
import { runResetHooks } from "./reset_hooks.js";
|
|
8
|
+
afterEach(async () => { await runResetHooks(); });
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const HAZO_TESTING_VERSION = "0.
|
|
1
|
+
export declare const HAZO_TESTING_VERSION = "0.3.0";
|
|
2
2
|
export { DockerNotSupported, UnmappedLlmCall } from "./errors.js";
|
|
3
3
|
export { augmentAdapter } from "./db/augmented_adapter.js";
|
|
4
4
|
export type { AugmentedAdapter } from "./db/augmented_adapter.js";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
// hazo_testing public server barrel.
|
|
3
|
-
export const HAZO_TESTING_VERSION = "0.
|
|
3
|
+
export const HAZO_TESTING_VERSION = "0.3.0";
|
|
4
4
|
export { DockerNotSupported, UnmappedLlmCall } from "./errors.js";
|
|
5
5
|
export { augmentAdapter } from "./db/augmented_adapter.js";
|
|
6
6
|
export { createTestDatabase, createTestDatabaseSqlite, createTestDatabaseDocker, } from "./db/test_database.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface RenderWithIntlOptions {
|
|
3
|
+
/** BCP-47 locale passed to NextIntlClientProvider. Defaults to "en". */
|
|
4
|
+
locale?: string;
|
|
5
|
+
/** Message catalog passed to NextIntlClientProvider. Defaults to {}. */
|
|
6
|
+
messages?: Record<string, unknown>;
|
|
7
|
+
/** Extra options forwarded to @testing-library/react's render(). */
|
|
8
|
+
renderOptions?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Render `ui` inside a NextIntlClientProvider. Returns the
|
|
12
|
+
* `@testing-library/react` RenderResult. Both peers are resolved lazily.
|
|
13
|
+
*/
|
|
14
|
+
export declare function renderWithIntl(ui: React.ReactElement, options?: RenderWithIntlOptions): Promise<unknown>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Render `ui` inside a NextIntlClientProvider. Returns the
|
|
4
|
+
* `@testing-library/react` RenderResult. Both peers are resolved lazily.
|
|
5
|
+
*/
|
|
6
|
+
export async function renderWithIntl(ui, options = {}) {
|
|
7
|
+
const { locale = "en", messages = {}, renderOptions } = options;
|
|
8
|
+
let NextIntlClientProvider;
|
|
9
|
+
try {
|
|
10
|
+
// optional peer — `next-intl` may not be installed at runtime
|
|
11
|
+
({ NextIntlClientProvider } = await import("next-intl"));
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
throw new Error("renderWithIntl requires the optional peer dependency `next-intl` to be installed.");
|
|
15
|
+
}
|
|
16
|
+
let render;
|
|
17
|
+
try {
|
|
18
|
+
// optional peer — `@testing-library/react` may not be installed at runtime
|
|
19
|
+
({ render } = await import("@testing-library/react"));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new Error("renderWithIntl requires the optional peer dependency `@testing-library/react` to be installed.");
|
|
23
|
+
}
|
|
24
|
+
const wrapped = (_jsx(NextIntlClientProvider, { locale: locale, messages: messages, children: ui }));
|
|
25
|
+
return render(wrapped, renderOptions);
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./hooks/after_each_setup.vitest.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// src/vitest-setup.ts
|
|
2
|
+
// Purpose: Vitest setupFiles entry point referenced by vitest-preset.mjs
|
|
3
|
+
// (`setupFiles: ["hazo_testing/vitest-setup"]`). Wires the afterEach reset-hook
|
|
4
|
+
// runner into every Vitest test file. The Jest equivalents (polyfills.ts +
|
|
5
|
+
// after_each_setup.ts, wired via the jest preset) are left unchanged.
|
|
6
|
+
import "./hooks/after_each_setup.vitest.js";
|
package/package.json
CHANGED
|
@@ -1,55 +1,113 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hazo_testing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared test infrastructure for hazo apps: SQLite test DB, real-package wiring, auth factories, apiTestClient, fixtures, Jest
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Shared test infrastructure for hazo apps: SQLite test DB, real-package wiring, auth factories, apiTestClient, fixtures, Jest + Vitest presets.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./client": {
|
|
14
|
+
"types": "./dist/index.client.d.ts",
|
|
15
|
+
"default": "./dist/index.client.js"
|
|
16
|
+
},
|
|
11
17
|
"./jest-preset": "./jest-preset.cjs",
|
|
12
|
-
"./
|
|
13
|
-
"./
|
|
18
|
+
"./vitest-preset": "./vitest-preset.mjs",
|
|
19
|
+
"./vitest-setup": {
|
|
20
|
+
"types": "./dist/vitest-setup.d.ts",
|
|
21
|
+
"default": "./dist/vitest-setup.js"
|
|
22
|
+
},
|
|
23
|
+
"./render": {
|
|
24
|
+
"types": "./dist/render_with_intl.d.ts",
|
|
25
|
+
"default": "./dist/render_with_intl.js"
|
|
26
|
+
},
|
|
27
|
+
"./axe": {
|
|
28
|
+
"types": "./dist/axe_helper.d.ts",
|
|
29
|
+
"default": "./dist/axe_helper.js"
|
|
30
|
+
},
|
|
31
|
+
"./playwright": {
|
|
32
|
+
"types": "./dist/playwright/index.d.ts",
|
|
33
|
+
"default": "./dist/playwright/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./nextjs": {
|
|
36
|
+
"types": "./dist/nextjs/index.d.ts",
|
|
37
|
+
"default": "./dist/nextjs/index.js"
|
|
38
|
+
}
|
|
14
39
|
},
|
|
15
|
-
"files": [
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"jest-preset.cjs",
|
|
43
|
+
"vitest-preset.mjs",
|
|
44
|
+
"config",
|
|
45
|
+
"README.md",
|
|
46
|
+
"SETUP_CHECKLIST.md",
|
|
47
|
+
"CHANGE_LOG.md"
|
|
48
|
+
],
|
|
16
49
|
"scripts": {
|
|
17
50
|
"build": "tsc -p tsconfig.build.json",
|
|
18
51
|
"lint": "tsc --noEmit",
|
|
19
|
-
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
|
|
52
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
53
|
+
"dev:test-app": "npm run build && cd test-app && npm run dev",
|
|
54
|
+
"build:test-app": "npm run build && cd test-app && npm run build"
|
|
20
55
|
},
|
|
21
56
|
"dependencies": {
|
|
57
|
+
"axe-core": "^4.12.1",
|
|
22
58
|
"jose": "^4.15.9 || ^5.9.6"
|
|
23
59
|
},
|
|
24
60
|
"peerDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"hazo_auth": "^
|
|
61
|
+
"@playwright/test": "^1.0.0",
|
|
62
|
+
"@testing-library/react": "^16.0.0",
|
|
63
|
+
"hazo_auth": "^10.0.0",
|
|
64
|
+
"hazo_connect": "^3.5.1",
|
|
65
|
+
"hazo_core": "^1.1.0",
|
|
28
66
|
"hazo_jobs": "^0.12.0",
|
|
29
|
-
"hazo_notify": "^6.1.0",
|
|
30
67
|
"hazo_llm_api": "^2.1.0",
|
|
68
|
+
"hazo_notify": "^6.1.3",
|
|
69
|
+
"next": "^14.0.0 || ^16.0.0",
|
|
70
|
+
"next-intl": "^4.0.0",
|
|
31
71
|
"react": "^18.0.0 || ^19.0.0",
|
|
32
72
|
"react-dom": "^18.0.0 || ^19.0.0",
|
|
33
|
-
"
|
|
34
|
-
"next": "^14.0.0 || ^16.0.0"
|
|
73
|
+
"vitest": "^2.0.0 || ^3.0.0"
|
|
35
74
|
},
|
|
36
75
|
"peerDependenciesMeta": {
|
|
37
|
-
"@playwright/test": {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"react
|
|
76
|
+
"@playwright/test": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@testing-library/react": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"next": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"next-intl": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"react": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"react-dom": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"vitest": {
|
|
95
|
+
"optional": true
|
|
96
|
+
}
|
|
41
97
|
},
|
|
42
98
|
"devDependencies": {
|
|
43
|
-
"typescript": "^5.7.2",
|
|
44
|
-
"jest": "^30.2.0",
|
|
45
|
-
"ts-jest": "^29.4.5",
|
|
46
|
-
"@types/jest": "^30.0.0",
|
|
47
|
-
"jest-environment-node": "^30.2.0",
|
|
48
|
-
"jest-environment-jsdom": "^30.2.0",
|
|
49
|
-
"@testing-library/react": "^16.1.0",
|
|
50
99
|
"@testing-library/jest-dom": "^6.6.3",
|
|
100
|
+
"@testing-library/react": "^16.1.0",
|
|
101
|
+
"@types/jest": "^30.0.0",
|
|
51
102
|
"@types/node": "^22.10.0",
|
|
52
103
|
"@types/react": "^19.0.0",
|
|
53
|
-
"@types/react-dom": "^19.0.0"
|
|
104
|
+
"@types/react-dom": "^19.0.0",
|
|
105
|
+
"jest": "^30.2.0",
|
|
106
|
+
"jest-environment-jsdom": "^30.2.0",
|
|
107
|
+
"jest-environment-node": "^30.2.0",
|
|
108
|
+
"next-intl": "^4.13.0",
|
|
109
|
+
"ts-jest": "^29.4.5",
|
|
110
|
+
"typescript": "^5.7.2",
|
|
111
|
+
"vitest": "^3.2.6"
|
|
54
112
|
}
|
|
55
113
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// vitest-preset.mjs
|
|
2
|
+
// Purpose: shared Vitest config factory for hazo consumers — the Vitest
|
|
3
|
+
// counterpart to jest-preset.cjs. Additive: the Jest preset is unchanged.
|
|
4
|
+
// Usage:
|
|
5
|
+
// import hazoTestingVitest from "hazo_testing/vitest-preset";
|
|
6
|
+
// import { defineConfig } from "vitest/config";
|
|
7
|
+
// export default defineConfig(hazoTestingVitest());
|
|
8
|
+
//
|
|
9
|
+
// The package is `type: "module"`, and Vitest reads its config through Vite's
|
|
10
|
+
// ESM loader, so this factory is `.mjs` (mirrors jest-preset.cjs being CJS for
|
|
11
|
+
// Jest's synchronous config read).
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
|
|
15
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
|
|
17
|
+
// Empty stub for the `server-only` package. The real module throws unless it is
|
|
18
|
+
// imported from an RSC bundler context, which breaks any server module that
|
|
19
|
+
// begins with `import "server-only";` under Vitest. Aliasing it to this dist
|
|
20
|
+
// file lets those modules import cleanly. (Mirror of Kinstripe's
|
|
21
|
+
// src/test/server-only-stub.ts, shipped here so consumers need no local copy.)
|
|
22
|
+
const serverOnlyStub = path.join(__dirname, "dist/test/server_only_stub.js");
|
|
23
|
+
|
|
24
|
+
export default function vitestPreset(options = {}) {
|
|
25
|
+
const { alias = {}, setupFiles = [] } = options;
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
test: {
|
|
29
|
+
// Node by default — the consuming project opts into jsdom per-file via
|
|
30
|
+
// the `// @vitest-environment jsdom` docblock comment.
|
|
31
|
+
environment: "node",
|
|
32
|
+
setupFiles: ["hazo_testing/vitest-setup", ...setupFiles],
|
|
33
|
+
testTimeout: 10000,
|
|
34
|
+
},
|
|
35
|
+
resolve: {
|
|
36
|
+
alias: {
|
|
37
|
+
"server-only": serverOnlyStub,
|
|
38
|
+
...alias,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
server: {
|
|
42
|
+
// hazo_* packages ship ESM/CJS dual builds; Vite must inline-transform
|
|
43
|
+
// them rather than externalising, or their sub-path exports break.
|
|
44
|
+
deps: {
|
|
45
|
+
inline: [/hazo_/],
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|