@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.62
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/assets/pano-bootstrap.css +17903 -0
- package/bin/__tests__/new/e2e.test.js +111 -0
- package/bin/__tests__/new/helpers.js +76 -0
- package/bin/__tests__/new/new.test.js +472 -0
- package/bin/new.js +530 -0
- package/bin/pano-plugin.js +162 -0
- package/bin/templates/plugin/README.md +30 -0
- package/bin/templates/plugin/_gitignore +15 -0
- package/bin/templates/plugin/build.gradle.kts +389 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/bin/templates/plugin/gradle.properties +55 -0
- package/bin/templates/plugin/gradlew +249 -0
- package/bin/templates/plugin/gradlew.bat +92 -0
- package/bin/templates/plugin/package.json.tpl +14 -0
- package/bin/templates/plugin/rollup.config.js +2 -0
- package/bin/templates/plugin/settings.gradle.kts +5 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
- package/bin/templates/plugin/src/main/resources/config.conf +1 -0
- package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
- package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
- package/bin/templates/plugin/src/main/resources/logo.png +0 -0
- package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
- package/bin/ui.js +96 -0
- package/package.json +48 -4
- package/src/config.js +191 -0
- package/src/controller/__tests__/controller.test.js +647 -0
- package/src/controller/index.js +604 -0
- package/src/dev.js +208 -0
- package/src/index.js +22 -0
- package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
- package/src/rollup/__tests__/controllers/fixture.js +143 -0
- package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
- package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
- package/src/rollup/__tests__/preset/preset.test.js +418 -0
- package/src/rollup/__tests__/rules/_build.js +39 -0
- package/src/rollup/__tests__/rules/_fixture.js +83 -0
- package/src/rollup/__tests__/rules/helpers.test.js +148 -0
- package/src/rollup/__tests__/rules/rules.test.js +259 -0
- package/src/rollup/__tests__/rules/stamp.test.js +113 -0
- package/src/rollup/__tests__/samples/samples.test.js +481 -0
- package/src/rollup/__tests__/seams/config.test.js +78 -0
- package/src/rollup/__tests__/seams/lock.test.js +124 -0
- package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
- package/src/rollup/__tests__/seams/stubs.test.js +52 -0
- package/src/rollup/__tests__/views/helpers.js +82 -0
- package/src/rollup/__tests__/views/views.test.js +522 -0
- package/src/rollup/__tests__/widgets/helpers.js +117 -0
- package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
- package/src/rollup/cli-build.js +90 -0
- package/src/rollup/cli-check.js +280 -0
- package/src/rollup/cli-classes.js +75 -0
- package/src/rollup/client-gen.js +67 -0
- package/src/rollup/controllers.js +434 -0
- package/src/rollup/entry.js +250 -0
- package/src/rollup/helpers.js +193 -0
- package/src/rollup/index.js +601 -0
- package/src/rollup/integrate.js +205 -0
- package/src/rollup/lock.js +230 -0
- package/src/rollup/meta.js +479 -0
- package/src/rollup/rules.js +950 -0
- package/src/rollup/samples.js +667 -0
- package/src/rollup/stamp.js +410 -0
- package/src/rollup/types.js +248 -0
- package/src/rollup/views.js +1205 -0
- package/src/rollup/widgets.js +1000 -0
- package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
- package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
- package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
- package/src/styles/__tests__/fallback/plugins.test.js +301 -0
- package/src/styles/__tests__/lint/check-styles.test.js +423 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
- package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
- package/src/styles/bootstrap-classes.js +155 -0
- package/src/styles/check-styles.js +626 -0
- package/src/styles/fallback-css.js +1182 -0
- package/src/styles/plugin-css.js +237 -0
- package/src/styles/scan.js +206 -0
- package/src/styles/semantic-classes.js +436 -0
- package/src/styles/tokens.map.js +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import {
|
|
5
|
+
CSRF_HEADER_NAME,
|
|
6
|
+
createFetchHost,
|
|
7
|
+
createSampleController,
|
|
8
|
+
createState,
|
|
9
|
+
defineController,
|
|
10
|
+
derive,
|
|
11
|
+
nullHost,
|
|
12
|
+
} from '../index.js';
|
|
13
|
+
|
|
14
|
+
const SOURCE_FILE = fileURLToPath(new URL('../index.js', import.meta.url));
|
|
15
|
+
const SDK_VARIABLES = fileURLToPath(new URL('../../../../sdk/core/js/variables.js', import.meta.url));
|
|
16
|
+
|
|
17
|
+
const browserHost = { ...nullHost, browser: true };
|
|
18
|
+
|
|
19
|
+
describe('source constraints', () => {
|
|
20
|
+
test('the file has no import statement, dynamic import or require', () => {
|
|
21
|
+
const source = readFileSync(SOURCE_FILE, 'utf8');
|
|
22
|
+
const code = source.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
|
|
23
|
+
expect(code).not.toMatch(/^\s*import\b/m);
|
|
24
|
+
expect(code).not.toMatch(/\bimport\s*\(/);
|
|
25
|
+
expect(code).not.toMatch(/\bimport\.meta\b/);
|
|
26
|
+
expect(code).not.toMatch(/\brequire\s*\(/);
|
|
27
|
+
expect(code).not.toMatch(/\bfrom\s+['"]/);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('the CSRF header constant equals CSRF_HEADER of the sdk', () => {
|
|
31
|
+
const sdk = readFileSync(SDK_VARIABLES, 'utf8');
|
|
32
|
+
const match = sdk.match(/export const CSRF_HEADER\s*=\s*['"]([^'"]+)['"]/);
|
|
33
|
+
expect(match).not.toBeNull();
|
|
34
|
+
expect(CSRF_HEADER_NAME).toBe(match[1]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('createState', () => {
|
|
39
|
+
test('satisfies the store contract: get, subscribe runs once synchronously', () => {
|
|
40
|
+
const s = createState({ n: 1 });
|
|
41
|
+
const seen = [];
|
|
42
|
+
const off = s.subscribe((v) => seen.push(v));
|
|
43
|
+
expect(seen).toEqual([{ n: 1 }]);
|
|
44
|
+
expect(s.get()).toEqual({ n: 1 });
|
|
45
|
+
off();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('set notifies synchronously and only when !Object.is', () => {
|
|
49
|
+
const s = createState(1);
|
|
50
|
+
const seen = [];
|
|
51
|
+
s.subscribe((v) => seen.push(v));
|
|
52
|
+
s.set(1);
|
|
53
|
+
expect(seen).toEqual([1]);
|
|
54
|
+
s.set(2);
|
|
55
|
+
expect(seen).toEqual([1, 2]);
|
|
56
|
+
s.set(2);
|
|
57
|
+
expect(seen).toEqual([1, 2]);
|
|
58
|
+
s.update((v) => v + 1);
|
|
59
|
+
expect(seen).toEqual([1, 2, 3]);
|
|
60
|
+
const o = {};
|
|
61
|
+
s.set(o);
|
|
62
|
+
s.set(o);
|
|
63
|
+
expect(seen.length).toBe(4);
|
|
64
|
+
s.set(NaN);
|
|
65
|
+
s.set(NaN);
|
|
66
|
+
expect(seen.length).toBe(5);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('unsubscribe stops notifications and is idempotent', () => {
|
|
70
|
+
const s = createState(0);
|
|
71
|
+
const seen = [];
|
|
72
|
+
const off = s.subscribe((v) => seen.push(v));
|
|
73
|
+
off();
|
|
74
|
+
off();
|
|
75
|
+
s.set(1);
|
|
76
|
+
expect(seen).toEqual([0]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('a set made inside a subscriber is delivered in order', () => {
|
|
80
|
+
const s = createState(0);
|
|
81
|
+
const a = [];
|
|
82
|
+
const b = [];
|
|
83
|
+
s.subscribe((v) => {
|
|
84
|
+
a.push(v);
|
|
85
|
+
if (v === 1) s.set(2);
|
|
86
|
+
});
|
|
87
|
+
s.subscribe((v) => b.push(v));
|
|
88
|
+
s.set(1);
|
|
89
|
+
expect(a).toEqual([0, 1, 2]);
|
|
90
|
+
expect(b).toEqual([0, 1, 2]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('works with the Svelte store contract shape', () => {
|
|
94
|
+
const s = createState('x');
|
|
95
|
+
expect(typeof s.subscribe).toBe('function');
|
|
96
|
+
expect(typeof s.subscribe(() => {})).toBe('function');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('derive', () => {
|
|
101
|
+
test('one source', () => {
|
|
102
|
+
const a = createState(2);
|
|
103
|
+
const d = derive(a, (v) => v * 2);
|
|
104
|
+
expect(d.get()).toBe(4);
|
|
105
|
+
const seen = [];
|
|
106
|
+
const off = d.subscribe((v) => seen.push(v));
|
|
107
|
+
a.set(3);
|
|
108
|
+
expect(seen).toEqual([4, 6]);
|
|
109
|
+
expect(d.get()).toBe(6);
|
|
110
|
+
off();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('several sources and no recompute when inputs are unchanged', () => {
|
|
114
|
+
const a = createState(1);
|
|
115
|
+
const b = createState(10);
|
|
116
|
+
let runs = 0;
|
|
117
|
+
const d = derive([a, b], ([x, y]) => {
|
|
118
|
+
runs++;
|
|
119
|
+
return { sum: x + y };
|
|
120
|
+
});
|
|
121
|
+
const first = d.get();
|
|
122
|
+
expect(first).toEqual({ sum: 11 });
|
|
123
|
+
expect(d.get()).toBe(first);
|
|
124
|
+
expect(runs).toBe(1);
|
|
125
|
+
const seen = [];
|
|
126
|
+
const off = d.subscribe((v) => seen.push(v.sum));
|
|
127
|
+
b.set(20);
|
|
128
|
+
a.set(2);
|
|
129
|
+
expect(seen).toEqual([11, 21, 22]);
|
|
130
|
+
off();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('connects to its sources on the first subscriber and lets go after the last', () => {
|
|
134
|
+
let subscribed = 0;
|
|
135
|
+
let unsubscribed = 0;
|
|
136
|
+
const src = {
|
|
137
|
+
get: () => 1,
|
|
138
|
+
subscribe(run) {
|
|
139
|
+
subscribed++;
|
|
140
|
+
run(1);
|
|
141
|
+
return () => {
|
|
142
|
+
unsubscribed++;
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
const d = derive(src, (v) => v);
|
|
147
|
+
const o1 = d.subscribe(() => {});
|
|
148
|
+
const o2 = d.subscribe(() => {});
|
|
149
|
+
expect(subscribed).toBe(1);
|
|
150
|
+
o1();
|
|
151
|
+
expect(unsubscribed).toBe(0);
|
|
152
|
+
o2();
|
|
153
|
+
expect(unsubscribed).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('defineController', () => {
|
|
158
|
+
test('validates the spec', () => {
|
|
159
|
+
expect(() => defineController({ name: 'Cart', version: 1 })).toThrow();
|
|
160
|
+
expect(() => defineController({ name: 'cart', version: 0 })).toThrow();
|
|
161
|
+
expect(() => defineController({ name: 'cart', version: 1.5 })).toThrow();
|
|
162
|
+
expect(() => defineController({ name: 'cart', version: 1, scope: 'page' })).toThrow();
|
|
163
|
+
expect(() => defineController({ name: 'cart', version: 1, state: 3 })).toThrow();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test('definition carries name, version, scope default, eager and load', () => {
|
|
167
|
+
const load = async () => ({ ok: 1 });
|
|
168
|
+
const def = defineController({ name: 'cart', version: 2, load });
|
|
169
|
+
expect(def.name).toBe('cart');
|
|
170
|
+
expect(def.version).toBe(2);
|
|
171
|
+
expect(def.scope).toBe('app');
|
|
172
|
+
expect(def.eager).toBe(false);
|
|
173
|
+
expect(def.load).toBe(load);
|
|
174
|
+
expect(defineController({ name: 'x', version: 1, scope: 'instance', eager: true }).scope).toBe('instance');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('create: state, params, initial patch, name with namespace, no actions by default', () => {
|
|
178
|
+
const def = defineController({
|
|
179
|
+
name: 'cart',
|
|
180
|
+
version: 1,
|
|
181
|
+
state: ({ params }) => ({ count: params.start ?? 0, items: [] }),
|
|
182
|
+
});
|
|
183
|
+
const c = def.create(nullHost, { start: 3 }, { items: ['a'] }, { namespace: 'market' });
|
|
184
|
+
expect(c.name).toBe('market/cart');
|
|
185
|
+
expect(c.version).toBe(1);
|
|
186
|
+
expect(c.get()).toEqual({ count: 3, items: ['a'] });
|
|
187
|
+
expect(c.actions).toEqual({});
|
|
188
|
+
expect(def.create(nullHost).name).toBe('cart');
|
|
189
|
+
expect(def.create(nullHost).get()).toEqual({ count: 0, items: [] });
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('state accepts params {} and a spec without state starts with {}', () => {
|
|
193
|
+
expect(defineController({ name: 'format', version: 1 }).create(nullHost).get()).toEqual({});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('actions mutate state through ctx; the snapshot is replaced, never mutated', () => {
|
|
197
|
+
const def = defineController({
|
|
198
|
+
name: 'cart',
|
|
199
|
+
version: 1,
|
|
200
|
+
state: () => ({ count: 0 }),
|
|
201
|
+
actions: (c) => ({
|
|
202
|
+
add: () => {
|
|
203
|
+
c.update((s) => ({ ...s, count: s.count + 1 }));
|
|
204
|
+
return { ok: true };
|
|
205
|
+
},
|
|
206
|
+
}),
|
|
207
|
+
});
|
|
208
|
+
const c = def.create(nullHost);
|
|
209
|
+
const before = c.get();
|
|
210
|
+
const seen = [];
|
|
211
|
+
c.subscribe((s) => seen.push(s.count));
|
|
212
|
+
expect(c.actions.add()).toEqual({ ok: true });
|
|
213
|
+
expect(before.count).toBe(0);
|
|
214
|
+
expect(c.get()).not.toBe(before);
|
|
215
|
+
expect(seen).toEqual([0, 1]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('ctx exposes host and params; set with an equal value does not notify', () => {
|
|
219
|
+
const marker = { ...nullHost, marker: true };
|
|
220
|
+
let got;
|
|
221
|
+
const def = defineController({
|
|
222
|
+
name: 'a',
|
|
223
|
+
version: 1,
|
|
224
|
+
state: () => ({ v: 1 }),
|
|
225
|
+
actions: (c) => {
|
|
226
|
+
got = c;
|
|
227
|
+
return {};
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
const c = def.create(marker, { p: 1 });
|
|
231
|
+
expect(got.host).toBe(marker);
|
|
232
|
+
expect(got.params).toEqual({ p: 1 });
|
|
233
|
+
const seen = [];
|
|
234
|
+
c.subscribe((s) => seen.push(s));
|
|
235
|
+
got.set(got.get());
|
|
236
|
+
expect(seen.length).toBe(1);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('start runs on the first subscriber and its cleanup after the last leaves', () => {
|
|
240
|
+
const log = [];
|
|
241
|
+
const def = defineController({
|
|
242
|
+
name: 'a',
|
|
243
|
+
version: 1,
|
|
244
|
+
start: () => {
|
|
245
|
+
log.push('start');
|
|
246
|
+
return () => log.push('stop');
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
const c = def.create(browserHost);
|
|
250
|
+
expect(log).toEqual([]);
|
|
251
|
+
const o1 = c.subscribe(() => {});
|
|
252
|
+
const o2 = c.subscribe(() => {});
|
|
253
|
+
expect(log).toEqual(['start']);
|
|
254
|
+
o1();
|
|
255
|
+
o1();
|
|
256
|
+
expect(log).toEqual(['start']);
|
|
257
|
+
o2();
|
|
258
|
+
expect(log).toEqual(['start', 'stop']);
|
|
259
|
+
const o3 = c.subscribe(() => {});
|
|
260
|
+
expect(log).toEqual(['start', 'stop', 'start']);
|
|
261
|
+
o3();
|
|
262
|
+
expect(log).toEqual(['start', 'stop', 'start', 'stop']);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('start gets actions and does not run on the server', () => {
|
|
266
|
+
let seenActions = null;
|
|
267
|
+
const def = defineController({
|
|
268
|
+
name: 'a',
|
|
269
|
+
version: 1,
|
|
270
|
+
actions: () => ({ ping: () => 1 }),
|
|
271
|
+
start: ({ actions }) => {
|
|
272
|
+
seenActions = actions;
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
def.create(nullHost).subscribe(() => {})();
|
|
276
|
+
expect(seenActions).toBeNull();
|
|
277
|
+
def.create(browserHost).subscribe(() => {})();
|
|
278
|
+
expect(typeof seenActions.ping).toBe('function');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('what start sets synchronously is in the first value delivered to the subscriber', () => {
|
|
282
|
+
const def = defineController({
|
|
283
|
+
name: 'a',
|
|
284
|
+
version: 1,
|
|
285
|
+
state: () => ({ ready: false }),
|
|
286
|
+
start: ({ update }) => update(() => ({ ready: true })),
|
|
287
|
+
});
|
|
288
|
+
const seen = [];
|
|
289
|
+
def.create(browserHost).subscribe((s) => seen.push(s.ready));
|
|
290
|
+
expect(seen).toEqual([true]);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test('eager: started at creation in the browser, not by subscribers, cleanup on destroy', () => {
|
|
294
|
+
const log = [];
|
|
295
|
+
const def = defineController({
|
|
296
|
+
name: 'a',
|
|
297
|
+
version: 1,
|
|
298
|
+
eager: true,
|
|
299
|
+
start: () => {
|
|
300
|
+
log.push('start');
|
|
301
|
+
return () => log.push('stop');
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
expect(log).toEqual([]);
|
|
305
|
+
const server = def.create(nullHost);
|
|
306
|
+
expect(log).toEqual([]);
|
|
307
|
+
server.destroy();
|
|
308
|
+
const c = def.create(browserHost);
|
|
309
|
+
expect(log).toEqual(['start']);
|
|
310
|
+
const off = c.subscribe(() => {});
|
|
311
|
+
off();
|
|
312
|
+
expect(log).toEqual(['start']);
|
|
313
|
+
c.destroy();
|
|
314
|
+
expect(log).toEqual(['start', 'stop']);
|
|
315
|
+
c.destroy();
|
|
316
|
+
expect(log).toEqual(['start', 'stop']);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
test('destroy stops a running non-eager controller and drops its subscribers', () => {
|
|
320
|
+
const log = [];
|
|
321
|
+
const def = defineController({
|
|
322
|
+
name: 'a',
|
|
323
|
+
version: 1,
|
|
324
|
+
state: () => ({ n: 0 }),
|
|
325
|
+
actions: (c) => ({ inc: () => c.update((s) => ({ n: s.n + 1 })) }),
|
|
326
|
+
start: () => () => log.push('stop'),
|
|
327
|
+
});
|
|
328
|
+
const c = def.create(browserHost);
|
|
329
|
+
const seen = [];
|
|
330
|
+
c.subscribe((s) => seen.push(s.n));
|
|
331
|
+
c.destroy();
|
|
332
|
+
expect(log).toEqual(['stop']);
|
|
333
|
+
c.actions.inc();
|
|
334
|
+
expect(seen).toEqual([0]);
|
|
335
|
+
c.subscribe(() => {})();
|
|
336
|
+
expect(log).toEqual(['stop']);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
test('ctx.use resolves siblings through the registry callback', () => {
|
|
340
|
+
const format = defineController({
|
|
341
|
+
name: 'format',
|
|
342
|
+
version: 1,
|
|
343
|
+
actions: () => ({ money: (n) => `$${n}` }),
|
|
344
|
+
}).create(nullHost, {}, undefined, { namespace: 'market' });
|
|
345
|
+
const asked = [];
|
|
346
|
+
const cart = defineController({
|
|
347
|
+
name: 'cart',
|
|
348
|
+
version: 1,
|
|
349
|
+
actions: (c) => ({ label: (n) => c.use('format').actions.money(n) }),
|
|
350
|
+
}).create(nullHost, {}, undefined, {
|
|
351
|
+
namespace: 'market',
|
|
352
|
+
use: (name) => {
|
|
353
|
+
asked.push(name);
|
|
354
|
+
return format;
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
expect(cart.actions.label(5)).toBe('$5');
|
|
358
|
+
expect(asked).toEqual(['format']);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test('ctx.use without a registry throws a clear error', () => {
|
|
362
|
+
const def = defineController({
|
|
363
|
+
name: 'cart',
|
|
364
|
+
version: 1,
|
|
365
|
+
actions: (c) => ({ x: () => c.use('format') }),
|
|
366
|
+
});
|
|
367
|
+
expect(() => def.create(nullHost).actions.x()).toThrow(/outside a registry/);
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
function fakeFetch(handler) {
|
|
372
|
+
const calls = [];
|
|
373
|
+
const fn = async (url, init) => {
|
|
374
|
+
calls.push({ url, init });
|
|
375
|
+
return handler(url, init);
|
|
376
|
+
};
|
|
377
|
+
fn.calls = calls;
|
|
378
|
+
return fn;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const jsonResponse = (body, status = 200) =>
|
|
382
|
+
new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
|
383
|
+
|
|
384
|
+
describe('createFetchHost', () => {
|
|
385
|
+
test('a plugin path goes to /api/plugins (unversioned), a core path under /api/v1', async () => {
|
|
386
|
+
const f = fakeFetch(() => jsonResponse({}));
|
|
387
|
+
const host = createFetchHost({ baseUrl: 'https://example.com', fetch: f });
|
|
388
|
+
await host.request({ path: '/api/plugins/pano-plugin-market/store/products' });
|
|
389
|
+
await host.request({ path: '/plugins/pano-plugin-market/panel/products' });
|
|
390
|
+
await host.request({ path: '/plugins/pano-plugin-market/_/translations/en' });
|
|
391
|
+
await host.request({ path: '/posts' });
|
|
392
|
+
expect(f.calls.map((c) => c.url)).toEqual([
|
|
393
|
+
'https://example.com/api/plugins/pano-plugin-market/store/products',
|
|
394
|
+
'https://example.com/api/plugins/pano-plugin-market/panel/products',
|
|
395
|
+
'https://example.com/api/v1/plugins/pano-plugin-market/_/translations/en',
|
|
396
|
+
'https://example.com/api/v1/posts',
|
|
397
|
+
]);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
test('request builds the URL under /api/v1 with query and returns the body', async () => {
|
|
401
|
+
const f = fakeFetch(() => jsonResponse({ items: [1], page: { total: 1 } }));
|
|
402
|
+
const host = createFetchHost({ baseUrl: 'https://example.com/', fetch: f });
|
|
403
|
+
const body = await host.request({ path: '/store/products', query: { q: 'a b', tag: ['x', 'y'], skip: undefined, n: 0 } });
|
|
404
|
+
expect(body).toEqual({ items: [1], page: { total: 1 } });
|
|
405
|
+
expect(f.calls[0].url).toBe('https://example.com/api/v1/store/products?q=a+b&tag=x&tag=y&n=0');
|
|
406
|
+
expect(f.calls[0].init.method).toBe('GET');
|
|
407
|
+
expect(f.calls[0].init.credentials).toBe('include');
|
|
408
|
+
expect(f.calls[0].init.body).toBeUndefined();
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
test('a proxy prefix works as baseUrl', async () => {
|
|
412
|
+
const f = fakeFetch(() => jsonResponse({}));
|
|
413
|
+
await createFetchHost({ baseUrl: '/pano', fetch: f }).request({ path: '/posts' });
|
|
414
|
+
expect(f.calls[0].url).toBe('/pano/api/v1/posts');
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
test('mutations send JSON, and the CSRF header when a token exists', async () => {
|
|
418
|
+
const f = fakeFetch(() => jsonResponse({ ok: 1 }));
|
|
419
|
+
const host = createFetchHost({ baseUrl: '', fetch: f, getCsrf: () => 'tok' });
|
|
420
|
+
await host.request({ method: 'post', path: '/cart', body: { a: 1 } });
|
|
421
|
+
const init = f.calls[0].init;
|
|
422
|
+
expect(init.method).toBe('POST');
|
|
423
|
+
expect(init.body).toBe('{"a":1}');
|
|
424
|
+
expect(init.headers['Content-Type']).toBe('application/json');
|
|
425
|
+
expect(init.headers[CSRF_HEADER_NAME]).toBe('tok');
|
|
426
|
+
await host.request({ path: '/cart' });
|
|
427
|
+
expect(f.calls[1].init.headers[CSRF_HEADER_NAME]).toBeUndefined();
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
test('getToken sends a bearer header and no cookies', async () => {
|
|
431
|
+
const f = fakeFetch(() => jsonResponse({}));
|
|
432
|
+
const host = createFetchHost({ baseUrl: '', fetch: f, getToken: () => 'jwt' });
|
|
433
|
+
await host.request({ path: '/me' });
|
|
434
|
+
expect(f.calls[0].init.headers.Authorization).toBe('Bearer jwt');
|
|
435
|
+
expect(f.calls[0].init.credentials).toBeUndefined();
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
test('FormData bodies pass through and custom headers win', async () => {
|
|
439
|
+
const f = fakeFetch(() => jsonResponse({}));
|
|
440
|
+
const host = createFetchHost({ baseUrl: '', fetch: f });
|
|
441
|
+
const form = new FormData();
|
|
442
|
+
form.append('a', 'b');
|
|
443
|
+
await host.request({ method: 'POST', path: '/up', body: form, headers: { 'X-Extra': '1' } });
|
|
444
|
+
expect(f.calls[0].init.body).toBe(form);
|
|
445
|
+
expect(f.calls[0].init.headers['Content-Type']).toBeUndefined();
|
|
446
|
+
expect(f.calls[0].init.headers['X-Extra']).toBe('1');
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
test('non-2xx returns the envelope unchanged', async () => {
|
|
450
|
+
const envelope = { error: { code: 'INVALID_CART', details: { lineErrors: { a: ['OUT_OF_STOCK'] } } } };
|
|
451
|
+
const host = createFetchHost({ baseUrl: '', fetch: fakeFetch(() => jsonResponse(envelope, 400)) });
|
|
452
|
+
expect(await host.request({ path: '/x' })).toEqual(envelope);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
test('non-2xx without an envelope still resolves a code', async () => {
|
|
456
|
+
const host = createFetchHost({ baseUrl: '', fetch: fakeFetch(() => new Response('<html>bad gateway</html>', { status: 502 })) });
|
|
457
|
+
expect(await host.request({ path: '/x' })).toEqual({ error: { code: 'UNKNOWN_ERROR', details: { status: 502 } } });
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
test('network failure resolves NETWORK_ERROR and never rejects', async () => {
|
|
461
|
+
const host = createFetchHost({
|
|
462
|
+
baseUrl: '',
|
|
463
|
+
fetch: async () => {
|
|
464
|
+
throw new TypeError('Failed to fetch');
|
|
465
|
+
},
|
|
466
|
+
});
|
|
467
|
+
expect(await host.request({ path: '/x' })).toEqual({ error: { code: 'NETWORK_ERROR' } });
|
|
468
|
+
const broken = createFetchHost({
|
|
469
|
+
baseUrl: '',
|
|
470
|
+
fetch: async () => ({ ok: true, status: 200, text: async () => { throw new Error('reset'); } }),
|
|
471
|
+
});
|
|
472
|
+
expect(await broken.request({ path: '/x' })).toEqual({ error: { code: 'NETWORK_ERROR' } });
|
|
473
|
+
expect(await createFetchHost({ baseUrl: '', getToken: () => { throw new Error('x'); }, fetch: async () => jsonResponse({}) }).request({ path: '/x' })).toEqual({ error: { code: 'NETWORK_ERROR' } });
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test('empty 2xx body is {} and an unparsable 2xx body is INVALID_RESPONSE', async () => {
|
|
477
|
+
expect(await createFetchHost({ baseUrl: '', fetch: fakeFetch(() => new Response(null, { status: 204 })) }).request({ method: 'DELETE', path: '/x' })).toEqual({});
|
|
478
|
+
expect(await createFetchHost({ baseUrl: '', fetch: fakeFetch(() => new Response('nope', { status: 200 })) }).request({ path: '/x' })).toEqual({ error: { code: 'INVALID_RESPONSE' } });
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test('blob: ok returns the Blob, failure returns the envelope', async () => {
|
|
482
|
+
const ok = createFetchHost({ baseUrl: '', fetch: fakeFetch(() => new Response('PDF', { status: 200 })) });
|
|
483
|
+
const blob = await ok.request({ path: '/invoice', blob: true });
|
|
484
|
+
expect(blob instanceof Blob).toBe(true);
|
|
485
|
+
expect(await blob.text()).toBe('PDF');
|
|
486
|
+
const bad = createFetchHost({ baseUrl: '', fetch: fakeFetch(() => jsonResponse({ error: { code: 'NOT_FOUND' } }, 404)) });
|
|
487
|
+
expect(await bad.request({ path: '/invoice', blob: true })).toEqual({ error: { code: 'NOT_FOUND' } });
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
test('session, locale, t, toast, feature, urls, storage, now, navigate', () => {
|
|
491
|
+
const toasts = [];
|
|
492
|
+
const nav = [];
|
|
493
|
+
const store = { getItem: () => null };
|
|
494
|
+
const host = createFetchHost({
|
|
495
|
+
baseUrl: 'https://x.test',
|
|
496
|
+
browser: true,
|
|
497
|
+
getCsrf: () => 'c',
|
|
498
|
+
getSession: () => ({ user: { id: 1 } }),
|
|
499
|
+
locale: () => 'tr',
|
|
500
|
+
messages: { 'plugins.pano-plugin-market.hello': 'Merhaba {name}' },
|
|
501
|
+
features: ['login-return-url'],
|
|
502
|
+
onToast: (key, o) => toasts.push([key, o]),
|
|
503
|
+
onNavigate: (url, o) => nav.push([url, o]),
|
|
504
|
+
storage: { local: store },
|
|
505
|
+
now: () => 42,
|
|
506
|
+
});
|
|
507
|
+
expect(host.baseUrl).toBe('https://x.test');
|
|
508
|
+
expect(host.browser).toBe(true);
|
|
509
|
+
expect(host.session()).toEqual({ user: { id: 1 }, csrfToken: 'c' });
|
|
510
|
+
expect(host.locale()).toBe('tr');
|
|
511
|
+
expect(host.t('plugins.pano-plugin-market.hello', { name: 'Ali' })).toBe('Merhaba Ali');
|
|
512
|
+
expect(host.t('missing.key')).toBe('missing.key');
|
|
513
|
+
host.toast('plugins.pano-plugin-market.hello', { variant: 'success', values: { name: 'Veli' } });
|
|
514
|
+
expect(toasts[0][0]).toBe('plugins.pano-plugin-market.hello');
|
|
515
|
+
expect(toasts[0][1]).toMatchObject({ variant: 'success', message: 'Merhaba Veli' });
|
|
516
|
+
expect(host.feature('login-return-url')).toBe(true);
|
|
517
|
+
expect(host.feature('other')).toBe(false);
|
|
518
|
+
expect(host.loginUrl()).toBe('/login');
|
|
519
|
+
expect(host.loginUrl('/store/checkout?a=1')).toBe('/login?redirect=%2Fstore%2Fcheckout%3Fa%3D1');
|
|
520
|
+
expect(host.registerUrl('/x')).toBe('/register?redirect=%2Fx');
|
|
521
|
+
expect(host.storage('local')).toBe(store);
|
|
522
|
+
expect(host.storage('session')).toBeNull();
|
|
523
|
+
expect(host.now()).toBe(42);
|
|
524
|
+
host.navigate('/a', { replace: true });
|
|
525
|
+
expect(nav).toEqual([['/a', { replace: true }]]);
|
|
526
|
+
expect(typeof host.onSession(() => {})).toBe('function');
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
test('defaults are safe on the server: guest, no storage, no toast target', () => {
|
|
530
|
+
const host = createFetchHost({ baseUrl: '', browser: false });
|
|
531
|
+
expect(host.session()).toEqual({ user: null, csrfToken: null });
|
|
532
|
+
expect(host.storage('local')).toBeNull();
|
|
533
|
+
expect(host.locale()).toBeUndefined();
|
|
534
|
+
expect(() => host.toast('k')).not.toThrow();
|
|
535
|
+
expect(() => host.navigate('/x')).not.toThrow();
|
|
536
|
+
expect(host.feature('x')).toBe(false);
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
test('a host drives a controller end to end', async () => {
|
|
540
|
+
const f = fakeFetch(() => jsonResponse({ items: [{ id: 'a' }, { id: 'b' }] }));
|
|
541
|
+
const def = defineController({
|
|
542
|
+
name: 'products',
|
|
543
|
+
version: 1,
|
|
544
|
+
state: () => ({ items: [], error: null }),
|
|
545
|
+
actions: (c) => ({
|
|
546
|
+
async load() {
|
|
547
|
+
const body = await c.host.request({ path: '/products' });
|
|
548
|
+
if (body.error) return { ok: false, code: body.error.code };
|
|
549
|
+
c.update((s) => ({ ...s, items: body.items }));
|
|
550
|
+
return { ok: true };
|
|
551
|
+
},
|
|
552
|
+
}),
|
|
553
|
+
});
|
|
554
|
+
const c = def.create(createFetchHost({ baseUrl: 'https://x.test', fetch: f }));
|
|
555
|
+
expect(await c.actions.load()).toEqual({ ok: true });
|
|
556
|
+
expect(c.get().items.length).toBe(2);
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
describe('nullHost', () => {
|
|
561
|
+
test('guest session and request resolves NULL_HOST', async () => {
|
|
562
|
+
expect(nullHost.session()).toEqual({ user: null, csrfToken: null });
|
|
563
|
+
expect(nullHost.browser).toBe(false);
|
|
564
|
+
expect(await nullHost.request({ path: '/x' })).toEqual({ error: { code: 'NULL_HOST' } });
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
test('the rest are safe no-ops', () => {
|
|
568
|
+
expect(nullHost.locale()).toBeUndefined();
|
|
569
|
+
expect(nullHost.t('a.b')).toBe('a.b');
|
|
570
|
+
expect(nullHost.storage('local')).toBeNull();
|
|
571
|
+
expect(nullHost.feature('x')).toBe(false);
|
|
572
|
+
expect(nullHost.loginUrl('/x')).toBe('/login');
|
|
573
|
+
expect(nullHost.registerUrl()).toBe('/register');
|
|
574
|
+
expect(typeof nullHost.now()).toBe('number');
|
|
575
|
+
expect(() => nullHost.toast('k')).not.toThrow();
|
|
576
|
+
expect(() => nullHost.navigate('/x')).not.toThrow();
|
|
577
|
+
expect(nullHost.onSession(() => {})()).toBeUndefined();
|
|
578
|
+
expect(Object.isFrozen(nullHost)).toBe(true);
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
test('a controller can be created on it for key enumeration', () => {
|
|
582
|
+
const def = defineController({
|
|
583
|
+
name: 'cart',
|
|
584
|
+
version: 1,
|
|
585
|
+
state: () => ({ count: 0, items: [] }),
|
|
586
|
+
actions: () => ({ add() {}, clear() {} }),
|
|
587
|
+
});
|
|
588
|
+
const c = def.create(nullHost, {});
|
|
589
|
+
expect(Object.keys(c.get())).toEqual(['count', 'items']);
|
|
590
|
+
expect(Object.keys(c.actions)).toEqual(['add', 'clear']);
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
describe('createSampleController', () => {
|
|
595
|
+
const def = defineController({
|
|
596
|
+
name: 'cart',
|
|
597
|
+
version: 3,
|
|
598
|
+
state: () => ({ count: 0, items: [] }),
|
|
599
|
+
actions: (c) => ({
|
|
600
|
+
add: (id) => c.update((s) => ({ ...s, count: s.count + 1 })),
|
|
601
|
+
clear: () => c.set({ count: 0, items: [] }),
|
|
602
|
+
}),
|
|
603
|
+
start: () => {
|
|
604
|
+
throw new Error('must not run');
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
test('fixed state is the initial state with the patch merged over it', () => {
|
|
609
|
+
const c = createSampleController(def, { count: 2 });
|
|
610
|
+
expect(c.get()).toEqual({ count: 2, items: [] });
|
|
611
|
+
const seen = [];
|
|
612
|
+
c.subscribe((s) => seen.push(s.count))();
|
|
613
|
+
expect(seen).toEqual([2]);
|
|
614
|
+
expect(c.name).toBe('cart');
|
|
615
|
+
expect(c.version).toBe(3);
|
|
616
|
+
expect(createSampleController(def, {}, { namespace: 'market' }).name).toBe('market/cart');
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
test('actions record { name, args } and leave the state alone', async () => {
|
|
620
|
+
const calls = [];
|
|
621
|
+
const c = createSampleController(def, { count: 2 }, { onCall: (call) => calls.push(call) });
|
|
622
|
+
expect(Object.keys(c.actions)).toEqual(['add', 'clear']);
|
|
623
|
+
expect(await c.actions.add('p1', 3)).toEqual({ ok: true });
|
|
624
|
+
await c.actions.clear();
|
|
625
|
+
expect(c.calls).toEqual([
|
|
626
|
+
{ name: 'add', args: ['p1', 3] },
|
|
627
|
+
{ name: 'clear', args: [] },
|
|
628
|
+
]);
|
|
629
|
+
expect(calls).toEqual(c.calls);
|
|
630
|
+
expect(c.get()).toEqual({ count: 2, items: [] });
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
test('does not touch the app: a second sample is independent, a failing factory yields no actions', () => {
|
|
634
|
+
const a = createSampleController(def, { count: 1 });
|
|
635
|
+
const b = createSampleController(def, { count: 5 });
|
|
636
|
+
expect(a.get().count).toBe(1);
|
|
637
|
+
expect(b.get().count).toBe(5);
|
|
638
|
+
const broken = defineController({
|
|
639
|
+
name: 'broken',
|
|
640
|
+
version: 1,
|
|
641
|
+
actions: () => {
|
|
642
|
+
throw new Error('boom');
|
|
643
|
+
},
|
|
644
|
+
});
|
|
645
|
+
expect(createSampleController(broken).actions).toEqual({});
|
|
646
|
+
});
|
|
647
|
+
});
|