toiljs 0.0.115 → 0.0.117
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 +10 -0
- package/README.md +16 -7
- package/TYPESCRIPT_LAW.md +1946 -2073
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2375 -258
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/routing/mount.js +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/auth-emails.js +7 -3
- package/build/compiler/emails.js +1 -1
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +25 -4
- package/build/compiler/pages.js +2 -3
- package/build/compiler/prerender.d.ts +3 -8
- package/build/compiler/prerender.js +88 -127
- package/build/compiler/ssg.js +3 -4
- package/build/compiler/template-build.js +3 -4
- package/build/compiler/toil-docs.generated.js +6 -5
- package/build/compiler/vite.js +3 -6
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/db/database.js +7 -3
- package/build/devserver/db/types.js +1 -1
- package/build/devserver/production.js +4 -3
- package/build/devserver/runtime/crypto.d.ts +1 -1
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/examples/basic/client/routes/analytics.tsx +7 -5
- package/examples/basic/server/tsconfig.json +1 -3
- package/package.json +45 -44
- package/presets/no-uint8array-tostring.js +120 -153
- package/presets/oxlint.d.ts +5 -0
- package/presets/oxlint.js +224 -0
- package/server/runtime/tsconfig.json +1 -3
- package/tsconfig.json +2 -11
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
- package/.github/changelog-config.json +0 -45
- package/.github/dependabot.yml +0 -27
- package/.github/workflows/ci.yml +0 -222
- package/.vscode/settings.json +0 -10
- package/docs/README.md +0 -107
- package/docs/auth/README.md +0 -104
- package/docs/auth/configuration.md +0 -105
- package/docs/auth/emails.md +0 -92
- package/docs/auth/extending.md +0 -202
- package/docs/auth/how-it-works.md +0 -138
- package/docs/auth/usage.md +0 -188
- package/docs/backend/README.md +0 -143
- package/docs/backend/data.md +0 -351
- package/docs/backend/rest.md +0 -402
- package/docs/backend/rpc.md +0 -226
- package/docs/background/README.md +0 -114
- package/docs/background/daemons.md +0 -276
- package/docs/background/derive.md +0 -179
- package/docs/cli/README.md +0 -379
- package/docs/concepts/ai-guide.md +0 -132
- package/docs/concepts/config.md +0 -416
- package/docs/concepts/decorators.md +0 -127
- package/docs/concepts/security.md +0 -108
- package/docs/concepts/tiers.md +0 -166
- package/docs/concepts/types.md +0 -216
- package/docs/database/README.md +0 -143
- package/docs/database/capacity.md +0 -350
- package/docs/database/counters.md +0 -174
- package/docs/database/documents.md +0 -277
- package/docs/database/events.md +0 -307
- package/docs/database/membership.md +0 -246
- package/docs/database/setup.md +0 -155
- package/docs/database/unique.md +0 -216
- package/docs/database/views.md +0 -246
- package/docs/frontend/README.md +0 -104
- package/docs/frontend/components.md +0 -286
- package/docs/frontend/data-fetching.md +0 -260
- package/docs/frontend/images.md +0 -148
- package/docs/frontend/metadata.md +0 -344
- package/docs/frontend/navigation.md +0 -296
- package/docs/frontend/rendering.md +0 -236
- package/docs/frontend/routing.md +0 -346
- package/docs/frontend/scripts.md +0 -118
- package/docs/frontend/search.md +0 -191
- package/docs/frontend/styling.md +0 -147
- package/docs/frontend/toil-global.md +0 -216
- package/docs/getting-started/README.md +0 -81
- package/docs/getting-started/create-project.md +0 -131
- package/docs/getting-started/deploy.md +0 -101
- package/docs/getting-started/first-app.md +0 -215
- package/docs/getting-started/installation.md +0 -124
- package/docs/getting-started/migrating.md +0 -125
- package/docs/getting-started/project-structure.md +0 -163
- package/docs/introduction/README.md +0 -66
- package/docs/introduction/design-principles.md +0 -81
- package/docs/introduction/distributed.md +0 -91
- package/docs/introduction/how-it-works.md +0 -121
- package/docs/introduction/hyperscale.md +0 -70
- package/docs/introduction/modern-stack.md +0 -85
- package/docs/introduction/vs-other-frameworks.md +0 -64
- package/docs/introduction/why-toil.md +0 -99
- package/docs/llms.txt +0 -95
- package/docs/realtime/README.md +0 -102
- package/docs/realtime/channels.md +0 -211
- package/docs/realtime/streams.md +0 -369
- package/docs/services/README.md +0 -60
- package/docs/services/analytics.md +0 -279
- package/docs/services/caching.md +0 -175
- package/docs/services/cookies.md +0 -235
- package/docs/services/crypto.md +0 -209
- package/docs/services/email.md +0 -289
- package/docs/services/environment.md +0 -141
- package/docs/services/ratelimit.md +0 -174
- package/docs/services/time.md +0 -85
- package/eslint.config.js +0 -57
- package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
- package/presets/eslint.js +0 -88
- package/src/backend/index.ts +0 -204
- package/src/cli/configure.ts +0 -363
- package/src/cli/create.ts +0 -924
- package/src/cli/db.ts +0 -158
- package/src/cli/diagnostics.ts +0 -665
- package/src/cli/doctor.ts +0 -982
- package/src/cli/features.ts +0 -170
- package/src/cli/index.ts +0 -326
- package/src/cli/notify.ts +0 -120
- package/src/cli/proc.ts +0 -56
- package/src/cli/ui.ts +0 -169
- package/src/cli/update.ts +0 -248
- package/src/cli/updates.ts +0 -92
- package/src/cli/validate.ts +0 -31
- package/src/cli/version-check.ts +0 -125
- package/src/client/auth.ts +0 -869
- package/src/client/channel/channel.ts +0 -146
- package/src/client/components/Form.tsx +0 -65
- package/src/client/components/Image.tsx +0 -160
- package/src/client/components/Script.tsx +0 -113
- package/src/client/components/Slot.tsx +0 -21
- package/src/client/dev/devtools.tsx +0 -1089
- package/src/client/dev/error-overlay.tsx +0 -225
- package/src/client/errors.ts +0 -11
- package/src/client/head/head.ts +0 -182
- package/src/client/head/metadata.ts +0 -110
- package/src/client/index.ts +0 -130
- package/src/client/navigation/Link.tsx +0 -108
- package/src/client/navigation/NavLink.tsx +0 -86
- package/src/client/navigation/navigation.ts +0 -235
- package/src/client/navigation/prefetch.ts +0 -169
- package/src/client/navigation/scroll.ts +0 -53
- package/src/client/routing/Router.tsx +0 -216
- package/src/client/routing/action.ts +0 -122
- package/src/client/routing/error-boundary.tsx +0 -43
- package/src/client/routing/hooks.ts +0 -140
- package/src/client/routing/lazy.ts +0 -77
- package/src/client/routing/loader.ts +0 -381
- package/src/client/routing/match.ts +0 -47
- package/src/client/routing/mount.tsx +0 -90
- package/src/client/routing/params-context.ts +0 -10
- package/src/client/routing/slot-context.ts +0 -7
- package/src/client/rpc.ts +0 -97
- package/src/client/search/search.ts +0 -189
- package/src/client/search/use-page-search.ts +0 -73
- package/src/client/ssr/markers.tsx +0 -172
- package/src/client/stream/client.ts +0 -237
- package/src/client/types.ts +0 -73
- package/src/compiler/auth-emails.ts +0 -312
- package/src/compiler/config.ts +0 -369
- package/src/compiler/docs.ts +0 -127
- package/src/compiler/email-preview.ts +0 -314
- package/src/compiler/emails.ts +0 -404
- package/src/compiler/fonts.ts +0 -89
- package/src/compiler/generate.ts +0 -471
- package/src/compiler/image-blur.ts +0 -42
- package/src/compiler/image-report.ts +0 -88
- package/src/compiler/index.ts +0 -1362
- package/src/compiler/pages.ts +0 -49
- package/src/compiler/plugin.ts +0 -391
- package/src/compiler/prerender.ts +0 -263
- package/src/compiler/routes.ts +0 -170
- package/src/compiler/seo.ts +0 -415
- package/src/compiler/sri.ts +0 -144
- package/src/compiler/ssg.ts +0 -177
- package/src/compiler/ssr-codegen.ts +0 -85
- package/src/compiler/template-build.ts +0 -676
- package/src/compiler/template.ts +0 -262
- package/src/compiler/toil-docs.generated.ts +0 -77
- package/src/compiler/vite.ts +0 -245
- package/src/devserver/analytics/index.ts +0 -317
- package/src/devserver/config/dotenv.ts +0 -102
- package/src/devserver/config/env.ts +0 -30
- package/src/devserver/config/ratelimit.ts +0 -123
- package/src/devserver/daemon/catalog.ts +0 -120
- package/src/devserver/daemon/cron.ts +0 -87
- package/src/devserver/daemon/host.ts +0 -134
- package/src/devserver/daemon/index.ts +0 -369
- package/src/devserver/daemon/runtime.ts +0 -48
- package/src/devserver/db/catalog.ts +0 -109
- package/src/devserver/db/database.ts +0 -1834
- package/src/devserver/db/derives.ts +0 -121
- package/src/devserver/db/index.ts +0 -20
- package/src/devserver/db/routeKinds.ts +0 -191
- package/src/devserver/db/types.ts +0 -168
- package/src/devserver/email/caps.ts +0 -0
- package/src/devserver/email/config.ts +0 -141
- package/src/devserver/email/console.ts +0 -348
- package/src/devserver/email/index.ts +0 -155
- package/src/devserver/email/providers.ts +0 -131
- package/src/devserver/email/status.ts +0 -23
- package/src/devserver/email/validate.ts +0 -37
- package/src/devserver/email/wire.ts +0 -55
- package/src/devserver/http/cache.ts +0 -0
- package/src/devserver/http/envelope.ts +0 -157
- package/src/devserver/http/proxy.ts +0 -172
- package/src/devserver/http/runtime.ts +0 -387
- package/src/devserver/index.ts +0 -43
- package/src/devserver/production-ipc.ts +0 -63
- package/src/devserver/production-worker.ts +0 -83
- package/src/devserver/production.ts +0 -718
- package/src/devserver/runtime/crypto.ts +0 -587
- package/src/devserver/runtime/host.ts +0 -427
- package/src/devserver/runtime/module.ts +0 -442
- package/src/devserver/server.ts +0 -251
- package/src/devserver/ssr.ts +0 -208
- package/src/devserver/stream/catalog.ts +0 -106
- package/src/devserver/stream/host.ts +0 -42
- package/src/devserver/stream/index.ts +0 -301
- package/src/devserver/stream/manager.ts +0 -167
- package/src/devserver/stream/router.ts +0 -101
- package/src/devserver/stream/wire.ts +0 -58
- package/src/devserver/stream/ws.ts +0 -78
- package/src/devserver/wasm/sections.ts +0 -59
- package/src/devserver/wasm/surface.ts +0 -115
- package/src/io/FastMap.ts +0 -151
- package/src/io/FastSet.ts +0 -113
- package/src/io/codec.ts +0 -362
- package/src/io/index.ts +0 -10
- package/src/io/lengths.ts +0 -14
- package/src/io/types.ts +0 -19
- package/src/logger/index.ts +0 -22
- package/src/shared/index.ts +0 -46
- package/test/analytics-dev.test.ts +0 -113
- package/test/assembly/aspect-shim.ts +0 -33
- package/test/assembly/cookie.spec.ts +0 -303
- package/test/assembly/example.spec.ts +0 -22
- package/test/assembly/ssr.spec.ts +0 -95
- package/test/assembly.test.ts +0 -59
- package/test/auth-emails.test.ts +0 -118
- package/test/built-ssr.test.ts +0 -148
- package/test/channel.test.ts +0 -21
- package/test/config-host-port.test.ts +0 -59
- package/test/configure.test.ts +0 -94
- package/test/daemon-build.test.ts +0 -278
- package/test/daemon-catalog.test.ts +0 -349
- package/test/daemon-emulation.test.ts +0 -256
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +0 -1102
- package/test/devserver-email.test.ts +0 -306
- package/test/devserver-pqauth.test.ts +0 -93
- package/test/devserver-secrets.test.ts +0 -63
- package/test/devserver.test.ts +0 -259
- package/test/doctor.test.ts +0 -298
- package/test/dom/Image.test.tsx +0 -144
- package/test/dom/Link.test.tsx +0 -67
- package/test/dom/NavLink.test.tsx +0 -37
- package/test/dom/Script.test.tsx +0 -48
- package/test/dom/action.test.tsx +0 -146
- package/test/dom/error-overlay.test.tsx +0 -44
- package/test/dom/loader.test.tsx +0 -121
- package/test/dom/navigation.test.ts +0 -59
- package/test/dom/revalidate.test.tsx +0 -38
- package/test/dom/route-head.test.tsx +0 -56
- package/test/dom/router-loading.test.tsx +0 -44
- package/test/dom/scroll.test.ts +0 -56
- package/test/dom/slot.test.tsx +0 -131
- package/test/dom/use-metadata.test.tsx +0 -58
- package/test/dom/view-transitions.test.tsx +0 -53
- package/test/dynamic-routing.test.ts +0 -132
- package/test/email-console.test.ts +0 -86
- package/test/email-preview.test.ts +0 -73
- package/test/emails.test.ts +0 -58
- package/test/errors.test.ts +0 -21
- package/test/example-guestbook.test.ts +0 -123
- package/test/features.test.ts +0 -149
- package/test/fixtures/bignum-wire/spec.ts +0 -25
- package/test/fixtures/daemon-app.ts +0 -65
- package/test/fixtures/stream-echo.ts +0 -26
- package/test/fixtures/stream-gate.ts +0 -28
- package/test/fixtures/stream-trap.ts +0 -18
- package/test/fixtures/stream-typed.ts +0 -41
- package/test/fonts.test.ts +0 -28
- package/test/global-setup.ts +0 -17
- package/test/head.test.ts +0 -42
- package/test/image-blur.test.ts +0 -50
- package/test/io.test.ts +0 -117
- package/test/metadata.test.ts +0 -40
- package/test/navlink.test.ts +0 -28
- package/test/pages.test.ts +0 -105
- package/test/placeholder.test.ts +0 -9
- package/test/pqauth-e2e.test.ts +0 -216
- package/test/pqauth-email.test.ts +0 -801
- package/test/prerender.test.ts +0 -116
- package/test/prettier-plugin.test.ts +0 -46
- package/test/routes.test.ts +0 -76
- package/test/rpc-bignum-wire.test.ts +0 -164
- package/test/rpc-dispatch.test.ts +0 -132
- package/test/rpc-kinds.test.ts +0 -18
- package/test/rpc.test.ts +0 -66
- package/test/search.test.ts +0 -114
- package/test/seo.test.ts +0 -175
- package/test/slot-layouts.test.ts +0 -69
- package/test/sri.test.ts +0 -129
- package/test/ssg.test.ts +0 -36
- package/test/ssr-hydration.test.tsx +0 -122
- package/test/ssr-render.test.ts +0 -197
- package/test/ssr-template.test.tsx +0 -482
- package/test/stream-emulation.test.ts +0 -479
- package/test/ui.test.ts +0 -40
- package/test/update.test.ts +0 -58
- package/test/validate.test.ts +0 -42
- package/test/version-check.test.ts +0 -130
- package/tests/data-parity/generated-parity.ts +0 -99
- package/tests/data-parity/parity.ts +0 -80
- package/tests/data-parity/spec.ts +0 -46
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { act, cleanup, render } from '@testing-library/react';
|
|
3
|
-
import { afterEach, beforeEach, describe, it } from 'vitest';
|
|
4
|
-
|
|
5
|
-
import { Router } from '../../src/client/routing/Router';
|
|
6
|
-
import { clearLoaderData, revalidate, useLoaderData } from '../../src/client/routing/loader';
|
|
7
|
-
import type { RouteDef } from '../../src/client/types';
|
|
8
|
-
|
|
9
|
-
afterEach(cleanup);
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
clearLoaderData();
|
|
12
|
-
window.history.replaceState({}, '', '/');
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
describe('revalidate refetches', () => {
|
|
16
|
-
it('re-runs the loader and updates the rendered data', async () => {
|
|
17
|
-
let n = 0;
|
|
18
|
-
function Page(): React.ReactNode {
|
|
19
|
-
const value = useLoaderData<number>();
|
|
20
|
-
return <p>val:{String(value)}</p>;
|
|
21
|
-
}
|
|
22
|
-
const routes: RouteDef[] = [
|
|
23
|
-
{
|
|
24
|
-
pattern: '/',
|
|
25
|
-
load: () => Promise.resolve({ default: Page, loader: () => (n += 1) }),
|
|
26
|
-
// matches the example: this route has a loading.tsx (keyed boundary + transition).
|
|
27
|
-
loading: () => Promise.resolve({ default: () => <p>loading</p> }),
|
|
28
|
-
},
|
|
29
|
-
];
|
|
30
|
-
const { findByText } = render(<Router routes={routes} />);
|
|
31
|
-
await findByText('val:1');
|
|
32
|
-
|
|
33
|
-
act(() => {
|
|
34
|
-
revalidate();
|
|
35
|
-
});
|
|
36
|
-
await findByText('val:2');
|
|
37
|
-
});
|
|
38
|
-
});
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { setRouteHead, useHead } from '../../src/client/head/head';
|
|
5
|
-
import { resolveMetadata } from '../../src/client/head/metadata';
|
|
6
|
-
import { cleanup, render } from '@testing-library/react';
|
|
7
|
-
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
cleanup();
|
|
10
|
-
setRouteHead(null);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const desc = (): string | null =>
|
|
14
|
-
document.head.querySelector('meta[name="description"]')?.getAttribute('content') ?? null;
|
|
15
|
-
|
|
16
|
-
describe('route head (metadata baseline)', () => {
|
|
17
|
-
it('applies a resolved metadata head to the document', () => {
|
|
18
|
-
setRouteHead(resolveMetadata({ title: 'About', description: 'about page' }));
|
|
19
|
-
expect(document.title).toBe('About');
|
|
20
|
-
expect(desc()).toBe('about page');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("wins over a layout's useHead/<Head> defaults for the keys it sets", () => {
|
|
24
|
-
// A layout default title + a route's metadata title: the route metadata should win.
|
|
25
|
-
function LayoutDefaults() {
|
|
26
|
-
useHead({ title: 'Site Default', meta: [{ name: 'description', content: 'site' }] });
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
render(<LayoutDefaults />);
|
|
30
|
-
setRouteHead(resolveMetadata({ title: 'useReducer', description: 'route desc' }));
|
|
31
|
-
expect(document.title).toBe('useReducer');
|
|
32
|
-
expect(desc()).toBe('route desc');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// Regression for the "metadata title doesn't update" report: a real layout (title default)
|
|
36
|
-
// plus a route's full `metadata` (the exact shape users write) must land on the route's title,
|
|
37
|
-
// with the route's og:title applied too.
|
|
38
|
-
it('applies a full route metadata over a layout title default', () => {
|
|
39
|
-
function LayoutDefaults() {
|
|
40
|
-
useHead({ title: 'ToilJS' });
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
render(<LayoutDefaults />);
|
|
44
|
-
setRouteHead(
|
|
45
|
-
resolveMetadata({
|
|
46
|
-
title: 'useReducer | React Hooks',
|
|
47
|
-
description: 'Manage complex state with a reducer.',
|
|
48
|
-
openGraph: { title: 'useReducer | React Hooks', type: 'website' },
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
51
|
-
expect(document.title).toBe('useReducer | React Hooks');
|
|
52
|
-
expect(
|
|
53
|
-
document.head.querySelector('meta[property="og:title"]')?.getAttribute('content'),
|
|
54
|
-
).toBe('useReducer | React Hooks');
|
|
55
|
-
});
|
|
56
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { act, cleanup, render, waitFor } from '@testing-library/react';
|
|
3
|
-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
4
|
-
|
|
5
|
-
import { navigate } from '../../src/client/navigation/navigation';
|
|
6
|
-
import { Router } from '../../src/client/routing/Router';
|
|
7
|
-
import type { RouteDef } from '../../src/client/types';
|
|
8
|
-
|
|
9
|
-
afterEach(cleanup);
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
window.history.replaceState({}, '', '/');
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
const routes: RouteDef[] = [
|
|
15
|
-
{
|
|
16
|
-
pattern: '/',
|
|
17
|
-
load: () => Promise.resolve({ default: () => <div>HOME</div> }),
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
// Page chunk never resolves, so the route stays suspended, exercising the fallback path.
|
|
21
|
-
pattern: '/slow',
|
|
22
|
-
load: () => new Promise<{ default: () => null }>(() => undefined),
|
|
23
|
-
loading: () => Promise.resolve({ default: () => <div>LOADING</div> }),
|
|
24
|
-
},
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
describe('Router loading fallback', () => {
|
|
28
|
-
it("shows the route's loading.tsx immediately when navigating to a suspending route", async () => {
|
|
29
|
-
const { findByText, queryByText } = render(<Router routes={routes} />);
|
|
30
|
-
await findByText('HOME');
|
|
31
|
-
|
|
32
|
-
// A route with a `loading.tsx` keys its Suspense boundary per URL, so even though navigation
|
|
33
|
-
// runs in a transition the fallback appears immediately (it isn't suppressed / frozen).
|
|
34
|
-
act(() => {
|
|
35
|
-
navigate('/slow');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
await waitFor(() => {
|
|
39
|
-
expect(queryByText('LOADING')).not.toBeNull();
|
|
40
|
-
});
|
|
41
|
-
// The keyed boundary remounts for the new route, so the previous page is gone (not frozen).
|
|
42
|
-
expect(queryByText('HOME')).toBeNull();
|
|
43
|
-
});
|
|
44
|
-
});
|
package/test/dom/scroll.test.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
applyScroll,
|
|
6
|
-
enableManualScrollRestoration,
|
|
7
|
-
planScroll,
|
|
8
|
-
rememberScroll,
|
|
9
|
-
} from '../../src/client/navigation/scroll';
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
window.scrollTo = vi.fn();
|
|
13
|
-
Object.defineProperty(window, 'scrollY', { value: 0, configurable: true });
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe('scroll management', () => {
|
|
17
|
-
it('enables manual scroll restoration when the browser supports it', () => {
|
|
18
|
-
// jsdom doesn't implement scrollRestoration; define it so the guarded branch runs.
|
|
19
|
-
(window.history as History & { scrollRestoration: ScrollRestoration }).scrollRestoration =
|
|
20
|
-
'auto';
|
|
21
|
-
enableManualScrollRestoration();
|
|
22
|
-
expect(window.history.scrollRestoration).toBe('manual');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('scrolls to top', () => {
|
|
26
|
-
planScroll({ hash: '', toTop: true });
|
|
27
|
-
applyScroll();
|
|
28
|
-
expect(window.scrollTo).toHaveBeenCalledWith(0, 0);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('restores a saved position', () => {
|
|
32
|
-
Object.defineProperty(window, 'scrollY', { value: 250, configurable: true });
|
|
33
|
-
rememberScroll('k1');
|
|
34
|
-
planScroll({ restoreKey: 'k1', hash: '', toTop: false });
|
|
35
|
-
applyScroll();
|
|
36
|
-
expect(window.scrollTo).toHaveBeenCalledWith(0, 250);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('scrolls to a #hash element', () => {
|
|
40
|
-
const el = document.createElement('div');
|
|
41
|
-
el.id = 'sec';
|
|
42
|
-
el.scrollIntoView = vi.fn();
|
|
43
|
-
document.body.appendChild(el);
|
|
44
|
-
planScroll({ hash: '#sec', toTop: false });
|
|
45
|
-
applyScroll();
|
|
46
|
-
expect(el.scrollIntoView).toHaveBeenCalled();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('consumes the plan (second applyScroll is a no-op)', () => {
|
|
50
|
-
planScroll({ hash: '', toTop: true });
|
|
51
|
-
applyScroll();
|
|
52
|
-
(window.scrollTo as ReturnType<typeof vi.fn>).mockClear();
|
|
53
|
-
applyScroll();
|
|
54
|
-
expect(window.scrollTo).not.toHaveBeenCalled();
|
|
55
|
-
});
|
|
56
|
-
});
|
package/test/dom/slot.test.tsx
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { act, cleanup, render } from '@testing-library/react';
|
|
3
|
-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
4
|
-
import type { ReactNode } from 'react';
|
|
5
|
-
|
|
6
|
-
import { Slot } from '../../src/client/components/Slot';
|
|
7
|
-
import { navigate } from '../../src/client/navigation/navigation';
|
|
8
|
-
import { Router } from '../../src/client/routing/Router';
|
|
9
|
-
import { clearLoaderData } from '../../src/client/routing/loader';
|
|
10
|
-
import { SlotContext } from '../../src/client/routing/slot-context';
|
|
11
|
-
import type { RouteDef } from '../../src/client/types';
|
|
12
|
-
|
|
13
|
-
const layoutWithModal = () =>
|
|
14
|
-
Promise.resolve({
|
|
15
|
-
default: ({ children }: { children?: ReactNode }) => (
|
|
16
|
-
<div>
|
|
17
|
-
{children}
|
|
18
|
-
<Slot name="modal" />
|
|
19
|
-
</div>
|
|
20
|
-
),
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
afterEach(cleanup);
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
clearLoaderData();
|
|
26
|
-
window.history.replaceState({}, '', '/');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe('Slot', () => {
|
|
30
|
-
it('renders the named slot element from context, or the fallback', () => {
|
|
31
|
-
const { getByText, queryByText } = render(
|
|
32
|
-
<SlotContext.Provider value={{ modal: <span>MODAL</span> }}>
|
|
33
|
-
<Slot name="modal" />
|
|
34
|
-
<Slot
|
|
35
|
-
name="missing"
|
|
36
|
-
fallback={<span>FB</span>}
|
|
37
|
-
/>
|
|
38
|
-
</SlotContext.Provider>,
|
|
39
|
-
);
|
|
40
|
-
expect(getByText('MODAL')).toBeTruthy();
|
|
41
|
-
expect(getByText('FB')).toBeTruthy();
|
|
42
|
-
expect(queryByText('missing')).toBeNull();
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe('Router parallel slots', () => {
|
|
47
|
-
const routes: RouteDef[] = [
|
|
48
|
-
{ pattern: '/', load: () => Promise.resolve({ default: () => <main>PAGE</main> }) },
|
|
49
|
-
];
|
|
50
|
-
// A layout that renders the page plus the "modal" slot.
|
|
51
|
-
const layout = () =>
|
|
52
|
-
Promise.resolve({
|
|
53
|
-
default: ({ children }: { children?: ReactNode }) => (
|
|
54
|
-
<div>
|
|
55
|
-
{children}
|
|
56
|
-
<Slot name="modal" />
|
|
57
|
-
</div>
|
|
58
|
-
),
|
|
59
|
-
});
|
|
60
|
-
const slots: Record<string, RouteDef[]> = {
|
|
61
|
-
modal: [
|
|
62
|
-
{ pattern: '/', load: () => Promise.resolve({ default: () => <aside>SLOT</aside> }) },
|
|
63
|
-
],
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
it('renders the main route and a matching slot together', async () => {
|
|
67
|
-
const { findByText } = render(
|
|
68
|
-
<Router
|
|
69
|
-
routes={routes}
|
|
70
|
-
layout={layout}
|
|
71
|
-
slots={slots}
|
|
72
|
-
/>,
|
|
73
|
-
);
|
|
74
|
-
// Both the page and the parallel slot render for the same URL.
|
|
75
|
-
await findByText('PAGE');
|
|
76
|
-
await findByText('SLOT');
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
describe('intercepting routes', () => {
|
|
81
|
-
const routes: RouteDef[] = [
|
|
82
|
-
{
|
|
83
|
-
pattern: '/photo/:id',
|
|
84
|
-
load: () => Promise.resolve({ default: () => <main>PHOTO PAGE</main> }),
|
|
85
|
-
},
|
|
86
|
-
{ pattern: '/', load: () => Promise.resolve({ default: () => <main>FEED</main> }) },
|
|
87
|
-
];
|
|
88
|
-
const slots: Record<string, RouteDef[]> = {
|
|
89
|
-
modal: [
|
|
90
|
-
{
|
|
91
|
-
pattern: '/photo/:id',
|
|
92
|
-
intercept: true,
|
|
93
|
-
load: () => Promise.resolve({ default: () => <aside>PHOTO MODAL</aside> }),
|
|
94
|
-
},
|
|
95
|
-
],
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// This test must run before any navigation so it observes a "hard" load (soft-nav state is false).
|
|
99
|
-
it('shows the full page on a hard load (no interception)', async () => {
|
|
100
|
-
window.history.replaceState({}, '', '/photo/1');
|
|
101
|
-
const { findByText, queryByText } = render(
|
|
102
|
-
<Router
|
|
103
|
-
routes={routes}
|
|
104
|
-
layout={layoutWithModal}
|
|
105
|
-
slots={slots}
|
|
106
|
-
/>,
|
|
107
|
-
);
|
|
108
|
-
await findByText('PHOTO PAGE');
|
|
109
|
-
expect(queryByText('PHOTO MODAL')).toBeNull();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('intercepts on soft navigation: modal overlays, previous page stays', async () => {
|
|
113
|
-
window.history.replaceState({}, '', '/');
|
|
114
|
-
const { findByText, queryByText } = render(
|
|
115
|
-
<Router
|
|
116
|
-
routes={routes}
|
|
117
|
-
layout={layoutWithModal}
|
|
118
|
-
slots={slots}
|
|
119
|
-
/>,
|
|
120
|
-
);
|
|
121
|
-
await findByText('FEED');
|
|
122
|
-
act(() => {
|
|
123
|
-
navigate('/photo/1');
|
|
124
|
-
});
|
|
125
|
-
// The intercepting slot route shows the modal…
|
|
126
|
-
await findByText('PHOTO MODAL');
|
|
127
|
-
// …while the main view keeps the previous page (the backdrop), not the full photo page.
|
|
128
|
-
expect(queryByText('FEED')).not.toBeNull();
|
|
129
|
-
expect(queryByText('PHOTO PAGE')).toBeNull();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
3
|
-
import { cleanup, render } from '@testing-library/react';
|
|
4
|
-
|
|
5
|
-
import { Metadata, useMetadata } from '../../src/client/head/metadata';
|
|
6
|
-
import { setRouteHead } from '../../src/client/head/head';
|
|
7
|
-
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
cleanup();
|
|
10
|
-
setRouteHead(null);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const meta = (key: string): string | null =>
|
|
14
|
-
document.head.querySelector(`meta[${key}]`)?.getAttribute('content') ?? null;
|
|
15
|
-
|
|
16
|
-
describe('useMetadata / <Metadata>', () => {
|
|
17
|
-
it('applies a full Metadata object to the document head from a component', () => {
|
|
18
|
-
function Article() {
|
|
19
|
-
useMetadata({
|
|
20
|
-
title: 'Article',
|
|
21
|
-
description: 'an article',
|
|
22
|
-
openGraph: { title: 'og title', type: 'website' },
|
|
23
|
-
});
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
render(<Article />);
|
|
27
|
-
expect(document.title).toBe('Article');
|
|
28
|
-
expect(meta('name="description"')).toBe('an article');
|
|
29
|
-
expect(meta('property="og:title"')).toBe('og title');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('reverts the head when the component unmounts', () => {
|
|
33
|
-
function Article() {
|
|
34
|
-
useMetadata({ title: 'Temp' });
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
const { unmount } = render(<Article />);
|
|
38
|
-
expect(document.title).toBe('Temp');
|
|
39
|
-
unmount();
|
|
40
|
-
expect(document.title).not.toBe('Temp');
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('the declarative <Metadata> form applies too', () => {
|
|
44
|
-
render(<Metadata title="Declarative" />);
|
|
45
|
-
expect(document.title).toBe('Declarative');
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("a route's metadata still wins over a component's useMetadata", () => {
|
|
49
|
-
function Article() {
|
|
50
|
-
useMetadata({ title: 'Component' });
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
render(<Article />);
|
|
54
|
-
// The route baseline (applied last) takes precedence for keys it sets.
|
|
55
|
-
setRouteHead({ title: 'Route' });
|
|
56
|
-
expect(document.title).toBe('Route');
|
|
57
|
-
});
|
|
58
|
-
});
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import { navigate, setViewTransitions } from '../../src/client/navigation/navigation';
|
|
5
|
-
|
|
6
|
-
interface VTDoc {
|
|
7
|
-
startViewTransition?: (cb: () => void) => unknown;
|
|
8
|
-
}
|
|
9
|
-
const doc = document as Document & VTDoc;
|
|
10
|
-
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
setViewTransitions(false);
|
|
13
|
-
delete doc.startViewTransition;
|
|
14
|
-
vi.restoreAllMocks();
|
|
15
|
-
window.history.replaceState({}, '', '/');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('view transitions', () => {
|
|
19
|
-
function stubReducedMotion(matches: boolean): void {
|
|
20
|
-
window.matchMedia = vi
|
|
21
|
-
.fn()
|
|
22
|
-
.mockReturnValue({ matches }) as unknown as typeof window.matchMedia;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
it('wraps navigation in startViewTransition when enabled and supported', () => {
|
|
26
|
-
const vt = vi.fn((cb: () => void) => {
|
|
27
|
-
cb();
|
|
28
|
-
});
|
|
29
|
-
doc.startViewTransition = vt;
|
|
30
|
-
stubReducedMotion(false);
|
|
31
|
-
setViewTransitions(true);
|
|
32
|
-
navigate('/a');
|
|
33
|
-
expect(vt).toHaveBeenCalledOnce();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('skips the view transition under prefers-reduced-motion', () => {
|
|
37
|
-
const vt = vi.fn();
|
|
38
|
-
doc.startViewTransition = vt;
|
|
39
|
-
stubReducedMotion(true);
|
|
40
|
-
setViewTransitions(true);
|
|
41
|
-
navigate('/b');
|
|
42
|
-
expect(vt).not.toHaveBeenCalled();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('does not use view transitions when disabled', () => {
|
|
46
|
-
const vt = vi.fn();
|
|
47
|
-
doc.startViewTransition = vt;
|
|
48
|
-
stubReducedMotion(false);
|
|
49
|
-
setViewTransitions(false);
|
|
50
|
-
navigate('/c');
|
|
51
|
-
expect(vt).not.toHaveBeenCalled();
|
|
52
|
-
});
|
|
53
|
-
});
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import os from 'node:os';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
import ts from 'typescript';
|
|
6
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
7
|
-
|
|
8
|
-
import { exportsTrue } from '../src/compiler/prerender';
|
|
9
|
-
import { patternToBracketFile, staticSectionPattern } from '../src/compiler/routes';
|
|
10
|
-
import { resolveDynamicFile } from '../src/devserver/http/runtime';
|
|
11
|
-
|
|
12
|
-
describe('patternToBracketFile', () => {
|
|
13
|
-
it('inverts URL patterns back to on-disk bracket filenames', () => {
|
|
14
|
-
expect(patternToBracketFile('/blog/:id')).toBe('blog/[id].html');
|
|
15
|
-
expect(patternToBracketFile('/docs/*slug')).toBe('docs/[...slug].html');
|
|
16
|
-
expect(patternToBracketFile('/docs/**slug')).toBe('docs/[[...slug]].html');
|
|
17
|
-
expect(patternToBracketFile('/:category/:id')).toBe('[category]/[id].html');
|
|
18
|
-
expect(patternToBracketFile('/gallery/photo/:id')).toBe('gallery/photo/[id].html');
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('staticSectionPattern', () => {
|
|
23
|
-
it('strips dynamic segments to the static section (for the template canonical)', () => {
|
|
24
|
-
expect(staticSectionPattern('/blog/:id')).toBe('/blog');
|
|
25
|
-
expect(staticSectionPattern('/docs/*slug')).toBe('/docs');
|
|
26
|
-
expect(staticSectionPattern('/:category/:id')).toBe('/');
|
|
27
|
-
expect(staticSectionPattern('/gallery/photo/:id')).toBe('/gallery/photo');
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe('exportsTrue (edge-SSR opt-in detection)', () => {
|
|
32
|
-
const files: string[] = [];
|
|
33
|
-
function tmp(source: string): string {
|
|
34
|
-
const file = path.join(os.tmpdir(), `toil-ssrflag-${String(files.length)}-${process.pid}.tsx`);
|
|
35
|
-
fs.writeFileSync(file, source);
|
|
36
|
-
files.push(file);
|
|
37
|
-
return file;
|
|
38
|
-
}
|
|
39
|
-
afterEach(() => {
|
|
40
|
-
for (const f of files.splice(0)) fs.rmSync(f, { force: true });
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('detects `export const ssr = true`', () => {
|
|
44
|
-
expect(exportsTrue(ts, tmp(`export const ssr = true;\nexport default () => null;\n`), 'ssr')).toBe(
|
|
45
|
-
true,
|
|
46
|
-
);
|
|
47
|
-
});
|
|
48
|
-
it('is false for ssr=false, a non-literal, or an absent export', () => {
|
|
49
|
-
expect(exportsTrue(ts, tmp(`export const ssr = false;\n`), 'ssr')).toBe(false);
|
|
50
|
-
expect(exportsTrue(ts, tmp(`const ssr = true;\n`), 'ssr')).toBe(false); // not exported
|
|
51
|
-
expect(exportsTrue(ts, tmp(`export const other = true;\n`), 'ssr')).toBe(false);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('resolveDynamicFile (self-host bracket routing, mirrors the edge)', () => {
|
|
56
|
-
const roots: string[] = [];
|
|
57
|
-
function siteRoot(): string {
|
|
58
|
-
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'toil-dyn-'));
|
|
59
|
-
roots.push(root);
|
|
60
|
-
return root;
|
|
61
|
-
}
|
|
62
|
-
function write(root: string, rel: string, body = '<html></html>'): void {
|
|
63
|
-
const full = path.join(root, rel);
|
|
64
|
-
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
65
|
-
fs.writeFileSync(full, body);
|
|
66
|
-
}
|
|
67
|
-
afterEach(() => {
|
|
68
|
-
for (const d of roots.splice(0)) fs.rmSync(d, { recursive: true, force: true });
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('serves a single-segment [id].html for a matching URL, one segment only', () => {
|
|
72
|
-
const root = siteRoot();
|
|
73
|
-
write(root, 'blog/[id].html', 'blog-shell');
|
|
74
|
-
expect(resolveDynamicFile(root, '/blog/hello')).toBe(path.join(root, 'blog/[id].html'));
|
|
75
|
-
expect(resolveDynamicFile(root, '/blog/a/b')).toBeNull();
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('serves a required catch-all for 1+ segments but not zero', () => {
|
|
79
|
-
const root = siteRoot();
|
|
80
|
-
write(root, 'docs/[...slug].html', 'docs');
|
|
81
|
-
expect(resolveDynamicFile(root, '/docs/a')).toBe(path.join(root, 'docs/[...slug].html'));
|
|
82
|
-
expect(resolveDynamicFile(root, '/docs/a/b/c')).toBe(path.join(root, 'docs/[...slug].html'));
|
|
83
|
-
expect(resolveDynamicFile(root, '/docs')).toBeNull();
|
|
84
|
-
// Parity with the client router + edge: a literal `index.html` segment is a slug (matches
|
|
85
|
-
// the catch-all), while a bare `/docs/` is zero trailing segments (no required-catch-all match).
|
|
86
|
-
expect(resolveDynamicFile(root, '/docs/index.html')).toBe(
|
|
87
|
-
path.join(root, 'docs/[...slug].html'),
|
|
88
|
-
);
|
|
89
|
-
expect(resolveDynamicFile(root, '/docs/')).toBeNull();
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('serves an optional catch-all for 0+ segments', () => {
|
|
93
|
-
const root = siteRoot();
|
|
94
|
-
write(root, 'files/[[...slug]].html', 'files');
|
|
95
|
-
expect(resolveDynamicFile(root, '/files')).toBe(path.join(root, 'files/[[...slug]].html'));
|
|
96
|
-
expect(resolveDynamicFile(root, '/files/a/b')).toBe(path.join(root, 'files/[[...slug]].html'));
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('prefers single over catch-all for one segment, catch-all for deeper', () => {
|
|
100
|
-
const root = siteRoot();
|
|
101
|
-
write(root, 'shop/[id].html', 'one');
|
|
102
|
-
write(root, 'shop/[...rest].html', 'many');
|
|
103
|
-
expect(fs.readFileSync(resolveDynamicFile(root, '/shop/x') as string, 'utf8')).toBe('one');
|
|
104
|
-
expect(fs.readFileSync(resolveDynamicFile(root, '/shop/x/y') as string, 'utf8')).toBe('many');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('prefers a real static subdirectory over a dynamic sibling', () => {
|
|
108
|
-
const root = siteRoot();
|
|
109
|
-
write(root, 'blog/[id].html', 'template');
|
|
110
|
-
write(root, 'blog/about/deep/[id].html', 'deep');
|
|
111
|
-
// /blog/about/deep/7 descends the real dirs to the deep template, not blog/[id].html.
|
|
112
|
-
expect(resolveDynamicFile(root, '/blog/about/deep/7')).toBe(
|
|
113
|
-
path.join(root, 'blog/about/deep/[id].html'),
|
|
114
|
-
);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('resolves a nested bracket directory', () => {
|
|
118
|
-
const root = siteRoot();
|
|
119
|
-
write(root, '[category]/[id].html', 'cat');
|
|
120
|
-
expect(resolveDynamicFile(root, '/electronics/9')).toBe(
|
|
121
|
-
path.join(root, '[category]/[id].html'),
|
|
122
|
-
);
|
|
123
|
-
expect(resolveDynamicFile(root, '/electronics')).toBeNull();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('returns null when no bracket template exists', () => {
|
|
127
|
-
const root = siteRoot();
|
|
128
|
-
write(root, 'about/index.html', 'about');
|
|
129
|
-
expect(resolveDynamicFile(root, '/blog/foo')).toBeNull();
|
|
130
|
-
expect(resolveDynamicFile(root, '/x/y/z')).toBeNull();
|
|
131
|
-
});
|
|
132
|
-
});
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dev email emulator (src/devserver/email/console.ts): the ASCII-drawn box + the
|
|
3
|
-
* HTML->text conversion + action (link/code) extraction that make confirm/reset/2FA
|
|
4
|
-
* flows testable in `toiljs dev` without a real inbox.
|
|
5
|
-
*/
|
|
6
|
-
import { describe, expect, it } from 'vitest';
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
emailAction,
|
|
10
|
-
htmlToText,
|
|
11
|
-
parseColor,
|
|
12
|
-
renderEmailConsole,
|
|
13
|
-
renderHtmlBody,
|
|
14
|
-
} from '../src/devserver/email/console.js';
|
|
15
|
-
|
|
16
|
-
const confirmEmail = {
|
|
17
|
-
to: 'bob@gmail.com',
|
|
18
|
-
subject: 'Confirm your account',
|
|
19
|
-
purpose: 'verify',
|
|
20
|
-
body: 'Confirm your account by opening this link:\nhttp://localhost:3000/confirm#token=abc123\n',
|
|
21
|
-
html:
|
|
22
|
-
'<p>Confirm your account by clicking the link below:</p>' +
|
|
23
|
-
'<p><a href="http://localhost:3000/confirm#token=abc123">Confirm my account</a></p>',
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/** picocolors may or may not emit ANSI depending on TTY; strip it for assertions. */
|
|
27
|
-
const plain = (s: string): string => s.replace(/\[[0-9;]*m/g, '');
|
|
28
|
-
|
|
29
|
-
describe('dev email emulator', () => {
|
|
30
|
-
it('htmlToText: anchors become "text (url)", blocks become newlines, entities decode', () => {
|
|
31
|
-
const t = htmlToText('<p>Hi & welcome</p><p><a href="https://x.com/a?b=1">Open</a></p>');
|
|
32
|
-
expect(t).toContain('Hi & welcome');
|
|
33
|
-
expect(t).toContain('Open (https://x.com/a?b=1)');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('emailAction extracts the link from a confirm email', () => {
|
|
37
|
-
expect(emailAction(confirmEmail)).toEqual({
|
|
38
|
-
kind: 'link',
|
|
39
|
-
value: 'http://localhost:3000/confirm#token=abc123',
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('emailAction extracts a 2FA code when there is no link', () => {
|
|
44
|
-
const twofa = { to: 'x', subject: 's', purpose: '2fa', body: 'Your code is 481920.', html: '' };
|
|
45
|
-
expect(emailAction(twofa)).toEqual({ kind: 'code', value: '481920' });
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('emailAction falls back to the HTML body when the text body is empty', () => {
|
|
49
|
-
expect(emailAction({ ...confirmEmail, body: '' })?.value).toBe(
|
|
50
|
-
'http://localhost:3000/confirm#token=abc123',
|
|
51
|
-
);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('renderEmailConsole draws a box with recipient, subject, and the clickable link', () => {
|
|
55
|
-
const box = plain(renderEmailConsole(confirmEmail, 'not sent'));
|
|
56
|
-
expect(box).toContain('bob@gmail.com');
|
|
57
|
-
expect(box).toContain('Confirm your account');
|
|
58
|
-
expect(box).toContain('http://localhost:3000/confirm#token=abc123');
|
|
59
|
-
expect(box).toContain('┌');
|
|
60
|
-
expect(box).toContain('└');
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('parseColor: hex, short hex, rgb, and named colors', () => {
|
|
64
|
-
expect(parseColor('#ff0000')).toEqual([255, 0, 0]);
|
|
65
|
-
expect(parseColor('#0f0')).toEqual([0, 255, 0]);
|
|
66
|
-
expect(parseColor('rgb(10, 20, 30)')).toEqual([10, 20, 30]);
|
|
67
|
-
expect(parseColor('red')).toEqual([229, 57, 53]);
|
|
68
|
-
expect(parseColor('not-a-color')).toBeUndefined();
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('renderHtmlBody: keeps text + link, splits paragraphs, wraps to width', () => {
|
|
72
|
-
const lines = renderHtmlBody(
|
|
73
|
-
'<p>Hi <b>there</b> & welcome</p>' +
|
|
74
|
-
'<p><a href="https://x.com/a?b=1">Open</a></p>',
|
|
75
|
-
40,
|
|
76
|
-
);
|
|
77
|
-
const joined = lines.map((l) => l.replace(/\x1b\[[0-9;]*m/g, '')).join('\n');
|
|
78
|
-
expect(joined).toContain('Hi there & welcome');
|
|
79
|
-
expect(joined).toContain('Open');
|
|
80
|
-
expect(joined).toContain('(https://x.com/a?b=1)');
|
|
81
|
-
// a blank separator line sits between the two paragraphs
|
|
82
|
-
expect(lines).toContain('');
|
|
83
|
-
// no line exceeds the wrap width (visible chars; color is off under vitest)
|
|
84
|
-
for (const l of lines) expect(l.replace(/\x1b\[[0-9;]*m/g, '').length).toBeLessThanOrEqual(40);
|
|
85
|
-
});
|
|
86
|
-
});
|