kopular 0.6.1 → 0.8.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/LLM.md +50 -5
- package/README.md +53 -4
- package/package.json +11 -2
- package/src/router.js +21 -1
- package/src/router.ks +40 -1
- package/src/testing.js +166 -0
package/LLM.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
# Kopular — LLM reference
|
|
2
2
|
|
|
3
3
|
Complete reference for generating correct Kopular code. This is a spec, not a tutorial —
|
|
4
|
-
see `README.md` for narrative/rationale. Kopular is
|
|
4
|
+
see `README.md` for narrative/rationale. Kopular is 8 files total; this covers all of
|
|
5
5
|
them. For the host language, see KopScript's own `LLM.md` in the `Kop` repo (or its
|
|
6
6
|
published `LLM.md` on the `kopscript` npm package) — that reference is a prerequisite,
|
|
7
7
|
not repeated here.
|
|
8
8
|
|
|
9
9
|
Published as npm `kopular`. Entry points: `kopular` / `kopular/component` (Component),
|
|
10
10
|
`kopular/router` (Router), `kopular/dom` (ambient DOM bindings), `kopular/directives`
|
|
11
|
-
(If), `kopular/http` (Http), `kopular/forms` (FormField, Validators)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
(If), `kopular/http` (Http), `kopular/forms` (FormField, Validators), `kopular/testing`
|
|
12
|
+
(runKopularApp, runKopularFixture — see below). Also ships a bin, `kp` — `npx kp new
|
|
13
|
+
<dir>` scaffolds a new project (the `extern` bindings below, plus the vendor/serve
|
|
14
|
+
scripts needed to run in a browser) rather than requiring it be reconstructed by hand;
|
|
15
|
+
prefer it over hand-writing the section below for a new project.
|
|
15
16
|
|
|
16
17
|
## Consuming Kopular from your own KopScript project
|
|
17
18
|
|
|
@@ -158,6 +159,21 @@ nav.Navigate("/about"); // pushState + immediate re-ren
|
|
|
158
159
|
- `Navigate(path)` calls `history.pushState` then re-renders immediately. A browser
|
|
159
160
|
back/forward triggers re-render via a `popstate` listener registered in the
|
|
160
161
|
constructor — `Navigate()` itself doesn't rely on that event.
|
|
162
|
+
- **Dynamic route segments**: a pattern segment written `:name` (e.g. `AddRoute("/dogs/:id",
|
|
163
|
+
...)`) matches any single non-empty path segment; every other segment must match
|
|
164
|
+
literally (same segment count required too — `/dogs` does NOT match `/dogs/:id`).
|
|
165
|
+
The captured value is `Router.Param` — a plain `string` field (**not** `state<T>`; no
|
|
166
|
+
`Subscribe()` needed — see below), `""` when the matched route has no `:` segment.
|
|
167
|
+
Just one dynamic segment per route in v1: no `/dogs/:id/toys/:toyId`, no wildcards, no
|
|
168
|
+
query string parsing.
|
|
169
|
+
- **Why `Param` is a plain field, not `state<T>`**: `Render()` already rebuilds a fresh
|
|
170
|
+
outlet and re-`Mount()`s the matched page on every `Navigate()`/`popstate`, which
|
|
171
|
+
re-runs that page's own `Render()` (reading the fresh `Param`) with no extra step.
|
|
172
|
+
Making it `state<T>` and having a page `Subscribe()` to it — the pattern every *other*
|
|
173
|
+
piece of state in this framework uses — actually crashes: `Match()` sets it *before*
|
|
174
|
+
`Render()` finishes swapping the matched page into the outlet, so the very first route
|
|
175
|
+
that matches a page nothing has `Mount()`ed yet fires that page's subscribed listener
|
|
176
|
+
while its inherited `Update()` still has no `ParentElement` to `replaceChild` into.
|
|
161
177
|
- **Every deployment target needs its own SPA/history-fallback config — this is
|
|
162
178
|
unavoidable, not a Kopular gap.** A direct load or refresh at `/about` is a plain HTTP
|
|
163
179
|
request that reaches your host *before* any JS (Router included) has run, so no
|
|
@@ -270,6 +286,35 @@ function, not a list.
|
|
|
270
286
|
call in your own `Render()`, the same as any other event handler; there is no
|
|
271
287
|
`[(ngModel)]`-equivalent.
|
|
272
288
|
|
|
289
|
+
## `kopular/testing` (`testing.js`, hand-written JS, not compiled from `.ks`)
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
import { runKopularApp } from "kopular/testing";
|
|
293
|
+
|
|
294
|
+
const { window, cleanup } = await runKopularApp(srcDir, "app.ks", {
|
|
295
|
+
includeKopularPackage: true, // app consumes Kopular via `extern`, not relative `using`
|
|
296
|
+
extraFiles: ["http_runtime.js"], // non-.ks/.html siblings a `using` graph needs at runtime
|
|
297
|
+
fetchMock: (...args) => Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve("body") }),
|
|
298
|
+
url: "http://localhost/",
|
|
299
|
+
});
|
|
300
|
+
try {
|
|
301
|
+
window.document.querySelector(...)
|
|
302
|
+
} finally {
|
|
303
|
+
cleanup(); // always call, even on a thrown assertion — restores globalThis + deletes the temp dir
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Compiles `entryFileName` (plus everything else in `srcDir` it `using`s) via kopscript's
|
|
308
|
+
`compileGraph`, binds jsdom onto `globalThis` (`document`/`Element`/`Event`/`location`/
|
|
309
|
+
`history`/`window`/`fetch`) for the compiled ambient `extern` declarations to find, and
|
|
310
|
+
runs it. `runKopularFixture(source, options)` is the sibling export for an inline fixture
|
|
311
|
+
string instead of a real file (used by Kopular's own test suite; copies Kopular's *own*
|
|
312
|
+
`.ks` sources alongside the fixture, so `using "./component"` resolves — only meaningful
|
|
313
|
+
for testing Kopular itself, not an external consumer, which should use `runKopularApp`
|
|
314
|
+
with `includeKopularPackage: true` instead).
|
|
315
|
+
|
|
316
|
+
`jsdom` is an optional peer dependency — add it to your own project to use this.
|
|
317
|
+
|
|
273
318
|
## Dependency injection — no container, no decorators
|
|
274
319
|
|
|
275
320
|
There is no injector, no `@Injectable`, no provider tokens. "Injecting" a service is
|
package/README.md
CHANGED
|
@@ -50,11 +50,13 @@ Generating Kopular code with an AI coding assistant? Point it at **[`LLM.md`](./
|
|
|
50
50
|
is its one companion file — the single hand-written (not compiled from `.ks`) file in
|
|
51
51
|
Kopular, and why is explained in its own header comment.
|
|
52
52
|
- `src/forms.ks` — `FormField<T>` and `Validators` (see "Forms" below).
|
|
53
|
-
- `
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
- `src/testing.js` — `runKopularApp`/`runKopularFixture` (see "Testing your own app"
|
|
54
|
+
below); hand-written for the same reason as `http_runtime.js` — filesystem/process
|
|
55
|
+
orchestration isn't a Kopular `Component`.
|
|
56
|
+
- `bin/kp.mjs` — the `kp new` scaffolding CLI (see "Starting a new project" below); also
|
|
57
|
+
hand-written, same reason.
|
|
56
58
|
|
|
57
|
-
That's the whole framework —
|
|
59
|
+
That's the whole framework — eight files, plus the scaffolding CLI. Everything else (a
|
|
58
60
|
real app built on top of it)
|
|
59
61
|
lives in a separate consumer repo, [KopularDemo](https://dev.azure.com/koppinator/Koppindependence/_git/KopularDemo).
|
|
60
62
|
|
|
@@ -141,6 +143,22 @@ Real URLs via the History API (`pushState`/`popstate`), not `#/about` hash routi
|
|
|
141
143
|
`state<T>` survives navigating away and back — see "Dependency injection" above for how
|
|
142
144
|
the whole page graph typically gets built once, in a composition root.
|
|
143
145
|
|
|
146
|
+
**Dynamic route segments**: a path segment written `:name` (e.g. `/dogs/:id`) matches any
|
|
147
|
+
single non-empty segment, captured into `Router.Param`:
|
|
148
|
+
|
|
149
|
+
```ks
|
|
150
|
+
nav.AddRoute("/dogs/:id", new DogPage(nav));
|
|
151
|
+
// inside DogPage.Render():
|
|
152
|
+
el.textContent = "Dog #" + this.Nav.Param;
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`Param` is a plain `string`, deliberately not `state<T>` — Router's own `Render()`
|
|
156
|
+
already rebuilds a fresh outlet and re-`Mount()`s the matched page on every
|
|
157
|
+
`Navigate()`/`popstate`, which re-runs that page's `Render()` (reading the fresh `Param`)
|
|
158
|
+
with no extra step. No `Subscribe()` needed on it. Deliberately just one dynamic segment
|
|
159
|
+
per route for now — no multiple params (`/dogs/:id/toys/:toyId`), no wildcards, no query
|
|
160
|
+
string parsing — each a real, separate extension, not an oversight.
|
|
161
|
+
|
|
144
162
|
**Deploying a Router-based app needs SPA/history-fallback configured on whatever you
|
|
145
163
|
deploy to — this is true of every client-side router in every framework, not a Kopular
|
|
146
164
|
gap.** A direct load or a refresh at `/about` is a plain HTTP request that reaches your
|
|
@@ -382,6 +400,37 @@ described directly this way — see `LLM.md`'s `FormField<T>`/`Validators` secti
|
|
|
382
400
|
per-concrete-type workaround (the same trust-based, per-shape approach the "HTTP" section
|
|
383
401
|
above uses for typed JSON).
|
|
384
402
|
|
|
403
|
+
## Testing your own app: `kopular/testing`
|
|
404
|
+
|
|
405
|
+
A `Component`/`Router` graph can only be exercised end-to-end by actually compiling its
|
|
406
|
+
`.ks` sources and running the result against a real DOM — there's no way to unit test one
|
|
407
|
+
otherwise. Doing that by hand is a real ~50-line dance (a fresh temp dir per test, since
|
|
408
|
+
Node's ESM module cache means re-importing the same compiled path twice never re-runs an
|
|
409
|
+
ambient extern binding's top-level code — silently binding every later test to the first
|
|
410
|
+
test's jsdom instance — compiling via `kopscript`'s `compileGraph`, binding jsdom onto
|
|
411
|
+
`globalThis` for `document`/`Element`/... to find, then restoring it). Both Kopular's own
|
|
412
|
+
test suite and KopularDemo's used to hand-roll this independently; `kopular/testing` is
|
|
413
|
+
that dance, written once:
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
import { runKopularApp } from "kopular/testing";
|
|
417
|
+
|
|
418
|
+
const { window, cleanup } = await runKopularApp(join(__dirname, "..", "src"), "app.ks", {
|
|
419
|
+
includeKopularPackage: true, // your app consumes Kopular via `extern`, not relative `using`
|
|
420
|
+
});
|
|
421
|
+
try {
|
|
422
|
+
expect(window.document.querySelector("h1")?.textContent).toBe("Hello");
|
|
423
|
+
} finally {
|
|
424
|
+
cleanup(); // always — even on a thrown assertion — or the next test inherits these globals
|
|
425
|
+
}
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
`jsdom` is an **optional peer dependency** — installing `kopular` alone doesn't pull it
|
|
429
|
+
in; only a project that actually calls `runKopularApp` needs it added too. See
|
|
430
|
+
`Kopular/test/kopular.test.ts` (`runKopularFixture`, the sibling export used for testing
|
|
431
|
+
Kopular's own source against inline fixtures) and KopularDemo's
|
|
432
|
+
`test/routed_app.test.ts` for two real, different call sites.
|
|
433
|
+
|
|
385
434
|
## Getting started (developing Kopular itself)
|
|
386
435
|
|
|
387
436
|
```bash
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kopular",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Kopular: a small component framework for KopScript — components, reactive state, constructor-injected services, routing, structural directives, and HTTP, with no template DSL and no DI container",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"./dom": "./src/dom.js",
|
|
27
27
|
"./directives": "./src/directives.js",
|
|
28
28
|
"./http": "./src/http.js",
|
|
29
|
-
"./forms": "./src/forms.js"
|
|
29
|
+
"./forms": "./src/forms.js",
|
|
30
|
+
"./testing": "./src/testing.js"
|
|
30
31
|
},
|
|
31
32
|
"files": [
|
|
32
33
|
"src",
|
|
@@ -40,6 +41,14 @@
|
|
|
40
41
|
"test": "vitest run",
|
|
41
42
|
"test:watch": "vitest"
|
|
42
43
|
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"jsdom": "^25.0.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"jsdom": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
43
52
|
"devDependencies": {
|
|
44
53
|
"kopscript": "^0.4.1",
|
|
45
54
|
"@types/jsdom": "^30.0.0",
|
package/src/router.js
CHANGED
|
@@ -7,6 +7,7 @@ export class Router extends Component {
|
|
|
7
7
|
(this.Paths = []);
|
|
8
8
|
(this.Pages = []);
|
|
9
9
|
(this.NotFoundPage = notFoundPage);
|
|
10
|
+
(this.Param = "");
|
|
10
11
|
window.addEventListener("popstate", (e) => (this.Update()));
|
|
11
12
|
}
|
|
12
13
|
|
|
@@ -21,12 +22,31 @@ export class Router extends Component {
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
Match(path) {
|
|
25
|
+
let pathSegments = path.split("/");
|
|
24
26
|
let found = this.NotFoundPage;
|
|
27
|
+
let param = "";
|
|
25
28
|
for (let i = 0; (i < this.Paths.length); (i = (i + 1))) {
|
|
26
|
-
|
|
29
|
+
let patternSegments = this.Paths[i].split("/");
|
|
30
|
+
if ((patternSegments.length !== pathSegments.length)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
let matched = true;
|
|
34
|
+
let capturedParam = "";
|
|
35
|
+
for (let j = 0; (j < patternSegments.length); (j = (j + 1))) {
|
|
36
|
+
if (patternSegments[j].startsWith(":")) {
|
|
37
|
+
(capturedParam = pathSegments[j]);
|
|
38
|
+
} else if ((patternSegments[j] !== pathSegments[j])) {
|
|
39
|
+
(matched = false);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (matched) {
|
|
27
44
|
(found = this.Pages[i]);
|
|
45
|
+
(param = capturedParam);
|
|
46
|
+
break;
|
|
28
47
|
}
|
|
29
48
|
}
|
|
49
|
+
(this.Param = param);
|
|
30
50
|
return found;
|
|
31
51
|
}
|
|
32
52
|
|
package/src/router.ks
CHANGED
|
@@ -28,10 +28,25 @@ class Router : Component {
|
|
|
28
28
|
private Component[] Pages;
|
|
29
29
|
private Component NotFoundPage;
|
|
30
30
|
|
|
31
|
+
// The current route's dynamic segment (e.g. "42" for a "/dogs/:id" route
|
|
32
|
+
// matching "/dogs/42"), or "" if the matched route has none.
|
|
33
|
+
//
|
|
34
|
+
// A plain field, deliberately **not** state<T> — Render() below always
|
|
35
|
+
// rebuilds a fresh outlet and re-Mount()s the matched page into it on
|
|
36
|
+
// every Navigate()/popstate, which already re-runs that page's own
|
|
37
|
+
// Render() (reading the fresh Param) with no extra step. Subscribing to
|
|
38
|
+
// it, the way a page reacts to its *own* state<T>, would fire during
|
|
39
|
+
// Match() itself — before Render() has finished swapping the matched page
|
|
40
|
+
// in — which crashes the very first time a route no page has been
|
|
41
|
+
// Mount()ed into yet matches, since a not-yet-mounted Component's
|
|
42
|
+
// inherited Update() has no ParentElement to replaceChild into.
|
|
43
|
+
public string Param;
|
|
44
|
+
|
|
31
45
|
constructor(Component notFoundPage) : base() {
|
|
32
46
|
this.Paths = [];
|
|
33
47
|
this.Pages = [];
|
|
34
48
|
this.NotFoundPage = notFoundPage;
|
|
49
|
+
this.Param = "";
|
|
35
50
|
// 'popstate' only fires on browser back/forward (or history.go/back/
|
|
36
51
|
// forward) — never on pushState itself, unlike hashchange firing
|
|
37
52
|
// whenever location.hash is set. Navigate() below calls Update()
|
|
@@ -40,6 +55,11 @@ class Router : Component {
|
|
|
40
55
|
window.addEventListener("popstate", (Event e) => this.Update());
|
|
41
56
|
}
|
|
42
57
|
|
|
58
|
+
// A path segment written ":name" (e.g. "dogs/:id") matches any single
|
|
59
|
+
// non-empty segment; every other segment must match literally. Comparing
|
|
60
|
+
// segment-by-segment (rather than the whole string at once) is what makes
|
|
61
|
+
// a static route's own matching still exactly as strict as a plain `==`
|
|
62
|
+
// was before — same segment count, same literal text throughout.
|
|
43
63
|
public void AddRoute(string path, Component page) {
|
|
44
64
|
this.Paths = this.Paths.Push(path);
|
|
45
65
|
this.Pages = this.Pages.Push(page);
|
|
@@ -51,12 +71,31 @@ class Router : Component {
|
|
|
51
71
|
}
|
|
52
72
|
|
|
53
73
|
private Component Match(string path) {
|
|
74
|
+
string[] pathSegments = path.Split("/");
|
|
54
75
|
Component found = this.NotFoundPage;
|
|
76
|
+
string param = "";
|
|
55
77
|
for (number i = 0; i < this.Paths.Length; i = i + 1) {
|
|
56
|
-
|
|
78
|
+
string[] patternSegments = this.Paths[i].Split("/");
|
|
79
|
+
if (patternSegments.Length != pathSegments.Length) { continue; }
|
|
80
|
+
|
|
81
|
+
bool matched = true;
|
|
82
|
+
string capturedParam = "";
|
|
83
|
+
for (number j = 0; j < patternSegments.Length; j = j + 1) {
|
|
84
|
+
if (patternSegments[j].StartsWith(":")) {
|
|
85
|
+
capturedParam = pathSegments[j];
|
|
86
|
+
} else if (patternSegments[j] != pathSegments[j]) {
|
|
87
|
+
matched = false;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (matched) {
|
|
57
93
|
found = this.Pages[i];
|
|
94
|
+
param = capturedParam;
|
|
95
|
+
break;
|
|
58
96
|
}
|
|
59
97
|
}
|
|
98
|
+
this.Param = param;
|
|
60
99
|
return found;
|
|
61
100
|
}
|
|
62
101
|
|
package/src/testing.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// A real Kopular app can only be exercised end-to-end by actually compiling
|
|
2
|
+
// its .ks sources and running the result in a DOM — there's no way to unit
|
|
3
|
+
// test a Component/Router graph without one. Every test suite that's tried
|
|
4
|
+
// this so far (Kopular's own, and KopularDemo's) ended up hand-rolling the
|
|
5
|
+
// same ~50-line dance: a fresh temp dir per test (Node's ESM module cache
|
|
6
|
+
// means re-importing the same compiled path twice never re-runs an ambient
|
|
7
|
+
// extern binding's top-level code — silently binding every test after the
|
|
8
|
+
// first to the first test's jsdom instance), compiling via kopscript's
|
|
9
|
+
// compileGraph, binding jsdom onto globalThis for the ambient `document`/
|
|
10
|
+
// `Element`/... extern declarations to find, then restoring/cleaning up.
|
|
11
|
+
// This is that dance, written once.
|
|
12
|
+
//
|
|
13
|
+
// Hand-written plain JS, not compiled from a .ks source, the same as
|
|
14
|
+
// http_runtime.js and bin/kp.mjs: filesystem/process orchestration isn't a
|
|
15
|
+
// Kopular Component.
|
|
16
|
+
//
|
|
17
|
+
// `jsdom` is a peer dependency (optional) — only installed by a consumer
|
|
18
|
+
// who actually calls this, not a weight every `kopular` install pays.
|
|
19
|
+
import { JSDOM } from "jsdom";
|
|
20
|
+
import { compileGraph } from "kopscript";
|
|
21
|
+
import { cpSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
22
|
+
import { tmpdir } from "node:os";
|
|
23
|
+
import { dirname, join } from "node:path";
|
|
24
|
+
import { fileURLToPath } from "node:url";
|
|
25
|
+
|
|
26
|
+
const GLOBAL_KEYS = ["document", "Element", "Event", "location", "history", "window", "fetch"];
|
|
27
|
+
const OWN_SRC_DIR = dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
|
|
29
|
+
// Copies every .ks/.html file from `srcDir` into `dir`, plus any filename
|
|
30
|
+
// listed in `extraFiles` (e.g. http_runtime.js — a hand-written sibling a
|
|
31
|
+
// compiled `using` graph needs at runtime, which the compiler wouldn't know
|
|
32
|
+
// to copy on its own).
|
|
33
|
+
function copySources(srcDir, dir, extraFiles) {
|
|
34
|
+
for (const name of readdirSync(srcDir)) {
|
|
35
|
+
if (name.endsWith(".ks") || name.endsWith(".html") || extraFiles.includes(name)) {
|
|
36
|
+
cpSync(join(srcDir, name), join(dir, name));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// kopscript's compiled output is genuine ESM (`export class ...`), which
|
|
42
|
+
// Node only interprets correctly given a ".mjs" extension or an ancestor
|
|
43
|
+
// package.json declaring "type": "module" — a bare os.tmpdir() has neither,
|
|
44
|
+
// so the dynamic `import()` below would otherwise fail with "Cannot use
|
|
45
|
+
// import statement outside a module" the moment anything outside this
|
|
46
|
+
// file's own module graph (a real npm package, not code vitest/vite-node
|
|
47
|
+
// itself transforms) tries to load it.
|
|
48
|
+
function mkTempDir() {
|
|
49
|
+
const dir = mkdtempSync(join(tmpdir(), "kopular-testing-"));
|
|
50
|
+
writeFileSync(join(dir, "package.json"), JSON.stringify({ type: "module" }), "utf-8");
|
|
51
|
+
return dir;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Copies this real, installed `kopular` package into the temp dir's own
|
|
55
|
+
// node_modules — for an entry that consumes Kopular the way a separate
|
|
56
|
+
// project does, through `extern ... from "kopular/..."` bare specifiers,
|
|
57
|
+
// not via relative `using`. Resolved off this very file's own location, so
|
|
58
|
+
// it's always the real package as installed in the *caller's*
|
|
59
|
+
// node_modules, not a guess at a path.
|
|
60
|
+
function copyKopularPackage(dir) {
|
|
61
|
+
mkdirSync(join(dir, "node_modules"), { recursive: true });
|
|
62
|
+
cpSync(join(OWN_SRC_DIR, ".."), join(dir, "node_modules", "kopular"), { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Shared by both exports below: compiles `entryFileName` (already written
|
|
66
|
+
// into `dir`, alongside everything it `using`s) and runs it against a fresh
|
|
67
|
+
// jsdom instance.
|
|
68
|
+
async function compileAndRun(dir, entryFileName, options) {
|
|
69
|
+
const entry = join(dir, entryFileName);
|
|
70
|
+
const result = compileGraph(entry);
|
|
71
|
+
if (!result.success) {
|
|
72
|
+
rmSync(dir, { recursive: true, force: true });
|
|
73
|
+
const messages = [...result.modules.values()]
|
|
74
|
+
.filter((m) => m.diagnostics.hasErrors)
|
|
75
|
+
.map((m) => m.diagnostics.format(m.source, m.absPath))
|
|
76
|
+
.join("\n\n");
|
|
77
|
+
throw new Error(messages || "Compilation failed");
|
|
78
|
+
}
|
|
79
|
+
for (const [absPath, js] of result.outputs) {
|
|
80
|
+
writeFileSync(absPath.replace(/\.ks$/, ".js"), js, "utf-8");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const dom = new JSDOM("<!doctype html><html><body></body></html>", { url: options.url ?? "http://localhost/" });
|
|
84
|
+
const g = globalThis;
|
|
85
|
+
const previous = {};
|
|
86
|
+
for (const key of GLOBAL_KEYS) previous[key] = g[key];
|
|
87
|
+
g.document = dom.window.document;
|
|
88
|
+
g.Element = dom.window.Element;
|
|
89
|
+
g.Event = dom.window.Event;
|
|
90
|
+
g.location = dom.window.location;
|
|
91
|
+
g.history = dom.window.history;
|
|
92
|
+
g.window = dom.window;
|
|
93
|
+
if (options.fetchMock) g.fetch = options.fetchMock;
|
|
94
|
+
|
|
95
|
+
let cleanedUp = false;
|
|
96
|
+
const cleanup = () => {
|
|
97
|
+
if (cleanedUp) return;
|
|
98
|
+
cleanedUp = true;
|
|
99
|
+
for (const key of GLOBAL_KEYS) g[key] = previous[key];
|
|
100
|
+
rmSync(dir, { recursive: true, force: true });
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await import(entry.replace(/\.ks$/, ".js"));
|
|
105
|
+
} catch (err) {
|
|
106
|
+
cleanup();
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { window: dom.window, cleanup };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Compiles a real KopScript app — `entryFileName` plus everything else in
|
|
115
|
+
* `srcDir` it `using`s — and runs it against a fresh jsdom instance. For a
|
|
116
|
+
* real project's own src/ directory, the way KopularDemo's test suite (or
|
|
117
|
+
* any app consuming `kopular/...` via `extern`) would use it.
|
|
118
|
+
*
|
|
119
|
+
* @param {string} srcDir - directory containing the entry file and
|
|
120
|
+
* everything it `using`s.
|
|
121
|
+
* @param {string} entryFileName - the entry .ks file's name within srcDir.
|
|
122
|
+
* @param {object} [options]
|
|
123
|
+
* @param {boolean} [options.includeKopularPackage] - copy the real
|
|
124
|
+
* installed `kopular` package into a temp node_modules/, for an entry
|
|
125
|
+
* that consumes Kopular via `extern ... from "kopular/..."` rather than
|
|
126
|
+
* relative `using`. Default false.
|
|
127
|
+
* @param {string[]} [options.extraFiles] - extra filenames (not .ks/.html)
|
|
128
|
+
* to copy from srcDir, e.g. ["http_runtime.js"].
|
|
129
|
+
* @param {(...args: unknown[]) => unknown} [options.fetchMock] - replaces
|
|
130
|
+
* the global `fetch` for the duration of the run, for testing `Http`.
|
|
131
|
+
* @param {string} [options.url] - the jsdom document's URL. Default
|
|
132
|
+
* "http://localhost/".
|
|
133
|
+
* @returns {Promise<{ window: import("jsdom").DOMWindow, cleanup: () => void }>}
|
|
134
|
+
* `cleanup()` restores globals and deletes the temp dir — call it in an
|
|
135
|
+
* `afterEach`/`finally`, always, even on a thrown error, or the next test
|
|
136
|
+
* silently inherits this one's globals.
|
|
137
|
+
*/
|
|
138
|
+
export async function runKopularApp(srcDir, entryFileName, options = {}) {
|
|
139
|
+
const dir = mkTempDir();
|
|
140
|
+
copySources(srcDir, dir, options.extraFiles ?? []);
|
|
141
|
+
if (options.includeKopularPackage) copyKopularPackage(dir);
|
|
142
|
+
return compileAndRun(dir, entryFileName, options);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Like `runKopularApp`, but for a fixture written inline as a string rather
|
|
147
|
+
* than a real file on disk — for testing Kopular's *own* source files
|
|
148
|
+
* (`using "./component"`, `using "./router"`, ...) against a tiny
|
|
149
|
+
* throwaway component, the way Kopular's own test suite does. Copies
|
|
150
|
+
* Kopular's own .ks sources (resolved from this very file's own directory)
|
|
151
|
+
* alongside the fixture, so a relative `using "./dom"` in `source` resolves
|
|
152
|
+
* correctly — this only makes sense for testing Kopular itself, not for an
|
|
153
|
+
* external consumer (which would `extern ... from "kopular/..."` instead;
|
|
154
|
+
* see `runKopularApp` for that case).
|
|
155
|
+
*
|
|
156
|
+
* @param {string} source - the entry file's full KopScript source.
|
|
157
|
+
* @param {object} [options] - same as `runKopularApp`, plus:
|
|
158
|
+
* @param {string} [options.entryFileName] - default "main.ks".
|
|
159
|
+
*/
|
|
160
|
+
export async function runKopularFixture(source, options = {}) {
|
|
161
|
+
const dir = mkTempDir();
|
|
162
|
+
copySources(OWN_SRC_DIR, dir, options.extraFiles ?? ["http_runtime.js"]);
|
|
163
|
+
const entryFileName = options.entryFileName ?? "main.ks";
|
|
164
|
+
writeFileSync(join(dir, entryFileName), source, "utf-8");
|
|
165
|
+
return compileAndRun(dir, entryFileName, options);
|
|
166
|
+
}
|