@nanobpm/urban 0.96.3 → 0.98.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.
Files changed (46) hide show
  1. package/README.md +60 -0
  2. package/dist/runtime/connector-worker-sdk.d.ts +2 -0
  3. package/dist/runtime/connector-worker-sdk.js +6 -1
  4. package/dist/runtime/core/host.d.ts +15 -5
  5. package/dist/runtime/core/manifest.d.ts +23 -0
  6. package/dist/runtime/core/manifest.js +8 -0
  7. package/dist/runtime/core/modules/connectors.d.ts +3 -0
  8. package/dist/runtime/core/modules/connectors.js +32 -1
  9. package/dist/runtime/core/modules/formjs.gen.js +1 -1
  10. package/dist/runtime/core/modules/forms.d.ts +20 -0
  11. package/dist/runtime/core/modules/forms.js +47 -0
  12. package/dist/runtime/core/modules/pages.js +12 -1
  13. package/dist/runtime/core/modules/runtime.gen.js +1 -1
  14. package/dist/runtime/core/modules/surfaces.js +57 -4
  15. package/dist/runtime/core/modules/workers.js +5 -1
  16. package/dist/runtime/core/validate.js +12 -0
  17. package/dist/runtime/engine/nanosdk.d.ts +1 -0
  18. package/dist/runtime/engine/nanosdk.js +3 -0
  19. package/dist/runtime/index.d.ts +3 -3
  20. package/dist/runtime/index.js +1 -1
  21. package/package.json +2 -2
  22. package/src/runtime/browser/fake-dom.test-utils.ts +16 -1
  23. package/src/runtime/browser/formjs.browser.js +258 -7
  24. package/src/runtime/browser/formjs.browser.test.ts +690 -0
  25. package/src/runtime/browser/runtime.browser.js +25 -3
  26. package/src/runtime/browser/runtime.engineform.test.ts +203 -0
  27. package/src/runtime/connector-worker-sdk.test.ts +9 -0
  28. package/src/runtime/connector-worker-sdk.ts +8 -1
  29. package/src/runtime/core/host.ts +16 -1
  30. package/src/runtime/core/manifest.ts +28 -0
  31. package/src/runtime/core/modules/connectors.test.ts +158 -1
  32. package/src/runtime/core/modules/connectors.ts +44 -1
  33. package/src/runtime/core/modules/formjs.gen.ts +1 -1
  34. package/src/runtime/core/modules/forms.ts +47 -0
  35. package/src/runtime/core/modules/pages.test.ts +21 -0
  36. package/src/runtime/core/modules/pages.ts +13 -1
  37. package/src/runtime/core/modules/runtime.gen.ts +1 -1
  38. package/src/runtime/core/modules/surfaces.test.ts +308 -0
  39. package/src/runtime/core/modules/surfaces.ts +58 -4
  40. package/src/runtime/core/modules/workers.test.ts +45 -1
  41. package/src/runtime/core/modules/workers.ts +5 -1
  42. package/src/runtime/core/validate.test.ts +19 -0
  43. package/src/runtime/core/validate.ts +12 -0
  44. package/src/runtime/engine/nanosdk.test.ts +17 -0
  45. package/src/runtime/engine/nanosdk.ts +4 -1
  46. package/src/runtime/index.ts +3 -0
@@ -134,3 +134,693 @@ test("renderForm wires a cancel button only when onCancel is supplied", async (t
134
134
  await buttons[1].fire("click");
135
135
  assert.equal(cancelled, true, "clicking cancel invokes onCancel");
136
136
  });
