paseo-plugin-genui 0.1.6 → 0.1.8

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.
@@ -0,0 +1,114 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { compile, dataKey, evaluate, initialState } from "./core";
3
+
4
+ // JavaScript that agents write without thinking; each must compile and give the value JavaScript gives.
5
+ const PRELUDE = [
6
+ 'xs = [{n: "a", v: 3, tags: ["t", "u"]}, {n: "b", v: 1, tags: []}]',
7
+ "o = {a: 1, b: 2}",
8
+ 's = "Hello world"',
9
+ ].join("\n");
10
+
11
+ const CASES: Record<string, [source: string, expected: string]> = {
12
+ // list methods
13
+ reduce: ["xs.reduce((t, x) => t + x.v, 0)", "4"],
14
+ "reduce without a start": ["[1, 2, 3].reduce((a, b) => a + b)", "6"],
15
+ flatMap: ['xs.flatMap(x => x.tags).join("|")', "t|u"],
16
+ flat: ['[[1], [2, [3]]].flat().length', "3"],
17
+ at: ["xs.at(-1).n", "b"],
18
+ findIndex: ["xs.findIndex(x => x.v == 1)", "1"],
19
+ lastIndexOf: ["[1, 2, 1].lastIndexOf(1)", "2"],
20
+ reverse: ['[1, 2].reverse().join("")', "21"],
21
+ "reverse copies": ['[1, 2].reverse().concat(xs.map(x => x.n)).join("")', "21ab"],
22
+ // text and number methods
23
+ padStart: ['"5".padStart(2, "0")', "05"],
24
+ padEnd: ['"ab".padEnd(4, ".")', "ab.."],
25
+ substring: ["s.substring(0, 5)", "Hello"],
26
+ charAt: ["s.charAt(1)", "e"],
27
+ repeat: ['"=".repeat(3)', "==="],
28
+ trimStart: ['" a ".trimStart() + "|"', "a |"],
29
+ trimEnd: ['"|" + " a ".trimEnd()', "| a"],
30
+ "indexOf on text": ['s.indexOf("w")', "6"],
31
+ "lastIndexOf on text": ['s.lastIndexOf("o")', "7"],
32
+ "at on text": ["s.at(-1)", "d"],
33
+ toLocaleString: ["(1234567.891).toLocaleString()", "1,234,567.891"],
34
+ toFixed: ["(1/3).toFixed(2)", "0.33"],
35
+ // conversion and globals
36
+ Number: ['Number("3") + 1', "4"],
37
+ "Number of grouped text": ['Number("1,234")', "1234"],
38
+ "Number of junk": ['Number("x") ?? "none"', "none"],
39
+ String: ["String(3) + String(4)", "34"],
40
+ toString: ['(12).toString() + (255).toString(16) + true.toString() + "x".toString()', "12fftruex"],
41
+ parseInt: ['parseInt("42px") + parseInt("ff", 16)', "297"],
42
+ "parseInt of junk": ['parseInt("x") ?? "none"', "none"],
43
+ parseFloat: ['parseFloat("3.5 kg") * 2', "7"],
44
+ "computed object key": ['["a", "b"].reduce((acc, k) => ({...acc, [k]: 1}), {}).b', "1"],
45
+ "JSON.stringify": ["JSON.stringify(o)", '{"a":1,"b":2}'],
46
+ "Date.now": ["Date.now() > 0", "true"],
47
+ "Math and Object": ['Math.max(...[1, 5, 2]) + Object.keys(o).length', "7"],
48
+ // syntax
49
+ "optional chaining": ["xs[0]?.n", "a"],
50
+ "optional method on null": ['o.z?.toUpperCase() ?? "none"', "none"],
51
+ "optional index": ['o.z?.[0] ?? "none"', "none"],
52
+ "list spread": ["[...xs, {n: 'c'}].length", "3"],
53
+ "object spread": ["{...o, c: 3}.c + {...o, a: 9}.a", "12"],
54
+ "argument spread": ["max(...xs.map(x => x.v))", "3"],
55
+ "shorthand property": ['f = n => ({n})\nf("k").n', "k"],
56
+ "block arrow": ['xs.map(x => { return x.n }).join("")', "ab"],
57
+ "block arrow with const": ['xs.map(x => { const d = x.v * 2; return `${x.n}${d}` }).join(",")', "a6,b2"],
58
+ "object destructuring": ['xs.map(({n}) => n).join("")', "ab"],
59
+ "destructuring with rename and default": ['xs.map(({n: name, w = 0}) => name + w).join("")', "a0b0"],
60
+ "array destructuring of entries": ['entries(o).map(([k, v]) => k + v).join(",")', "a1,b2"],
61
+ "nested destructuring": ['[{p: {q: 1}}].map(({p: {q}}) => q).join("")', "1"],
62
+ "destructuring in const": ['f = x => { const {n, v} = x; return n + v }\nf(xs[0])', "a3"],
63
+ typeof: ['typeof 3 + typeof "x" + typeof null', "numberstringobject"],
64
+ in: ['("a" in o) + "," + ("z" in o) + "," + (1 in xs)', "true,false,true"],
65
+ // already supported; kept so they stay that way
66
+ "named function in map": ['f = (x, i) => i\nxs.map(f).join("")', "01"],
67
+ "strict equality": ["1 === 1 && 1 !== 2", "true"],
68
+ "includes on a list": ['xs.map(x => x.n).includes("a")', "true"],
69
+ "nested template": ['`${xs.length} items: ${xs.map(x => `${x.n}`).join()}`', "2 items: a,b"],
70
+ nullish: ['o.z ?? "none"', "none"],
71
+ };
72
+
73
+ describe("JavaScript idioms in the UI language", () => {
74
+ for (const [name, [source, expected]] of Object.entries(CASES)) {
75
+ it(name, () => {
76
+ const lines = source.split("\n");
77
+ const body = lines.pop()!;
78
+ const compiled = compile([PRELUDE, ...lines, `Text(${body})`].join("\n"));
79
+ expect(compiled.diagnostics.filter((d) => d.severity !== "warning")).toEqual([]);
80
+ const { roots, diagnostics } = evaluate(compiled.program!, initialState(compiled.program!));
81
+ expect(diagnostics).toEqual([]);
82
+ expect(roots[0].props.text).toBe(expected);
83
+ });
84
+ }
85
+
86
+ it("explains what a block body cannot hold", () => {
87
+ const { diagnostics } = compile("Text([1].map(x => { if (x) return 1 }).join())");
88
+ expect(diagnostics[0].message).toContain("`{ }` function body holds `const name = ...` lines and one `return ...`");
89
+ });
90
+
91
+ it("rejects spreading into a component", () => {
92
+ const { diagnostics } = compile('Row(...["a"])');
93
+ expect(diagnostics[0].message).toContain("cannot spread into Row");
94
+ });
95
+
96
+ it("does not copy a loaded object's own __proto__ key when spreading it", () => {
97
+ const compiled = compile('o = {...Load("d.json"), b: 2}\nKeyValue(o)');
98
+ const loaded = JSON.parse('{"__proto__": {"polluted": "yes"}, "a": 1}');
99
+ const data = new Map([[dataKey({ path: "d.json", raw: false }), { ok: true as const, value: loaded }]]);
100
+ const { roots } = evaluate(compiled.program!, initialState(compiled.program!, data), data);
101
+ const pairs = roots[0].props.pairs as object;
102
+ expect(Object.keys(pairs)).toEqual(["a", "b"]);
103
+ expect(Object.getPrototypeOf(pairs)).toBe(Object.prototype);
104
+ });
105
+
106
+ it("caps repeat and padStart at the text limit", () => {
107
+ const compiled = compile('Text("ab".repeat(100000))\nText("".padStart(200000))');
108
+ const { diagnostics } = evaluate(compiled.program!, initialState(compiled.program!));
109
+ expect(diagnostics.map((d) => d.message)).toEqual([
110
+ "text longer than 100000 characters",
111
+ "text longer than 100000 characters",
112
+ ]);
113
+ });
114
+ });
@@ -8,5 +8,13 @@ export const genuiSettings = defineSettings({
8
8
  schema: z.object({
9
9
  autoInstructions: z.boolean().default(true),
10
10
  webLookups: z.boolean().default(true),
11
+ extraFolders: z.string().default(""),
11
12
  }),
12
13
  });
14
+
15
+ export function parseFolders(value: string): string[] {
16
+ return value
17
+ .split(/[,\n]/)
18
+ .map((folder) => folder.trim())
19
+ .filter((folder) => folder !== "");
20
+ }