create-tigra 3.3.0 → 4.0.1
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/CHANGELOG.md +151 -0
- package/README.md +264 -56
- package/bin/create-tigra.js +497 -472
- package/lib/patchers/email-verification.patcher.js +72 -10
- package/modules/email-verification/client/hooks/useVerification.ts +4 -1
- package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
- package/modules/email-verification/server/verification.controller.ts +3 -1
- package/modules/email-verification/server/verification.service.ts +44 -13
- package/package.json +5 -3
- package/template/.github/dependabot.yml +47 -0
- package/template/.github/workflows/audit.yml +36 -0
- package/template/.github/workflows/ci.yml +79 -0
- package/template/AGENTS.md +18 -9
- package/template/README.md +79 -0
- package/template/client/.dockerignore +1 -0
- package/template/client/.env.example.production +13 -2
- package/template/client/AGENTS.md +13 -5
- package/template/client/Dockerfile +186 -14
- package/template/client/README.md +92 -21
- package/template/client/next.config.ts +1 -1
- package/template/client/package-lock.json +15983 -14181
- package/template/client/package.json +56 -48
- package/template/client/scripts/check-sharp.mjs +53 -0
- package/template/client/scripts/next-dev.cjs +5 -1
- package/template/client/src/__tests__/proxy.test.ts +92 -0
- package/template/client/src/app/healthz/route.ts +16 -0
- package/template/client/src/app/layout.tsx +9 -3
- package/template/client/src/app/page.tsx +2 -2
- package/template/client/src/app/providers.tsx +23 -4
- package/template/client/src/components/layout/Header.tsx +7 -3
- package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
- package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
- package/template/client/src/features/admin/types/admin.types.ts +2 -3
- package/template/client/src/features/auth/components/AuthInitializer.tsx +35 -18
- package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
- package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
- package/template/client/src/features/auth/components/LoginForm.tsx +13 -2
- package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
- package/template/client/src/features/auth/hooks/useAuth.ts +11 -1
- package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
- package/template/client/src/features/auth/services/auth.service.ts +8 -3
- package/template/client/src/features/auth/store/authSlice.ts +7 -2
- package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
- package/template/client/src/lib/api/api.types.ts +14 -0
- package/template/client/src/lib/api/axios.config.ts +101 -15
- package/template/client/src/lib/api/hard-redirect.ts +8 -0
- package/template/client/src/lib/api/session-events.ts +29 -0
- package/template/client/src/lib/constants/routes.ts +12 -0
- package/template/client/src/lib/observability/sentry-scrub.ts +1 -1
- package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
- package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
- package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
- package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
- package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
- package/template/client/src/lib/utils/error.ts +81 -0
- package/template/client/src/lib/utils/form.ts +31 -0
- package/template/client/src/lib/utils/paths.ts +18 -0
- package/template/client/src/lib/utils/session-marker.ts +24 -0
- package/template/client/src/{middleware.ts → proxy.ts} +25 -11
- package/template/client/src/store/__tests__/store.test.ts +44 -0
- package/template/client/src/store/index.ts +26 -8
- package/template/client/vitest.config.mts +48 -0
- package/template/server/.env.example +280 -270
- package/template/server/.env.example.production +283 -242
- package/template/server/AGENTS.md +13 -4
- package/template/server/Dockerfile +176 -14
- package/template/server/README.md +108 -0
- package/template/server/docker-compose.yml +4 -2
- package/template/server/docs/deployment.md +63 -0
- package/template/server/docs/logging.md +66 -4
- package/template/server/docs/rate-limits.md +153 -0
- package/template/server/docs/testing.md +81 -0
- package/template/server/eslint.config.mjs +19 -0
- package/template/server/package-lock.json +8784 -8743
- package/template/server/package.json +79 -75
- package/template/server/prisma/migrations/0_init/migration.sql +82 -0
- package/template/server/prisma/migrations/migration_lock.toml +3 -0
- package/template/server/prisma/schema.prisma +0 -3
- package/template/server/scripts/check-heic.mjs +40 -0
- package/template/server/scripts/check-sharp.mjs +53 -0
- package/template/server/src/__tests__/routes.integration.test.ts +280 -0
- package/template/server/src/app.ts +248 -346
- package/template/server/src/config/__tests__/env.test.ts +219 -0
- package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
- package/template/server/src/config/env.ts +326 -182
- package/template/server/src/config/rate-limit.config.ts +157 -106
- package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +60 -8
- package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
- package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
- package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
- package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +38 -36
- package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
- package/template/server/src/jobs/index.ts +12 -2
- package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
- package/template/server/src/jobs/schedule.ts +50 -0
- package/template/server/src/libs/__tests__/auth.test.ts +98 -7
- package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
- package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
- package/template/server/src/libs/__tests__/cookies.test.ts +53 -2
- package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
- package/template/server/src/libs/__tests__/email.test.ts +68 -0
- package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
- package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
- package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
- package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
- package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
- package/template/server/src/libs/__tests__/http.test.ts +74 -2
- package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
- package/template/server/src/libs/__tests__/logger.test.ts +50 -0
- package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
- package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
- package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
- package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
- package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
- package/template/server/src/libs/__tests__/redis.test.ts +38 -2
- package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
- package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
- package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
- package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -80
- package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
- package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
- package/template/server/src/libs/auth.ts +39 -10
- package/template/server/src/libs/client-ip.ts +158 -77
- package/template/server/src/libs/cloudflare-ranges.ts +132 -0
- package/template/server/src/libs/cookies.ts +53 -14
- package/template/server/src/libs/email-quota.ts +74 -0
- package/template/server/src/libs/email.ts +46 -9
- package/template/server/src/libs/error-handler.ts +111 -0
- package/template/server/src/libs/health-routes.ts +53 -0
- package/template/server/src/libs/http.ts +139 -47
- package/template/server/src/libs/ip-address.ts +37 -0
- package/template/server/src/libs/ip-block.ts +284 -221
- package/template/server/src/libs/logger.ts +41 -12
- package/template/server/src/libs/monitoring.ts +127 -41
- package/template/server/src/libs/observability/sentry.ts +1 -1
- package/template/server/src/libs/origin-check.ts +9 -7
- package/template/server/src/libs/prisma.ts +47 -4
- package/template/server/src/libs/query-counter.ts +71 -59
- package/template/server/src/libs/rate-limiting.ts +110 -0
- package/template/server/src/libs/redis-required.ts +28 -0
- package/template/server/src/libs/redis.ts +27 -1
- package/template/server/src/libs/request-id.ts +18 -0
- package/template/server/src/libs/requestLogger.ts +112 -17
- package/template/server/src/libs/safe-http.ts +101 -0
- package/template/server/src/libs/shutdown.ts +87 -0
- package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
- package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +59 -0
- package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
- package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
- package/template/server/src/libs/storage/file-storage.service.ts +94 -84
- package/template/server/src/libs/storage/file-validator.ts +48 -46
- package/template/server/src/libs/storage/image-optimizer.service.ts +16 -5
- package/template/server/src/libs/token-hash.ts +18 -0
- package/template/server/src/libs/url-safety.ts +127 -121
- package/template/server/src/libs/validation-errors.ts +86 -0
- package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
- package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
- package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
- package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
- package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
- package/template/server/src/modules/admin/admin.controller.ts +66 -49
- package/template/server/src/modules/admin/admin.repo.ts +49 -10
- package/template/server/src/modules/admin/admin.routes.ts +48 -2
- package/template/server/src/modules/admin/admin.schemas.ts +18 -6
- package/template/server/src/modules/admin/admin.service.ts +66 -4
- package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
- package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
- package/template/server/src/modules/auth/__tests__/auth.service.test.ts +197 -37
- package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
- package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
- package/template/server/src/modules/auth/auth.controller.ts +133 -128
- package/template/server/src/modules/auth/auth.repo.ts +44 -44
- package/template/server/src/modules/auth/auth.schemas.ts +4 -2
- package/template/server/src/modules/auth/auth.service.ts +167 -96
- package/template/server/src/modules/auth/session.repo.ts +12 -0
- package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
- package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
- package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
- package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
- package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
- package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
- package/template/server/src/modules/users/users.controller.ts +9 -38
- package/template/server/src/modules/users/users.repo.ts +23 -7
- package/template/server/src/modules/users/users.routes.ts +3 -22
- package/template/server/src/modules/users/users.schemas.ts +15 -9
- package/template/server/src/modules/users/users.service.ts +105 -93
- package/template/server/src/scripts/make-admin.ts +48 -0
- package/template/server/src/server.ts +48 -26
- package/template/server/src/shared/errors/AppError.ts +27 -1
- package/template/server/src/shared/errors/errors.ts +42 -5
- package/template/server/src/shared/responses/successResponse.ts +22 -3
- package/template/server/src/shared/types/index.ts +7 -0
- package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
- package/template/server/src/test/integration-helpers.ts +343 -0
- package/template/server/src/test/integration-services.ts +74 -0
- package/template/server/src/test/integration.global-setup.ts +144 -0
- package/template/server/src/test/integration.setup.ts +35 -158
- package/template/server/src/test/setup.ts +3 -10
- package/template/server/tsconfig.build.json +1 -1
- package/template/server/tsconfig.json +1 -0
- package/template/server/vitest.config.ts +17 -4
- package/template/server/vitest.integration.config.ts +18 -13
- package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server's "signed in" marker: the auth_session cookie (value "1", not a
|
|
3
|
+
* secret, readable by JavaScript; server src/libs/cookies.ts). It lives as long
|
|
4
|
+
* as the refresh token, and in a split deployment (app. + api. subdomains) it
|
|
5
|
+
* reaches the client only through COOKIE_DOMAIN, like everything that depends
|
|
6
|
+
* on it, the proxy's protected-page check included.
|
|
7
|
+
*/
|
|
8
|
+
export const SESSION_MARKER_COOKIE = 'auth_session';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether a cookie string holds the marker. Without it there is no session to
|
|
12
|
+
* ask about, so the start-up "who am I" call is skipped and an anonymous
|
|
13
|
+
* visitor makes no API calls at all. Defaults to this browser's cookies, and is
|
|
14
|
+
* false during server rendering (no document).
|
|
15
|
+
*/
|
|
16
|
+
export function hasSessionMarker(
|
|
17
|
+
cookies: string | undefined = typeof document === 'undefined' ? undefined : document.cookie
|
|
18
|
+
): boolean {
|
|
19
|
+
if (!cookies) return false;
|
|
20
|
+
return cookies.split(';').some((part) => {
|
|
21
|
+
const [name, ...value] = part.trim().split('=');
|
|
22
|
+
return name === SESSION_MARKER_COOKIE && value.join('=') !== '';
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js proxy (the file convention formerly called middleware; renamed in
|
|
3
|
+
* Next 16, which runs it on the Node.js runtime). It runs before every page
|
|
4
|
+
* request except static assets and /healthz (see `config` below): it mints the
|
|
5
|
+
* per-request CSP nonce, sets the security headers, and sends visitors without
|
|
6
|
+
* a session marker away from protected pages.
|
|
7
|
+
*/
|
|
1
8
|
import { NextResponse } from 'next/server';
|
|
2
9
|
|
|
3
10
|
import type { NextRequest } from 'next/server';
|
|
4
11
|
|
|
5
|
-
|
|
12
|
+
import { isProtectedPath } from '@/lib/utils/paths';
|
|
13
|
+
import { SESSION_MARKER_COOKIE } from '@/lib/utils/session-marker';
|
|
6
14
|
|
|
7
15
|
function isTokenExpired(token: string): boolean {
|
|
8
16
|
try {
|
|
@@ -35,7 +43,7 @@ const sentryOrigin = toOrigin(process.env.NEXT_PUBLIC_SENTRY_DSN);
|
|
|
35
43
|
|
|
36
44
|
/**
|
|
37
45
|
* Build the per-request Content-Security-Policy. A fresh nonce is minted for
|
|
38
|
-
* every request and threaded onto Next's own scripts (see
|
|
46
|
+
* every request and threaded onto Next's own scripts (see proxy below), so
|
|
39
47
|
* 'strict-dynamic' can trust them while refusing any attacker-injected inline
|
|
40
48
|
* script that lacks the nonce.
|
|
41
49
|
*
|
|
@@ -84,7 +92,7 @@ function applySecurityHeaders(response: NextResponse): NextResponse {
|
|
|
84
92
|
return response;
|
|
85
93
|
}
|
|
86
94
|
|
|
87
|
-
export function
|
|
95
|
+
export function proxy(request: NextRequest): NextResponse {
|
|
88
96
|
const { pathname } = request.nextUrl;
|
|
89
97
|
|
|
90
98
|
const isProd = process.env.NODE_ENV === 'production';
|
|
@@ -102,18 +110,22 @@ export function middleware(request: NextRequest): NextResponse {
|
|
|
102
110
|
requestHeaders.set('x-nonce', nonce);
|
|
103
111
|
requestHeaders.set('content-security-policy', cspHeader);
|
|
104
112
|
|
|
105
|
-
// NOTE: This
|
|
113
|
+
// NOTE: This proxy only checks COOKIE PRESENCE (and a best-effort,
|
|
106
114
|
// unverified exp claim) for UX-level routing — redirecting users who are
|
|
107
115
|
// obviously logged out away from protected pages. It does NOT verify the
|
|
108
116
|
// JWT signature and is NOT a security boundary. Real authorization happens
|
|
109
|
-
// server-side on every API call; a forged cookie gets past this
|
|
117
|
+
// server-side on every API call; a forged cookie gets past this proxy
|
|
110
118
|
// but every API request it makes will be rejected with 401/403.
|
|
119
|
+
// access_token is host-only on the API host, so in a split deployment only
|
|
120
|
+
// the session marker (shared through COOKIE_DOMAIN) is visible here.
|
|
111
121
|
const accessToken = request.cookies.get('access_token')?.value;
|
|
112
|
-
const authSession = request.cookies.get(
|
|
122
|
+
const authSession = request.cookies.get(SESSION_MARKER_COOKIE)?.value;
|
|
113
123
|
|
|
114
|
-
|
|
124
|
+
// Whole path segments (lib/utils/paths.ts): /admin/users is protected,
|
|
125
|
+
// /administrator is not.
|
|
126
|
+
const isProtected = isProtectedPath(pathname);
|
|
115
127
|
|
|
116
|
-
if (
|
|
128
|
+
if (isProtected) {
|
|
117
129
|
// No access_token AND no auth_session — truly unauthenticated.
|
|
118
130
|
if (!accessToken && !authSession) {
|
|
119
131
|
const loginUrl = new URL('/login', request.url);
|
|
@@ -130,7 +142,7 @@ export function middleware(request: NextRequest): NextResponse {
|
|
|
130
142
|
response.headers.set('content-security-policy', cspHeader);
|
|
131
143
|
applySecurityHeaders(response);
|
|
132
144
|
|
|
133
|
-
if (
|
|
145
|
+
if (isProtected) {
|
|
134
146
|
// No access_token BUT auth_session exists — session is alive, let through
|
|
135
147
|
// so client-side can do getMe() → 401 → refresh → retry.
|
|
136
148
|
// (response already passes through; nothing extra needed here.)
|
|
@@ -146,6 +158,8 @@ export function middleware(request: NextRequest): NextResponse {
|
|
|
146
158
|
|
|
147
159
|
export const config = {
|
|
148
160
|
// Apply security headers site-wide, but skip Next's static assets and the
|
|
149
|
-
// favicon (no CSP needed on immutable static files / images)
|
|
150
|
-
|
|
161
|
+
// favicon (no CSP needed on immutable static files / images), and the
|
|
162
|
+
// container health check /healthz (src/app/healthz/route.ts), which must
|
|
163
|
+
// answer 200 whatever this proxy decides for pages.
|
|
164
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico|healthz).*)'],
|
|
151
165
|
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { setUser } from '@/features/auth/store/authSlice';
|
|
4
|
+
import type { IUser } from '@/features/auth/types/auth.types';
|
|
5
|
+
|
|
6
|
+
import { makeStore } from '../index';
|
|
7
|
+
|
|
8
|
+
const user: IUser = {
|
|
9
|
+
id: 'u1',
|
|
10
|
+
email: 'a@example.test',
|
|
11
|
+
firstName: 'Ana',
|
|
12
|
+
lastName: 'Test',
|
|
13
|
+
role: 'USER',
|
|
14
|
+
isActive: true,
|
|
15
|
+
emailVerifiedAt: null,
|
|
16
|
+
avatarUrl: null,
|
|
17
|
+
createdAt: '2026-01-01T00:00:00.000Z',
|
|
18
|
+
updatedAt: '2026-01-01T00:00:00.000Z',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
describe('makeStore', () => {
|
|
22
|
+
it('without a session marker the visitor is signed out from the first render', () => {
|
|
23
|
+
expect(makeStore().getState().auth).toEqual({
|
|
24
|
+
user: null,
|
|
25
|
+
isAuthenticated: false,
|
|
26
|
+
isInitializing: false,
|
|
27
|
+
isLoggingOut: false,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('with a session marker the user is unknown until the "who am I" check answers', () => {
|
|
32
|
+
const auth = makeStore({ hasSession: true }).getState().auth;
|
|
33
|
+
expect(auth.isInitializing).toBe(true);
|
|
34
|
+
expect(auth.isAuthenticated).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('every store is its own: one visitor signing in never shows in another', () => {
|
|
38
|
+
const a = makeStore({ hasSession: true });
|
|
39
|
+
const b = makeStore({ hasSession: true });
|
|
40
|
+
a.dispatch(setUser(user));
|
|
41
|
+
expect(a.getState().auth.user?.firstName).toBe('Ana');
|
|
42
|
+
expect(b.getState().auth.user).toBeNull();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import { configureStore } from '@reduxjs/toolkit';
|
|
2
2
|
|
|
3
|
-
import authReducer from '@/features/auth/store/authSlice';
|
|
3
|
+
import authReducer, { initialAuthState } from '@/features/auth/store/authSlice';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
/**
|
|
6
|
+
* A new store per visitor: Providers makes one per page load in the browser and
|
|
7
|
+
* one per request during server rendering, so no visitor's state can ever leak
|
|
8
|
+
* into another's (Redux's guidance for the Next.js App Router: a module-level
|
|
9
|
+
* store would be shared by every request the server renders).
|
|
10
|
+
*
|
|
11
|
+
* `hasSession`: the request carries the server's session marker (the root
|
|
12
|
+
* layout reads it). Then who the visitor is stays unknown until the "who am I"
|
|
13
|
+
* check answers (isInitializing). Without it the visitor is plainly signed out
|
|
14
|
+
* from the first render, so the server already renders the signed-out header
|
|
15
|
+
* instead of one that flips after loading.
|
|
16
|
+
*/
|
|
17
|
+
export function makeStore({ hasSession = false }: { hasSession?: boolean } = {}) {
|
|
18
|
+
return configureStore({
|
|
19
|
+
reducer: {
|
|
20
|
+
auth: authReducer,
|
|
21
|
+
},
|
|
22
|
+
preloadedState: {
|
|
23
|
+
auth: { ...initialAuthState, isInitializing: hasSession },
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
10
27
|
|
|
11
|
-
export type
|
|
12
|
-
export type
|
|
28
|
+
export type AppStore = ReturnType<typeof makeStore>;
|
|
29
|
+
export type RootState = ReturnType<AppStore['getState']>;
|
|
30
|
+
export type AppDispatch = AppStore['dispatch'];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
|
|
3
|
+
import { defineConfig } from 'vitest/config';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Unit tests for the client's logic: the API client's session rules, the
|
|
7
|
+
* proxy's CSP and redirects, redirect safety, error parsing and the store.
|
|
8
|
+
* They run in Node; a file that needs `document` or `window` starts with
|
|
9
|
+
* `// @vitest-environment jsdom`. Screens are not unit-tested here: the
|
|
10
|
+
* template ships no pages, and the components are thin over these functions.
|
|
11
|
+
*
|
|
12
|
+
* Test files and this config are kept out of the production image
|
|
13
|
+
* (.dockerignore), so the image's `next build` never type-checks them. The
|
|
14
|
+
* .mts extension: this package is not `"type": "module"`, and Vite warns about
|
|
15
|
+
* ESM syntax in a config it would load as CommonJS.
|
|
16
|
+
*/
|
|
17
|
+
export default defineConfig({
|
|
18
|
+
test: {
|
|
19
|
+
environment: 'node',
|
|
20
|
+
include: ['src/**/*.test.ts'],
|
|
21
|
+
restoreMocks: true,
|
|
22
|
+
coverage: {
|
|
23
|
+
provider: 'v8',
|
|
24
|
+
reporter: ['text', 'json', 'html', 'lcov'],
|
|
25
|
+
// Every source file counts, also the ones no test loads, so the number
|
|
26
|
+
// is honest. Type-only files and the tests themselves are left out.
|
|
27
|
+
include: ['src/**/*.{ts,tsx}'],
|
|
28
|
+
exclude: ['**/__tests__/**', '**/*.test.ts', '**/*.d.ts', 'src/types/**'],
|
|
29
|
+
// A ratchet, not a target: the real numbers when they were set
|
|
30
|
+
// (2026-10-20), rounded down, from a project made with the
|
|
31
|
+
// email-verification module (its client code has no unit tests, so it
|
|
32
|
+
// sits about one point below a plain project). `npm run test:coverage`
|
|
33
|
+
// (and CI) fails below them; raise them when tests are added. Components
|
|
34
|
+
// and pages count but are thin: the logic they call is what is tested.
|
|
35
|
+
thresholds: {
|
|
36
|
+
lines: 23,
|
|
37
|
+
statements: 23,
|
|
38
|
+
branches: 27,
|
|
39
|
+
functions: 13,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
resolve: {
|
|
44
|
+
alias: {
|
|
45
|
+
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
});
|