137
+
138
+ // ── #603: variables context — text templating, image component, prefill ──────
139
+
140
+ test("buildField templates {{name}} placeholders in a text component from the context", (t) => {
141
+ const created: FakeElement[] = [];
142
+ t.after(installFakeDom(created));
143
+ const built = buildField(
144
+ { type: "text", text: "Review {{title}} by {{author}} ({{count}} comments)" },
145
+ { title: "Draft v2", author: "ada", count: 3 },
146
+ );
147
+ assert.equal(built!.field.textContent, "Review Draft v2 by ada (3 comments)");
148
+ });
149
+
150
+ test("buildField templating: unknown names become empty, objects stringify as JSON", (t) => {
151
+ const created: FakeElement[] = [];
152
+ t.after(installFakeDom(created));
153
+ const built = buildField(
154
+ { type: "text", text: "[{{missing}}] {{payload}}" },
155
+ { payload: { a: 1 } },
156
+ );
157
+ assert.equal(built!.field.textContent, "[] {\"a\":1}");
158
+ });
159
+
160
+ test("buildField templating is textContent-only — a variable holding markup renders as literal text", (t) => {
161
+ const created: FakeElement[] = [];
162
+ t.after(installFakeDom(created));
163
+ const evil = "<img src=x onerror=alert(1)>";
164
+ const built = buildField({ type: "text", text: "Draft: {{body}}" }, { body: evil });
165
+ // The value went through textContent, so it is literal text — the fake DOM has no
166
+ // innerHTML path at all, so asserting the textContent round-trips the markup
167
+ // character-for-character pins the no-markup-injection rule for templated text.
168
+ assert.equal(built!.field.textContent, "Draft: " + evil);
169
+ assert.equal(built!.field.children.length, 0, "no child elements were created from the markup");
170
+ });
171
+
172
+ test("buildField renders an image component with a literal URL source", (t) => {
173
+ const created: FakeElement[] = [];
174
+ t.after(installFakeDom(created));
175
+ const built = buildField({ type: "image", source: "https://cdn.example.com/t1.png", alt: "Thumb 1" }, {});
176
+ assert.ok(built && !built.read, "an image component is keyless (no reader)");
177
+ const img = created.find((n) => n.tagName === "IMG");
178
+ assert.ok(img);
179
+ assert.equal(img!.getAttribute("src"), "https://cdn.example.com/t1.png");
180
+ assert.equal(img!.getAttribute("alt"), "Thumb 1");
181
+ assert.equal(img!.getAttribute("class"), "njf-image");
182
+ });
183
+
184
+ test("buildField resolves an image =varName source from the context (plain variable reference)", (t) => {
185
+ const created: FakeElement[] = [];
186
+ t.after(installFakeDom(created));
187
+ const built = buildField(
188
+ { type: "image", source: "=img1", alt: "Choice 1" },
189
+ { img1: "data:image/png;base64,iVBORw0KGgo=" },
190
+ );
191
+ const img = created.find((n) => n.tagName === "IMG");
192
+ assert.ok(img);
193
+ assert.equal(img!.getAttribute("src"), "data:image/png;base64,iVBORw0KGgo=");
194
+ });
195
+
196
+ test("buildField rejects unsafe image sources — javascript:, other schemes, protocol-relative, non-identifier =expr", (t) => {
197
+ const created: FakeElement[] = [];
198
+ t.after(installFakeDom(created));
199
+ assert.equal(buildField({ type: "image", source: "javascript:alert(1)" }, {}), null, "javascript: rejected");
200
+ assert.equal(buildField({ type: "image", source: "=evil" }, { evil: "javascript:alert(1)" }), null, "a variable holding javascript: is rejected");
201
+ assert.equal(buildField({ type: "image", source: "data:text/html;base64,PGI+" }, {}), null, "non-image data: rejected");
202
+ assert.equal(buildField({ type: "image", source: "//evil.example.com/x.png" }, {}), null, "protocol-relative rejected");
203
+ assert.equal(buildField({ type: "image", source: "=foo.bar" }, { foo: { bar: "/x.png" } }), null, "a FEEL path (not a plain variable) is rejected");
204
+ assert.equal(buildField({ type: "image", source: "=missing" }, {}), null, "an unknown variable resolves to nothing");
205
+ assert.equal(buildField({ type: "image" }, {}), null, "no source at all renders nothing");
206
+ assert.equal(created.filter((n) => n.tagName === "IMG").length, 0, "no <img> was created for a rejected source");
207
+ });
208
+
209
+ test("buildField allows app-relative image sources", (t) => {
210
+ const created: FakeElement[] = [];
211
+ t.after(installFakeDom(created));
212
+ const built = buildField({ type: "image", source: "/assets/thumb.png" }, {});
213
+ assert.ok(built);
214
+ assert.equal(created.find((n) => n.tagName === "IMG")!.getAttribute("src"), "/assets/thumb.png");
215
+ });
216
+
217
+ test("buildField prefills a textfield and textarea from the context", (t) => {
218
+ const created: FakeElement[] = [];
219
+ t.after(installFakeDom(created));
220
+ buildField({ type: "textfield", key: "title" }, { title: "Draft v2" });
221
+ buildField({ type: "textarea", key: "body" }, { body: "once upon a time" });
222
+ const input = created.find((n) => n.tagName === "INPUT");
223
+ const area = created.find((n) => n.tagName === "TEXTAREA");
224
+ assert.equal(input!.value, "Draft v2");
225
+ assert.equal(area!.value, "once upon a time");
226
+ });
227
+
228
+ test("buildField prefills a radio group and a select from the context", (t) => {
229
+ const created: FakeElement[] = [];
230
+ t.after(installFakeDom(created));
231
+ const radio = buildField(
232
+ {
233
+ type: "radio",
234
+ key: "choice",
235
+ values: [
236
+ { value: "t1", label: "One" },
237
+ { value: "t2", label: "Two" },
238
+ ],
239
+ },
240
+ { choice: "t2" },
241
+ );
242
+ const radios = created.filter((n) => n.tagName === "INPUT" && n.type === "radio");
243
+ assert.equal(radios[0].checked, false);
244
+ assert.equal(radios[1].checked, true, "the context-matching option is checked");
245
+ assert.deepEqual(radio!.read!(), { key: "choice", value: "t2" }, "the prefill is submitted unchanged when untouched");
246
+
247
+ const select = buildField(
248
+ {
249
+ type: "select",
250
+ key: "color",
251
+ values: [
252
+ { value: "r", label: "Red" },
253
+ { value: "g", label: "Green" },
254
+ ],
255
+ },
256
+ { color: "g" },
257
+ );
258
+ const sel = created.find((n) => n.tagName === "SELECT");
259
+ assert.equal(sel!.value, "g");
260
+ assert.deepEqual(select!.read!(), { key: "color", value: "g" });
261
+ });
262
+
263
+ test("buildField prefills a checkbox from a boolean context value", (t) => {
264
+ const created: FakeElement[] = [];
265
+ t.after(installFakeDom(created));
266
+ const built = buildField({ type: "checkbox", key: "agree" }, { agree: true });
267
+ const input = created.find((n) => n.tagName === "INPUT");
268
+ assert.equal(input!.checked, true);
269
+ assert.deepEqual(built!.read!(), { key: "agree", value: true });
270
+ });
271
+
272
+ test("buildField prefill: an absent key leaves the input untouched (backwards compatible)", (t) => {
273
+ const created: FakeElement[] = [];
274
+ t.after(installFakeDom(created));
275
+ const built = buildField({ type: "textfield", key: "note" }, { other: "x" });
276
+ const input = created.find((n) => n.tagName === "INPUT");
277
+ assert.equal(input!.value, "", "no context key → no prefill");
278
+ assert.equal(built!.read!(), null, "an untouched blank field is still omitted");
279
+ });
280
+
281
+ test("renderForm passes opts.context through to every component", (t) => {
282
+ const created: FakeElement[] = [];
283
+ t.after(installFakeDom(created));
284
+ const schema = {
285
+ components: [
286
+ { type: "text", text: "Reviewing {{title}}" },
287
+ { type: "image", source: "=img1", alt: "thumb" },
288
+ { type: "textfield", key: "title" },
289
+ ],
290
+ };
291
+ renderForm(schema, {
292
+ context: { title: "Draft v2", img1: "https://cdn.example.com/t.png" },
293
+ onSubmit: () => {},
294
+ });
295
+ const p = created.find((n) => n.tagName === "P");
296
+ assert.equal(p!.textContent, "Reviewing Draft v2");
297
+ const img = created.find((n) => n.tagName === "IMG");
298
+ assert.equal(img!.getAttribute("src"), "https://cdn.example.com/t.png");
299
+ const input = created.find((n) => n.tagName === "INPUT");
300
+ assert.equal(input!.value, "Draft v2", "keyed input prefilled from the same context");
301
+ });
302
+
303
+ test("renderForm without a context renders exactly as before (backwards compatible)", (t) => {
304
+ const created: FakeElement[] = [];
305
+ t.after(installFakeDom(created));
306
+ renderForm(
307
+ { components: [{ type: "text", text: "Static {{notReplaced}}" }, { type: "textfield", key: "n" }] },
308
+ { onSubmit: () => {} },
309
+ );
310
+ const p = created.find((n) => n.tagName === "P");
311
+ assert.equal(p!.textContent, "Static ", "unknown names template to empty even with no context");
312
+ const input = created.find((n) => n.tagName === "INPUT");
313
+ assert.equal(input!.value, "");
314
+ });
315
+
316
+ // ── PR #604 review: defect classes hardened ──────────────────────────────────
317
+
318
+ test("buildField rejects disguised unsafe image URLs — whitespace, control chars, backslashes", (t) => {
319
+ const created: FakeElement[] = [];
320
+ t.after(installFakeDom(created));
321
+ const disguised = [
322
+ "java\tscript:alert(1)",
323
+ "java\nscript:alert(1)",
324
+ " javascript:alert(1)",
325
+ " data:image/svg+xml,<svg onload=alert(1)>",
326
+ "\u0000javascript:alert(1)",
327
+ "/\\evil.example.com/x.png",
328
+ "\\\\evil.example.com/x.png",
329
+ "https://ok.example.com/a b.png",
330
+ "/x.png\u007f",
331
+ ];
332
+ for (const source of disguised) {
333
+ assert.equal(buildField({ type: "image", source }, {}), null, `literal ${JSON.stringify(source)} rejected`);
334
+ assert.equal(buildField({ type: "image", source: "=u" }, { u: source }), null, `variable ${JSON.stringify(source)} rejected`);
335
+ }
336
+ assert.equal(created.filter((n) => n.tagName === "IMG").length, 0);
337
+ });
338
+
339
+ test("buildField ignores inherited prototype members of the context (own properties only)", (t) => {
340
+ const created: FakeElement[] = [];
341
+ t.after(installFakeDom(created));
342
+ for (const key of ["__proto__", "constructor", "toString", "hasOwnProperty"]) {
343
+ const built = buildField({ type: "textfield", key }, {});
344
+ const input = created.filter((n) => n.tagName === "INPUT").at(-1);
345
+ assert.equal(input!.value, "", `${key}: an absent prototype member does not prefill`);
346
+ assert.equal(built!.read!(), null, `${key}: untouched field is omitted`);
347
+ }
348
+ const text = buildField({ type: "text", text: "[{{constructor}}][{{toString}}]" }, {});
349
+ assert.equal(text!.field.textContent, "[][]", "unknown prototype-member placeholders render empty");
350
+ assert.equal(buildField({ type: "image", source: "=constructor" }, {}), null);
351
+ });
352
+
353
+ test("buildField honours explicitly supplied own values named like prototype members", (t) => {
354
+ const created: FakeElement[] = [];
355
+ t.after(installFakeDom(created));
356
+ const ctx = JSON.parse('{"__proto__":"P","constructor":"C"}');
357
+ buildField({ type: "textfield", key: "__proto__" }, ctx);
358
+ buildField({ type: "textfield", key: "constructor" }, ctx);
359
+ const inputs = created.filter((n) => n.tagName === "INPUT");
360
+ assert.equal(inputs[0]!.value, "P");
361
+ assert.equal(inputs[1]!.value, "C");
362
+ const text = buildField({ type: "text", text: "{{__proto__}}/{{constructor}}" }, ctx);
363
+ assert.equal(text!.field.textContent, "P/C");
364
+ });
365
+
366
+ // ── PR #604 round 3: comma data URLs, mount-rebased image sources, cleared prefill ──
367
+
368
+ test("buildField allows a comma-delimited image data URL (no parameters, no ;base64)", (t) => {
369
+ const created: FakeElement[] = [];
370
+ t.after(installFakeDom(created));
371
+ // RFC 2397: the mediatype may be followed directly by `,` — parameters and
372
+ // `;base64` are both optional. A percent-encoded PNG payload is a valid source.
373
+ const literal = "data:image/png,%89PNG%0D%0A%1A%0A";
374
+ const built = buildField({ type: "image", source: literal }, {});
375
+ assert.ok(built, "a comma-delimited data:image/png URL is accepted");
376
+ assert.equal(created.find((n) => n.tagName === "IMG")!.getAttribute("src"), literal);
377
+ // The same URL arriving via a `=varName` reference is accepted too.
378
+ created.length = 0;
379
+ const viaVar = buildField({ type: "image", source: "=img" }, { img: literal });
380
+ assert.ok(viaVar, "a comma-delimited data URL from a variable is accepted");
381
+ assert.equal(created.find((n) => n.tagName === "IMG")!.getAttribute("src"), literal);
382
+ });
383
+
384
+ test("buildField image source: opts.resolveUrl rebases a root-relative source onto the app mount", (t) => {
385
+ const created: FakeElement[] = [];
386
+ t.after(installFakeDom(created));
387
+ // Behind the console proxy the app is mounted at /console/app-view/demo/: a
388
+ // literal (or variable) source of /app/thumb.png must request the APP's
389
+ // /app/thumb.png, not the console's. Both surfaces pass their mount-aware URL
390
+ // resolver as opts.resolveUrl; the resolver runs after the raw-source allowlist
391
+ // and its RESULT is revalidated (round 5), so it can never launder a rejected
392
+ // URL — and only an http(s)-ish output is ever set on the <img>.
393
+ const rebase = (u: string) =>
394
+ u.startsWith("/") && !u.startsWith("//") ? "https://console.example.com/console/app-view/demo" + u : u;
395
+ const literal = buildField({ type: "image", source: "/app/thumb.png" }, {}, { resolveUrl: rebase });
396
+ assert.equal(
397
+ created.filter((n) => n.tagName === "IMG").at(-1)!.getAttribute("src"),
398
+ "https://console.example.com/console/app-view/demo/app/thumb.png",
399
+ "a literal root-relative source is rebased onto the app mount",
400
+ );
401
+ assert.ok(literal);
402
+ const viaVar = buildField({ type: "image", source: "=img" }, { img: "/app/thumb.png" }, { resolveUrl: rebase });
403
+ assert.equal(
404
+ created.filter((n) => n.tagName === "IMG").at(-1)!.getAttribute("src"),
405
+ "https://console.example.com/console/app-view/demo/app/thumb.png",
406
+ "a variable holding a root-relative source is rebased too",
407
+ );
408
+ assert.ok(viaVar);
409
+ // Absolute http(s) and data: URLs pass through the resolver untouched (the
410
+ // resolver only rewrites single-leading-slash paths, exactly like apiUrl).
411
+ buildField({ type: "image", source: "https://cdn.example.com/t.png" }, {}, { resolveUrl: rebase });
412
+ assert.equal(
413
+ created.filter((n) => n.tagName === "IMG").at(-1)!.getAttribute("src"),
414
+ "https://cdn.example.com/t.png",
415
+ "an absolute URL is not rewritten",
416
+ );
417
+ // Without a resolver the historical behaviour stands (backwards compatible).
418
+ buildField({ type: "image", source: "/app/thumb.png" }, {});
419
+ assert.equal(
420
+ created.filter((n) => n.tagName === "IMG").at(-1)!.getAttribute("src"),
421
+ "/app/thumb.png",
422
+ "no resolver → the source is used verbatim",
423
+ );
424
+ // The allowlist still runs first: a rejected source never reaches the resolver.
425
+ assert.equal(buildField({ type: "image", source: "javascript:alert(1)" }, {}, { resolveUrl: rebase }), null);
426
+ });
427
+
428
+ test("renderForm threads opts.resolveUrl through to every image component", (t) => {
429
+ const created: FakeElement[] = [];
430
+ t.after(installFakeDom(created));
431
+ const rebase = (u: string) => (u.startsWith("/") && !u.startsWith("//") ? "https://app.example.com/mounted" + u : u);
432
+ renderForm(
433
+ { components: [{ type: "image", source: "/app/a.png" }, { type: "image", source: "=img" }] },
434
+ { context: { img: "/app/b.png" }, resolveUrl: rebase, onSubmit: () => {} },
435
+ );
436
+ const srcs = created.filter((n) => n.tagName === "IMG").map((n) => n.getAttribute("src"));
437
+ assert.deepEqual(srcs, ["https://app.example.com/mounted/app/a.png", "https://app.example.com/mounted/app/b.png"]);
438
+ });
439
+
440
+ test("buildField submits an empty string when a prefilled textfield/textarea is cleared", (t) => {
441
+ const created: FakeElement[] = [];
442
+ t.after(installFakeDom(created));
443
+ const tf = buildField({ type: "textfield", key: "title" }, { title: "Draft v2" });
444
+ const ta = buildField({ type: "textarea", key: "body" }, { body: "once upon a time" });
445
+ const input = created.find((n) => n.tagName === "INPUT")!;
446
+ const area = created.find((n) => n.tagName === "TEXTAREA")!;
447
+ assert.equal(input.value, "Draft v2");
448
+ assert.equal(area.value, "once upon a time");
449
+ input.value = "";
450
+ area.value = "";
451
+ assert.deepEqual(tf!.read!(), { key: "title", value: "" }, "a cleared prefilled textfield submits an empty string");
452
+ assert.deepEqual(ta!.read!(), { key: "body", value: "" }, "a cleared prefilled textarea submits an empty string");
453
+ });
454
+
455
+ test("buildField submits an empty string when a prefilled select is set back to the blank option", (t) => {
456
+ const created: FakeElement[] = [];
457
+ t.after(installFakeDom(created));
458
+ const built = buildField(
459
+ {
460
+ type: "select",
461
+ key: "color",
462
+ values: [
463
+ { value: "r", label: "Red" },
464
+ { value: "g", label: "Green" },
465
+ ],
466
+ },
467
+ { color: "g" },
468
+ );
469
+ const sel = created.find((n) => n.tagName === "SELECT")!;
470
+ assert.equal(sel.value, "g", "prefilled from the context");
471
+ sel.value = ""; // the operator chooses the blank "—" option
472
+ assert.deepEqual(built!.read!(), { key: "color", value: "" }, "a cleared prefilled select submits an empty string");
473
+ });
474
+
475
+ test("buildField cleared-prefill regressions hold for number fields and no-context omission is preserved", (t) => {
476
+ const created: FakeElement[] = [];
477
+ t.after(installFakeDom(created));
478
+ // A cleared prefilled NUMBER field submits an empty string (explicitly cleared),
479
+ // never null/NaN.
480
+ const num = buildField({ type: "number", key: "qty" }, { qty: 3 });
481
+ const numInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
482
+ assert.equal(numInput.value, "3");
483
+ numInput.value = "";
484
+ assert.deepEqual(num!.read!(), { key: "qty", value: "" }, "a cleared prefilled number field submits an empty string");
485
+ // No-context fields keep the historical omission behaviour: a blank field that
486
+ // was never prefilled is left out of the submitted variables entirely.
487
+ const plain = buildField({ type: "textfield", key: "note" }, { other: "x" });
488
+ const plainInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
489
+ assert.equal(plainInput.value, "");
490
+ assert.equal(plain!.read!(), null, "an untouched blank field with no prefill is still omitted");
491
+ const plainSelect = buildField(
492
+ { type: "select", key: "s", values: [{ value: "a", label: "A" }] },
493
+ {},
494
+ );
495
+ const sel = created.filter((n) => n.tagName === "SELECT").at(-1)!;
496
+ assert.equal(sel.value, "");
497
+ assert.equal(plainSelect!.read!(), null, "an untouched blank select with no prefill is still omitted");
498
+ });
499
+
500
+ // ── PR #604 round 4: a REJECTED prefill is not a prefill — never overwrite with "" ──
501
+
502
+ test("buildField select: a context value absent from values[] is a rejected prefill — untouched submit omits the key", (t) => {
503
+ const created: FakeElement[] = [];
504
+ t.after(installFakeDom(created));
505
+ // The context value "b" matches NO option: a real browser select rejects the
506
+ // assignment and snaps back to the blank option. Model that native sanitization
507
+ // by leaving the fake control's value at "" (what the DOM would report).
508
+ const built = buildField(
509
+ {
510
+ type: "select",
511
+ key: "color",
512
+ values: [
513
+ { value: "r", label: "Red" },
514
+ { value: "g", label: "Green" },
515
+ ],
516
+ },
517
+ { color: "b" },
518
+ );
519
+ const sel = created.find((n) => n.tagName === "SELECT")!;
520
+ assert.ok(sel, "the select control was built");
521
+ // A real select snaps an out-of-range assignment back to the blank option; model
522
+ // that native behaviour (the fake DOM stores values verbatim). The renderer's
523
+ // guard is structural — the rejected value never counted as a prefill.
524
+ sel.value = "";
525
+ assert.equal(
526
+ built!.read!(),
527
+ null,
528
+ "an untouched form must NOT overwrite the original variable with an empty string",
529
+ );
530
+ });
531
+
532
+ test("buildField datetime-local: a timezone-bearing prefill the control rejects is omitted, not submitted as empty", (t) => {
533
+ const created: FakeElement[] = [];
534
+ t.after(installFakeDom(created));
535
+ // A datetime-local control accepts only "YYYY-MM-DDTHH:mm" (no zone designator);
536
+ // the fake DOM models the browser blanking a timezone-bearing value at
537
+ // assignment time (see fake-dom.test-utils.ts), so the renderer's read-back
538
+ // sees exactly what the DOM would report.
539
+ const built = buildField({ type: "datetime", key: "due" }, { due: "2026-11-04T10:30:00Z" });
540
+ const input = created.find((n) => n.tagName === "INPUT")!;
541
+ assert.equal(input.value, "", "the control rejected the zoned value at assignment");
542
+ assert.equal(
543
+ built!.read!(),
544
+ null,
545
+ "a rejected datetime prefill leaves the original variable untouched on submit",
546
+ );
547
+ // …while a plain local datetime the control CAN hold is still a successful prefill.
548
+ const ok = buildField({ type: "datetime", key: "start" }, { start: "2026-11-04T10:30" });
549
+ const okInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
550
+ assert.equal(okInput.value, "2026-11-04T10:30");
551
+ assert.deepEqual(ok!.read!(), { key: "start", value: "2026-11-04T10:30" });
552
+ });
553
+
554
+ test("buildField rejected-prefill guard still allows an explicit clear after a SUCCESSFUL prefill", (t) => {
555
+ const created: FakeElement[] = [];
556
+ t.after(installFakeDom(created));
557
+ // Select: a value present in values[] IS accepted, so clearing it afterwards is
558
+ // an explicit clear and submits "".
559
+ const sel = buildField(
560
+ {
561
+ type: "select",
562
+ key: "color",
563
+ values: [
564
+ { value: "r", label: "Red" },
565
+ { value: "g", label: "Green" },
566
+ ],
567
+ },
568
+ { color: "g" },
569
+ );
570
+ const selEl = created.find((n) => n.tagName === "SELECT")!;
571
+ assert.equal(selEl.value, "g", "accepted prefill");
572
+ selEl.value = "";
573
+ assert.deepEqual(sel!.read!(), { key: "color", value: "" }, "clearing an accepted select prefill submits an empty string");
574
+ // Typed input: a valid local datetime IS accepted; clearing it submits "".
575
+ const dt = buildField({ type: "datetime", key: "start" }, { start: "2026-11-04T10:30" });
576
+ const dtInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
577
+ assert.equal(dtInput.value, "2026-11-04T10:30", "accepted prefill");
578
+ dtInput.value = "";
579
+ assert.deepEqual(dt!.read!(), { key: "start", value: "" }, "clearing an accepted datetime prefill submits an empty string");
580
+ });
581
+
582
+ // ── PR #604 round 5: the resolver's OUTPUT is revalidated — no allowlist bypass ──
583
+
584
+ test("buildField revalidates the RESOLVED image URL — a resolver cannot launder a forbidden scheme", (t) => {
585
+ const created: FakeElement[] = [];
586
+ t.after(installFakeDom(created));
587
+ // The pages runtime's apiUrl strips the leading slash before `new URL(...)`,
588
+ // so an allowlist-accepted "app-relative" source of "/javascript:alert(1)"
589
+ // comes back as the forbidden-scheme URL "javascript:alert(1)". Model that
590
+ // resolver exactly (a file:// APP_BASE, like the Node test runner's
591
+ // import.meta.url) and require the renderer to re-run the allowlist on the
592
+ // resolver's RESULT rather than trusting it verbatim.
593
+ const base = new URL("../", import.meta.url);
594
+ const apiUrl = (u: string) =>
595
+ u.startsWith("/") && !u.startsWith("//") ? new URL(u.slice(1), base).toString() : u;
596
+ for (const source of ["/javascript:alert(1)", "/data:text/html;base64,PGI+", "/vbscript:msgbox(1)"]) {
597
+ assert.equal(
598
+ buildField({ type: "image", source }, {}, { resolveUrl: apiUrl }),
599
+ null,
600
+ `literal ${JSON.stringify(source)} rejected after resolution`,
601
+ );
602
+ assert.equal(
603
+ buildField({ type: "image", source: "=u" }, { u: source }, { resolveUrl: apiUrl }),
604
+ null,
605
+ `variable holding ${JSON.stringify(source)} rejected after resolution`,
606
+ );
607
+ }
608
+ assert.equal(created.filter((n) => n.tagName === "IMG").length, 0, "no <img> was rendered for a laundered source");
609
+ // The revalidation fails closed on ANY non-http(s)-ish resolver output: under
610
+ // this Node runner the module base is file://, so even a genuine app path
611
+ // resolves to a file: URL and is refused — in a browser the same resolver
612
+ // yields https:// and passes. Pin the fail-closed side here…
613
+ assert.equal(
614
+ buildField({ type: "image", source: "/app/thumb.png" }, {}, { resolveUrl: apiUrl }),
615
+ null,
616
+ "a file://-rebased resolver output is refused (not a fetchable image origin)",
617
+ );
618
+ // …and the pass side with an https-mount resolver (what a browser deployment
619
+ // actually produces): a genuine app path renders, rebased onto the mount.
620
+ const httpsRebase = (u: string) =>
621
+ u.startsWith("/") && !u.startsWith("//") ? new URL(u.slice(1), "https://console.example.com/console/app-view/demo/").toString() : u;
622
+ const ok = buildField({ type: "image", source: "/app/thumb.png" }, {}, { resolveUrl: httpsRebase });
623
+ assert.ok(ok, "a genuine app path survives an https-mount resolver");
624
+ const src = created.find((n) => n.tagName === "IMG")!.getAttribute("src");
625
+ assert.equal(src, "https://console.example.com/console/app-view/demo/app/thumb.png", "rebased onto the https app mount");
626
+ });
627
+
628
+ test("buildField keeps every SUPPORTED image source after resolution (path-returning, data:image, relative)", (t) => {
629
+ const created: FakeElement[] = [];
630
+ t.after(installFakeDom(created));
631
+ // The inbox resolver returns a PATH (mount prefix + source), and both
632
+ // resolvers leave data:image and non-root-relative sources unchanged. The
633
+ // post-resolution check is the same allowlist as the raw-source check, so
634
+ // every source it accepts raw must survive an identity-preserving resolver.
635
+ const inboxRebase = (u: string) => (u.startsWith("/") && !u.startsWith("//") ? "/console/app-view/demo" + u : u);
636
+ const png = "data:image/png;base64,iVBORw0KGgo=";
637
+ const cases: Array<[string, string]> = [
638
+ ["/app/thumb.png", "/console/app-view/demo/app/thumb.png"],
639
+ [png, png],
640
+ ["thumb.png", "thumb.png"],
641
+ ["img/thumb.png", "img/thumb.png"],
642
+ ];
643
+ for (const [source, expected] of cases) {
644
+ for (const [s, ctx] of [[source, {}], ["=u", { u: source }]] as const) {
645
+ assert.ok(buildField({ type: "image", source: s }, ctx, { resolveUrl: inboxRebase }), `${source} (${s}) accepted`);
646
+ assert.equal(created.filter((n) => n.tagName === "IMG").at(-1)!.getAttribute("src"), expected);
647
+ }
648
+ }
649
+ });
650
+
651
+ // ── PR #604 round 7: untouched prefills keep their TYPE; resolver throws skip the image ──
652
+
653
+ test("buildField submits the ORIGINAL typed value while a prefilled control still shows it", (t) => {
654
+ const created: FakeElement[] = [];
655
+ t.after(installFakeDom(created));
656
+ // The bug class: a prefill is STRINGIFIED into the control (form-js semantics),
657
+ // but an untouched submit must not forward that stringified rendering — the
658
+ // completion endpoint sends submitted values verbatim, so a textarea holding
659
+ // { approved: true } would otherwise overwrite the object variable with the
660
+ // STRING "{\"approved\":true}", and a numeric select/radio prefill would
661
+ // overwrite the number with "1". While the control still shows exactly the
662
+ // prefill, the reader returns the captured ORIGINAL value (type preserved).
663
+ const ta = buildField({ type: "textarea", key: "review" }, { review: { approved: true } });
664
+ const area = created.find((n) => n.tagName === "TEXTAREA")!;
665
+ assert.equal(area.value, '{"approved":true}', "the object prefill is shown as JSON");
666
+ assert.deepEqual(
667
+ ta!.read!(),
668
+ { key: "review", value: { approved: true } },
669
+ "an untouched object prefill resubmits the OBJECT, not its JSON string",
670
+ );
671
+
672
+ const arr = buildField({ type: "textfield", key: "tags" }, { tags: ["a", "b"] });
673
+ const arrInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
674
+ assert.equal(arrInput.value, '["a","b"]', "the array prefill is shown as JSON");
675
+ assert.deepEqual(arr!.read!(), { key: "tags", value: ["a", "b"] }, "an untouched array prefill resubmits the ARRAY");
676
+
677
+ const sel = buildField(
678
+ {
679
+ type: "select",
680
+ key: "priority",
681
+ values: [
682
+ { value: 1, label: "One" },
683
+ { value: 2, label: "Two" },
684
+ ],
685
+ },
686
+ { priority: 2 },
687
+ );
688
+ const selEl = created.find((n) => n.tagName === "SELECT")!;
689
+ assert.equal(selEl.value, "2", "the numeric prefill selects the matching option");
690
+ assert.deepEqual(sel!.read!(), { key: "priority", value: 2 }, "an untouched numeric select prefill resubmits the NUMBER");
691
+
692
+ const radio = buildField(
693
+ {
694
+ type: "radio",
695
+ key: "level",
696
+ values: [
697
+ { value: 1, label: "One" },
698
+ { value: 2, label: "Two" },
699
+ ],
700
+ },
701
+ { level: 2 },
702
+ );
703
+ assert.deepEqual(radio!.read!(), { key: "level", value: 2 }, "an untouched numeric radio prefill resubmits the NUMBER");
704
+
705
+ const num = buildField({ type: "number", key: "qty" }, { qty: 3 });
706
+ const numInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
707
+ assert.equal(numInput.value, "3");
708
+ assert.deepEqual(num!.read!(), { key: "qty", value: 3 }, "an untouched number prefill resubmits the NUMBER");
709
+
710
+ const dt = buildField({ type: "datetime", key: "start" }, { start: "2026-11-04T10:30" });
711
+ assert.deepEqual(
712
+ dt!.read!(),
713
+ { key: "start", value: "2026-11-04T10:30" },
714
+ "an untouched datetime prefill resubmits the original string",
715
+ );
716
+ });
717
+
718
+ test("buildField an EDITED prefilled control submits the edited (string/number) value", (t) => {
719
+ const created: FakeElement[] = [];
720
+ t.after(installFakeDom(created));
721
+ // The type preservation above is ONLY for the untouched case: once the operator
722
+ // changes the control away from the prefill rendering, the control holds what
723
+ // they typed and that (string, or coerced number for a number field) submits.
724
+ const ta = buildField({ type: "textarea", key: "review" }, { review: { approved: true } });
725
+ const area = created.find((n) => n.tagName === "TEXTAREA")!;
726
+ area.value = '{"approved":false}';
727
+ assert.deepEqual(
728
+ ta!.read!(),
729
+ { key: "review", value: '{"approved":false}' },
730
+ "an edited textarea submits the edited string (the renderer does not parse it)",
731
+ );
732
+
733
+ const sel = buildField(
734
+ {
735
+ type: "select",
736
+ key: "priority",
737
+ values: [
738
+ { value: 1, label: "One" },
739
+ { value: 2, label: "Two" },
740
+ ],
741
+ },
742
+ { priority: 2 },
743
+ );
744
+ const selEl = created.find((n) => n.tagName === "SELECT")!;
745
+ selEl.value = "1"; // the operator picks a different option
746
+ assert.deepEqual(sel!.read!(), { key: "priority", value: "1" }, "a changed select submits the picked option value");
747
+
748
+ const num = buildField({ type: "number", key: "qty" }, { qty: 3 });
749
+ const numInput = created.filter((n) => n.tagName === "INPUT").at(-1)!;
750
+ numInput.value = "5";
751
+ assert.deepEqual(num!.read!(), { key: "qty", value: 5 }, "an edited number field submits the coerced number");
752
+
753
+ // Clearing still submits an explicit empty string (round 3), never the prefill.
754
+ numInput.value = "";
755
+ assert.deepEqual(num!.read!(), { key: "qty", value: "" }, "a cleared prefilled number field submits an empty string");
756
+ area.value = "";
757
+ assert.deepEqual(ta!.read!(), { key: "review", value: "" }, "a cleared prefilled textarea submits an empty string");
758
+ });
759
+
760
+ test("renderForm an untouched prefill round-trips through submit with its type intact", async (t) => {
761
+ const created: FakeElement[] = [];
762
+ t.after(installFakeDom(created));
763
+ // End-to-end through the form submit handler: the variables bag the completion
764
+ // endpoint receives carries the typed values, so completing a task without
765
+ // touching the form can never retype its variables.
766
+ let submitted: Record<string, unknown> | undefined;
767
+ const form = renderForm(
768
+ {
769
+ components: [
770
+ { type: "textarea", key: "review" },
771
+ { type: "number", key: "qty" },
772
+ ],
773
+ },
774
+ {
775
+ context: { review: { approved: true }, qty: 3 },
776
+ onSubmit: (variables) => {
777
+ submitted = variables;
778
+ },
779
+ },
780
+ );
781
+ await form.fire("submit");
782
+ assert.ok(submitted);
783
+ assert.deepEqual(submitted!.review, { approved: true }, "the object variable survives an untouched submit");
784
+ assert.deepEqual(submitted!.qty, 3, "the numeric variable survives an untouched submit");
785
+ });
786
+
787
+ test("buildField a THROWING image resolver skips the image instead of failing the form", (t) => {
788
+ const created: FakeElement[] = [];
789
+ t.after(installFakeDom(created));
790
+ // The pages runtime's apiUrl resolver does `new URL(u.slice(1), APP_BASE)`,
791
+ // which THROWS `Invalid URL` on an allowlist-accepted app-relative path that is
792
+ // not URL-path-shaped (e.g. "/http://" — the leading slash passes the raw
793
+ // allowlist, the stripped remainder does not parse). An uncaught throw escapes
794
+ // buildField and buildEngineForm fails to render the WHOLE task form. A
795
+ // resolver failure must reject just that image (null), exactly like a resolver
796
+ // that returns a rejected URL.
797
+ const apiUrl = (u: string) =>
798
+ u.startsWith("/") && !u.startsWith("//") ? new URL(u.slice(1), new URL("../", import.meta.url)).toString() : u;
799
+ // "/http://" is the throwing case: stripping the leading slash leaves
800
+ // "http://", which `new URL` cannot parse. ("/http://evil.example.com/x.png"
801
+ // strips to a VALID absolute http URL — the resolver succeeds and the
802
+ // post-resolution allowlist passes it as http:; that is correct, not a throw.)
803
+ for (const source of ["/http://"]) {
804
+ assert.equal(
805
+ buildField({ type: "image", source }, {}, { resolveUrl: apiUrl }),
806
+ null,
807
+ `literal ${JSON.stringify(source)}: a throwing resolver rejects the image`,
808
+ );
809
+ assert.equal(
810
+ buildField({ type: "image", source: "=u" }, { u: source }, { resolveUrl: apiUrl }),
811
+ null,
812
+ `variable holding ${JSON.stringify(source)}: a throwing resolver rejects the image`,
813
+ );
814
+ }
815
+ // An unconditionally-throwing resolver (any failure mode, not just Invalid URL)
816
+ // is contained the same way, and does not take the neighbouring fields down:
817
+ const alwaysThrows = () => {
818
+ throw new Error("resolver exploded");
819
+ };
820
+ assert.equal(buildField({ type: "image", source: "/app/t.png" }, {}, { resolveUrl: alwaysThrows }), null);
821
+ const text = buildField({ type: "textfield", key: "note" }, { note: "still here" });
822
+ const input = created.find((n) => n.tagName === "INPUT")!;
823
+ assert.equal(input.value, "still here", "a rejected image does not stop the next field rendering");
824
+ assert.deepEqual(text!.read!(), { key: "note", value: "still here" });
825
+ assert.equal(created.filter((n) => n.tagName === "IMG").length, 0, "no <img> was rendered for a failed resolution");
826
+ });