@yuneta/gobj-ui 7.27.28 → 7.27.30
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/README.md +91 -2
- package/dist/gobj-ui.cjs.js +800 -69
- package/dist/gobj-ui.es.js +800 -71
- package/index.js +1 -0
- package/package.json +3 -3
- package/src/c_yui_gclass.js +32 -0
- package/src/c_yui_gobj_tree_js.js +66 -22
- package/src/c_yui_node.js +6 -22
- package/src/c_yui_update_watch.css +45 -0
- package/src/c_yui_update_watch.js +769 -0
- package/src/c_yui_update_watch.wiring.test.js +390 -0
- package/src/gclass_language.wiring.test.js +141 -0
- package/src/gobj_tree_camera.wiring.test.js +81 -7
- package/src/gobj_tree_remote.wiring.test.js +102 -2
- package/src/shell_modals.js +8 -0
- package/src/shell_view_shown.wiring.test.js +71 -1
- package/src/vite_plugin_yuneta_html.test.js +34 -1
- package/src/yui_build_stamp.js +125 -0
- package/src/yui_build_stamp.test.js +56 -0
- package/vite-plugin-yuneta-html.js +60 -2
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* c_yui_update_watch.wiring.test.js
|
|
3
|
+
*
|
|
4
|
+
* C_YUI_UPDATE_WATCH driven through its FSM on a document double,
|
|
5
|
+
* with the server played by a fake fetch: the bar appears for
|
|
6
|
+
* another build and not for the same one, the ✕ hides it for THAT
|
|
7
|
+
* build only, a rollback takes it away, a check too soon or
|
|
8
|
+
* offline asks nothing (navigator.onLine is stubbed: vitest has
|
|
9
|
+
* none, and the guard never ran) and coming back online asks at
|
|
10
|
+
* once, a failure is said once, the reload asks the host first,
|
|
11
|
+
* and with no shell (the login) the app's language switch reaches
|
|
12
|
+
* the bar through yui_update_watch_language_changed().
|
|
13
|
+
*
|
|
14
|
+
* Copyright (c) 2026, ArtGins.
|
|
15
|
+
* All Rights Reserved.
|
|
16
|
+
***********************************************************************/
|
|
17
|
+
import {describe, test, expect, beforeAll, beforeEach, afterEach} from "vitest";
|
|
18
|
+
import {install_dom_double, fake_event} from "../test/dom_double.js";
|
|
19
|
+
|
|
20
|
+
install_dom_double();
|
|
21
|
+
|
|
22
|
+
const {
|
|
23
|
+
gclass_create, SDATA_END,
|
|
24
|
+
gobj_start_up, gobj_create_yuno, gobj_create_service, gobj_start, gobj_stop,
|
|
25
|
+
gobj_destroy, gobj_is_running, gobj_send_event, gobj_current_state,
|
|
26
|
+
set_log_callback, register_c_timer,
|
|
27
|
+
} = await import("@yuneta/gobj-js");
|
|
28
|
+
const {register_c_yui_update_watch, yui_update_watch_language_changed} =
|
|
29
|
+
await import("./c_yui_update_watch.js");
|
|
30
|
+
|
|
31
|
+
const RUNNING = "aaaaaaaaaaaaaaaa";
|
|
32
|
+
|
|
33
|
+
const logged = [];
|
|
34
|
+
const heard = [];
|
|
35
|
+
let yuno = null;
|
|
36
|
+
let host = null;
|
|
37
|
+
let watch = null;
|
|
38
|
+
let seq = 0;
|
|
39
|
+
let i18next = null;
|
|
40
|
+
|
|
41
|
+
/* navigator.onLine as the browser says it: vitest has no such
|
|
42
|
+
* property, so `undefined` unless a test sets it. */
|
|
43
|
+
const network = {online: undefined};
|
|
44
|
+
Object.defineProperty(globalThis.navigator, "onLine", {
|
|
45
|
+
configurable: true,
|
|
46
|
+
get: () => network.online,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/* The server: what the next fetches answer, and what was asked. */
|
|
50
|
+
const server = {
|
|
51
|
+
stamp: RUNNING,
|
|
52
|
+
version: "1.0.0",
|
|
53
|
+
fail: null, /* null | "network" | number (HTTP status) | "no-stamp" */
|
|
54
|
+
asked: 0,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function page(stamp, version)
|
|
58
|
+
{
|
|
59
|
+
return `<!doctype html><html><head><title>x</title>` +
|
|
60
|
+
`<meta name="yuneta-build" content="${stamp}" data-version="${version}">` +
|
|
61
|
+
`</head><body></body></html>`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function fake_fetch(url, opts)
|
|
65
|
+
{
|
|
66
|
+
server.asked++;
|
|
67
|
+
expect(opts.cache).toBe("no-store");
|
|
68
|
+
if(server.fail === "network") {
|
|
69
|
+
return Promise.reject(new TypeError("Failed to fetch"));
|
|
70
|
+
}
|
|
71
|
+
if(typeof server.fail === "number") {
|
|
72
|
+
return Promise.resolve({ok: false, status: server.fail, text: () => Promise.resolve("")});
|
|
73
|
+
}
|
|
74
|
+
const body = server.fail === "no-stamp"
|
|
75
|
+
? "<html><head><title>Hotel wifi</title></head></html>"
|
|
76
|
+
: page(server.stamp, server.version);
|
|
77
|
+
return Promise.resolve({ok: true, status: 200, text: () => Promise.resolve(body)});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function settle()
|
|
81
|
+
{
|
|
82
|
+
for(let i = 0; i < 5; i++) {
|
|
83
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function bar()
|
|
88
|
+
{
|
|
89
|
+
return document.body.querySelector(".UPDATE_BAR");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function make_watch(kw)
|
|
93
|
+
{
|
|
94
|
+
watch = gobj_create_service(`update_watch_${++seq}`, "C_YUI_UPDATE_WATCH",
|
|
95
|
+
{subscriber: host, period_ms: 0, ...(kw || {})}, yuno);
|
|
96
|
+
gobj_start(watch);
|
|
97
|
+
return watch;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
beforeAll(async () => {
|
|
101
|
+
i18next = (await import("i18next")).default;
|
|
102
|
+
await i18next.init({lng: "en", resources: {
|
|
103
|
+
es: {translation: {
|
|
104
|
+
"new version available": "hay una versión nueva",
|
|
105
|
+
"reload": "recargar",
|
|
106
|
+
"close": "cerrar",
|
|
107
|
+
}},
|
|
108
|
+
}});
|
|
109
|
+
|
|
110
|
+
const $meta = document.createElement("meta");
|
|
111
|
+
$meta.setAttribute("name", "yuneta-build");
|
|
112
|
+
$meta.setAttribute("content", RUNNING);
|
|
113
|
+
$meta.setAttribute("data-version", "1.0.0");
|
|
114
|
+
document.head.appendChild($meta);
|
|
115
|
+
|
|
116
|
+
globalThis.fetch = fake_fetch;
|
|
117
|
+
|
|
118
|
+
gobj_start_up(null, null, null, null, null, null, null);
|
|
119
|
+
set_log_callback((level, msg) => {
|
|
120
|
+
logged.push({level: String(level), msg: String(msg)});
|
|
121
|
+
});
|
|
122
|
+
register_c_timer();
|
|
123
|
+
register_c_yui_update_watch();
|
|
124
|
+
gclass_create("C_TEST_UPD_YUNO", [], [["ST_IDLE", []]], {}, 0, [SDATA_END()], {}, 0, 0, 0, 0);
|
|
125
|
+
gclass_create(
|
|
126
|
+
"C_TEST_UPD_HOST",
|
|
127
|
+
[["EV_NEW_BUILD", 0], ["EV_BUILD_CURRENT", 0]],
|
|
128
|
+
[["ST_IDLE", [
|
|
129
|
+
["EV_NEW_BUILD", (gobj, event, kw) => {
|
|
130
|
+
heard.push({event, kw});
|
|
131
|
+
return 0;
|
|
132
|
+
}, null],
|
|
133
|
+
["EV_BUILD_CURRENT", (gobj, event, kw) => {
|
|
134
|
+
heard.push({event, kw});
|
|
135
|
+
return 0;
|
|
136
|
+
}, null]
|
|
137
|
+
]]],
|
|
138
|
+
{}, 0, [SDATA_END()], {}, 0, 0, 0, 0
|
|
139
|
+
);
|
|
140
|
+
yuno = gobj_create_yuno("update_watch_yuno", "C_TEST_UPD_YUNO", {});
|
|
141
|
+
gobj_start(yuno);
|
|
142
|
+
host = gobj_create_service("update_watch_host", "C_TEST_UPD_HOST", {}, yuno);
|
|
143
|
+
gobj_start(host);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
beforeEach(() => {
|
|
147
|
+
logged.length = 0;
|
|
148
|
+
heard.length = 0;
|
|
149
|
+
server.stamp = RUNNING;
|
|
150
|
+
server.version = "1.0.0";
|
|
151
|
+
server.fail = null;
|
|
152
|
+
server.asked = 0;
|
|
153
|
+
network.online = undefined;
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
afterEach(() => {
|
|
157
|
+
if(watch) {
|
|
158
|
+
if(gobj_is_running(watch)) {
|
|
159
|
+
gobj_stop(watch);
|
|
160
|
+
}
|
|
161
|
+
gobj_destroy(watch);
|
|
162
|
+
watch = null;
|
|
163
|
+
}
|
|
164
|
+
expect(bar()).toBe(null);
|
|
165
|
+
expect(logged.filter((l) => l.level === "error")).toEqual([]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("C_YUI_UPDATE_WATCH", () => {
|
|
169
|
+
test("the same build deployed: asked at start, no bar", async () => {
|
|
170
|
+
make_watch();
|
|
171
|
+
await settle();
|
|
172
|
+
expect(server.asked).toBe(1);
|
|
173
|
+
expect(gobj_current_state(watch)).toBe("ST_CURRENT");
|
|
174
|
+
expect(bar()).toBe(null);
|
|
175
|
+
expect(heard).toEqual([]);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("another build: the bar, its version, EV_NEW_BUILD", async () => {
|
|
179
|
+
server.stamp = "bbbbbbbbbbbbbbbb";
|
|
180
|
+
server.version = "1.0.1";
|
|
181
|
+
make_watch();
|
|
182
|
+
await settle();
|
|
183
|
+
expect(gobj_current_state(watch)).toBe("ST_STALE");
|
|
184
|
+
const $bar = bar();
|
|
185
|
+
expect($bar).not.toBe(null);
|
|
186
|
+
expect($bar.querySelector(".UPDATE_VERSION").textContent).toBe("1.0.1");
|
|
187
|
+
const $reload = $bar.querySelector(".UPDATE_RELOAD");
|
|
188
|
+
expect($reload.getAttribute("title")).toBe("reload");
|
|
189
|
+
expect($reload.getAttribute("aria-label")).toBe("reload");
|
|
190
|
+
expect($reload.getAttribute("data-i18n-aria-label")).toBe("reload");
|
|
191
|
+
const $x = $bar.querySelector(".UPDATE_DISMISS");
|
|
192
|
+
expect($x.getAttribute("aria-label")).toBe("close");
|
|
193
|
+
expect(heard).toEqual([{event: "EV_NEW_BUILD", kw: {
|
|
194
|
+
running: RUNNING, deployed: "bbbbbbbbbbbbbbbb", version: "1.0.1"
|
|
195
|
+
}}]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("the ✕ hides it for that build; a later build shows it again", async () => {
|
|
199
|
+
server.stamp = "bbbbbbbbbbbbbbbb";
|
|
200
|
+
make_watch();
|
|
201
|
+
await settle();
|
|
202
|
+
gobj_send_event(watch, "EV_DISMISS", {}, watch);
|
|
203
|
+
expect(bar()).toBe(null);
|
|
204
|
+
|
|
205
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
206
|
+
await settle();
|
|
207
|
+
expect(bar()).toBe(null);
|
|
208
|
+
|
|
209
|
+
server.stamp = "cccccccccccccccc";
|
|
210
|
+
server.version = "1.0.2";
|
|
211
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
212
|
+
await settle();
|
|
213
|
+
expect(bar().querySelector(".UPDATE_VERSION").textContent).toBe("1.0.2");
|
|
214
|
+
expect(heard.map((h) => h.kw.deployed)).toEqual(["bbbbbbbbbbbbbbbb", "cccccccccccccccc"]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("a rollback to the running build takes the bar away", async () => {
|
|
218
|
+
server.stamp = "bbbbbbbbbbbbbbbb";
|
|
219
|
+
make_watch();
|
|
220
|
+
await settle();
|
|
221
|
+
expect(bar()).not.toBe(null);
|
|
222
|
+
|
|
223
|
+
server.stamp = RUNNING;
|
|
224
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
225
|
+
await settle();
|
|
226
|
+
expect(gobj_current_state(watch)).toBe("ST_CURRENT");
|
|
227
|
+
expect(bar()).toBe(null);
|
|
228
|
+
expect(heard[1]).toEqual({event: "EV_BUILD_CURRENT", kw: {running: RUNNING}});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("the focus asks, but not twice within min_interval_ms", async () => {
|
|
232
|
+
make_watch({min_interval_ms: 60000});
|
|
233
|
+
await settle();
|
|
234
|
+
expect(server.asked).toBe(1);
|
|
235
|
+
window.dispatchEvent(fake_event("focus"));
|
|
236
|
+
document.dispatchEvent(fake_event("visibilitychange"));
|
|
237
|
+
await settle();
|
|
238
|
+
expect(server.asked).toBe(1);
|
|
239
|
+
|
|
240
|
+
window.dispatchEvent(fake_event("vite:preloadError"));
|
|
241
|
+
await settle();
|
|
242
|
+
expect(server.asked).toBe(2);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("with min_interval_ms 0, the focus asks", async () => {
|
|
246
|
+
make_watch({min_interval_ms: 0});
|
|
247
|
+
await settle();
|
|
248
|
+
window.dispatchEvent(fake_event("focus"));
|
|
249
|
+
await settle();
|
|
250
|
+
expect(server.asked).toBe(2);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("period_ms asks on its own, for a screen that never gets the focus", async () => {
|
|
254
|
+
make_watch({min_interval_ms: 0, period_ms: 20});
|
|
255
|
+
await settle();
|
|
256
|
+
expect(server.asked).toBe(1);
|
|
257
|
+
await new Promise((resolve) => setTimeout(resolve, 70));
|
|
258
|
+
await settle();
|
|
259
|
+
expect(server.asked).toBeGreaterThanOrEqual(3);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test("offline asks nothing, not even forced", async () => {
|
|
263
|
+
network.online = false;
|
|
264
|
+
make_watch({min_interval_ms: 0});
|
|
265
|
+
await settle();
|
|
266
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
267
|
+
window.dispatchEvent(fake_event("focus"));
|
|
268
|
+
await settle();
|
|
269
|
+
expect(server.asked).toBe(0);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("back online asks at once, inside min_interval_ms", async () => {
|
|
273
|
+
network.online = true;
|
|
274
|
+
make_watch({min_interval_ms: 60000});
|
|
275
|
+
await settle();
|
|
276
|
+
expect(server.asked).toBe(1);
|
|
277
|
+
window.dispatchEvent(fake_event("focus"));
|
|
278
|
+
await settle();
|
|
279
|
+
expect(server.asked).toBe(1);
|
|
280
|
+
|
|
281
|
+
network.online = false;
|
|
282
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
283
|
+
await settle();
|
|
284
|
+
expect(server.asked).toBe(1);
|
|
285
|
+
|
|
286
|
+
network.online = true;
|
|
287
|
+
window.dispatchEvent(fake_event("online"));
|
|
288
|
+
await settle();
|
|
289
|
+
expect(server.asked).toBe(2);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("with no shell, the app's language switch re-translates the bar", async () => {
|
|
293
|
+
server.stamp = "bbbbbbbbbbbbbbbb";
|
|
294
|
+
make_watch();
|
|
295
|
+
await settle();
|
|
296
|
+
const $bar = bar();
|
|
297
|
+
const $reload = $bar.querySelector(".UPDATE_RELOAD");
|
|
298
|
+
expect($reload.getAttribute("aria-label")).toBe("reload");
|
|
299
|
+
|
|
300
|
+
/* The texts carry their key; the document double's text nodes
|
|
301
|
+
* have no nodeValue for refresh_language() to write, so what it
|
|
302
|
+
* rewrites is read from the attributes. */
|
|
303
|
+
expect($bar.querySelector(".UPDATE_TEXT").getAttribute("data-i18n")).toBe("new version available");
|
|
304
|
+
await i18next.changeLanguage("es");
|
|
305
|
+
try {
|
|
306
|
+
expect($reload.getAttribute("aria-label")).toBe("reload");
|
|
307
|
+
yui_update_watch_language_changed();
|
|
308
|
+
expect([$reload.getAttribute("title"), $reload.getAttribute("aria-label")])
|
|
309
|
+
.toEqual(["recargar", "recargar"]);
|
|
310
|
+
expect($bar.querySelector(".UPDATE_DISMISS").getAttribute("aria-label")).toBe("cerrar");
|
|
311
|
+
expect($bar.querySelector(".UPDATE_VERSION").textContent).toBe("1.0.0");
|
|
312
|
+
} finally {
|
|
313
|
+
await i18next.changeLanguage("en");
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("stopped, the page events ask nothing", async () => {
|
|
318
|
+
make_watch({min_interval_ms: 0});
|
|
319
|
+
await settle();
|
|
320
|
+
gobj_stop(watch);
|
|
321
|
+
window.dispatchEvent(fake_event("focus"));
|
|
322
|
+
window.dispatchEvent(fake_event("online"));
|
|
323
|
+
await settle();
|
|
324
|
+
expect(server.asked).toBe(1);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("a failure is said once, and its end once", async () => {
|
|
328
|
+
server.fail = 503;
|
|
329
|
+
make_watch();
|
|
330
|
+
await settle();
|
|
331
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
332
|
+
await settle();
|
|
333
|
+
const warnings = logged.filter((l) => l.level === "warning");
|
|
334
|
+
expect(warnings.length).toBe(1);
|
|
335
|
+
expect(warnings[0].msg).toContain("HTTP 503");
|
|
336
|
+
|
|
337
|
+
server.fail = null;
|
|
338
|
+
gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
|
|
339
|
+
await settle();
|
|
340
|
+
expect(logged.filter((l) => l.msg.includes("can be read again")).length).toBe(1);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("a network failure is info, a page with no stamp a warning, neither a new build", async () => {
|
|
344
|
+
server.fail = "network";
|
|
345
|
+
make_watch();
|
|
346
|
+
await settle();
|
|
347
|
+
expect(logged.filter((l) => l.level === "warning")).toEqual([]);
|
|
348
|
+
expect(logged.some((l) => l.level === "info" && l.msg.includes("Failed to fetch"))).toBe(true);
|
|
349
|
+
gobj_stop(watch);
|
|
350
|
+
gobj_destroy(watch);
|
|
351
|
+
|
|
352
|
+
logged.length = 0;
|
|
353
|
+
server.fail = "no-stamp";
|
|
354
|
+
make_watch();
|
|
355
|
+
await settle();
|
|
356
|
+
expect(gobj_current_state(watch)).toBe("ST_CURRENT");
|
|
357
|
+
expect(bar()).toBe(null);
|
|
358
|
+
expect(logged.filter((l) => l.level === "warning").length).toBe(1);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test("the reload asks the host first, and a veto keeps the page", async () => {
|
|
362
|
+
let reloads = 0;
|
|
363
|
+
window.location.reload = () => {
|
|
364
|
+
reloads++;
|
|
365
|
+
};
|
|
366
|
+
let answer = false;
|
|
367
|
+
server.stamp = "bbbbbbbbbbbbbbbb";
|
|
368
|
+
make_watch({before_reload: () => Promise.resolve(answer)});
|
|
369
|
+
await settle();
|
|
370
|
+
|
|
371
|
+
gobj_send_event(watch, "EV_RELOAD", {}, watch);
|
|
372
|
+
await settle();
|
|
373
|
+
expect(reloads).toBe(0);
|
|
374
|
+
|
|
375
|
+
answer = true;
|
|
376
|
+
bar().querySelector(".UPDATE_RELOAD").dispatchEvent(fake_event("click"));
|
|
377
|
+
await settle();
|
|
378
|
+
expect(reloads).toBe(1);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test("in ST_CURRENT a reload is not an event of the state", async () => {
|
|
382
|
+
make_watch();
|
|
383
|
+
await settle();
|
|
384
|
+
const ret = gobj_send_event(watch, "EV_RELOAD", {}, watch);
|
|
385
|
+
expect(ret).toBe(-1);
|
|
386
|
+
const errors = logged.filter((l) => l.level === "error");
|
|
387
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
388
|
+
logged.length = 0;
|
|
389
|
+
});
|
|
390
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* gclass_language.wiring.test.js
|
|
3
|
+
*
|
|
4
|
+
* C_YUI_GCLASS, the gclass viewer, changes the language of its
|
|
5
|
+
* COUNTED words ("4 instances", "show all 25") when the app
|
|
6
|
+
* switches language. Those carry no key on purpose (a key would
|
|
7
|
+
* make refresh_language() write the bare word over the count), so
|
|
8
|
+
* only a re-render can change them, and nobody sent the viewer the
|
|
9
|
+
* switch: open_gclass_view() creates it with no subscriber and its
|
|
10
|
+
* host does not forward it. It hears the shell's
|
|
11
|
+
* EV_LANGUAGE_CHANGED itself now, as C_YUI_JSON does.
|
|
12
|
+
*
|
|
13
|
+
* The real gclass and a real C_YUI_SHELL, on a document double.
|
|
14
|
+
*
|
|
15
|
+
* Copyright (c) 2026, ArtGins.
|
|
16
|
+
* All Rights Reserved.
|
|
17
|
+
***********************************************************************/
|
|
18
|
+
import {describe, test, expect, beforeAll, beforeEach} from "vitest";
|
|
19
|
+
import {install_dom_double} from "../test/dom_double.js";
|
|
20
|
+
|
|
21
|
+
install_dom_double();
|
|
22
|
+
|
|
23
|
+
if(typeof globalThis.ResizeObserver === "undefined") {
|
|
24
|
+
globalThis.ResizeObserver = class {
|
|
25
|
+
observe() {
|
|
26
|
+
}
|
|
27
|
+
unobserve() {
|
|
28
|
+
}
|
|
29
|
+
disconnect() {
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const {
|
|
35
|
+
SDATA, SDATA_END, data_type_t, gclass_create, gobj_write_attr,
|
|
36
|
+
gobj_start_up, gobj_create_yuno, gobj_create, gobj_start, gobj_stop,
|
|
37
|
+
gobj_destroy, gobj_read_attr, set_log_callback,
|
|
38
|
+
} = await import("@yuneta/gobj-js");
|
|
39
|
+
const {register_c_yui_shell, yui_shell_language_changed} = await import("./c_yui_shell.js");
|
|
40
|
+
const {register_c_yui_nav} = await import("./c_yui_nav.js");
|
|
41
|
+
const {register_c_yui_gclass} = await import("./c_yui_gclass.js");
|
|
42
|
+
|
|
43
|
+
const logged = [];
|
|
44
|
+
let i18next = null;
|
|
45
|
+
let yuno = null;
|
|
46
|
+
let host = null;
|
|
47
|
+
let shell = null;
|
|
48
|
+
|
|
49
|
+
const DESCRIPTION = {
|
|
50
|
+
id: "C_TCP",
|
|
51
|
+
gcflag: [],
|
|
52
|
+
instances: 4,
|
|
53
|
+
attrs: Array.from({length: 25}, (_, i) => ({
|
|
54
|
+
id: `attr_${i}`, type: "string", flag: "SDF_RD",
|
|
55
|
+
default_value: "", description: `attribute ${i}`
|
|
56
|
+
})),
|
|
57
|
+
commands: [],
|
|
58
|
+
gclass_methods: [],
|
|
59
|
+
internal_methods: [],
|
|
60
|
+
FSM: {events: [], states: {}},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
beforeAll(async () => {
|
|
64
|
+
i18next = (await import("i18next")).default;
|
|
65
|
+
await i18next.init({lng: "en", resources: {
|
|
66
|
+
en: {translation: {
|
|
67
|
+
"instances": "instances",
|
|
68
|
+
"instances_one": "instance",
|
|
69
|
+
"show all {{n}}": "show all {{n}}",
|
|
70
|
+
}},
|
|
71
|
+
es: {translation: {
|
|
72
|
+
"instances": "instancias",
|
|
73
|
+
"instances_one": "instancia",
|
|
74
|
+
"show all {{n}}": "ver las {{n}}",
|
|
75
|
+
}},
|
|
76
|
+
}});
|
|
77
|
+
gobj_start_up(null, null, null, null, null, null, null);
|
|
78
|
+
set_log_callback((level, msg) => {
|
|
79
|
+
logged.push({level: String(level), msg: String(msg)});
|
|
80
|
+
});
|
|
81
|
+
gclass_create("C_TEST_GCLASS_LANG_YUNO", [], [["ST_IDLE", []]], {}, 0,
|
|
82
|
+
[SDATA_END()], {}, 0, 0, 0, 0);
|
|
83
|
+
gclass_create("C_TEST_GCLASS_LANG_VIEW", [], [["ST_IDLE", []]], {
|
|
84
|
+
mt_create: (gobj) => {
|
|
85
|
+
gobj_write_attr(gobj, "$container", document.createElement("div"));
|
|
86
|
+
}
|
|
87
|
+
}, 0, [SDATA(data_type_t.DTP_POINTER, "$container", 0, null, "view root"), SDATA_END()],
|
|
88
|
+
{}, 0, 0, 0, 0);
|
|
89
|
+
register_c_yui_shell();
|
|
90
|
+
register_c_yui_nav();
|
|
91
|
+
register_c_yui_gclass();
|
|
92
|
+
yuno = gobj_create_yuno("gclass_lang_yuno", "C_TEST_GCLASS_LANG_YUNO", {});
|
|
93
|
+
gobj_start(yuno);
|
|
94
|
+
host = gobj_create("host", "C_TEST_GCLASS_LANG_YUNO", {}, yuno);
|
|
95
|
+
shell = gobj_create("gclass_lang_shell", "C_YUI_SHELL", {use_hash: false, config: {
|
|
96
|
+
shell: {
|
|
97
|
+
zones: {center: {host: "stage.main"}},
|
|
98
|
+
stages: {main: {zone: "center", default_route: "/a"}}
|
|
99
|
+
},
|
|
100
|
+
menu: {primary: {items: [
|
|
101
|
+
{id: "a", name: "a", route: "/a",
|
|
102
|
+
target: {stage: "main", gclass: "C_TEST_GCLASS_LANG_VIEW", kw: {}}}
|
|
103
|
+
]}}
|
|
104
|
+
}}, yuno);
|
|
105
|
+
gobj_start(shell);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
beforeEach(async () => {
|
|
109
|
+
logged.length = 0;
|
|
110
|
+
await i18next.changeLanguage("en");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
function problems()
|
|
114
|
+
{
|
|
115
|
+
return logged.filter((l) => l.level === "error" || l.level === "warning").map((l) => l.msg);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
describe("C_YUI_GCLASS: the counted words follow the language", () => {
|
|
119
|
+
test("the shell's language switch re-renders them", async () => {
|
|
120
|
+
const view = gobj_create("gclass_lang_view", "C_YUI_GCLASS",
|
|
121
|
+
{description: DESCRIPTION}, host);
|
|
122
|
+
gobj_start(view);
|
|
123
|
+
const $c = gobj_read_attr(view, "$container");
|
|
124
|
+
const $more = () => $c.querySelector('.GCLASS_SHOW_ALL[data-zone="attrs"]');
|
|
125
|
+
expect($c.querySelector(".GCLASS_INSTANCES_LABEL").textContent).toBe("instances");
|
|
126
|
+
expect($more().textContent).toBe("show all 25");
|
|
127
|
+
|
|
128
|
+
await i18next.changeLanguage("es");
|
|
129
|
+
yui_shell_language_changed(shell);
|
|
130
|
+
expect($c.querySelector(".GCLASS_INSTANCES_LABEL").textContent).toBe("instancias");
|
|
131
|
+
expect([$more().textContent, $more().getAttribute("title"), $more().getAttribute("aria-label")])
|
|
132
|
+
.toEqual(["ver las 25", "ver las 25", "ver las 25"]);
|
|
133
|
+
|
|
134
|
+
/* stopped, it no longer hears the shell */
|
|
135
|
+
gobj_stop(view);
|
|
136
|
+
gobj_destroy(view);
|
|
137
|
+
await i18next.changeLanguage("en");
|
|
138
|
+
yui_shell_language_changed(shell);
|
|
139
|
+
expect(problems()).toEqual([]);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
* camera was saved as nulls (a NaN has no json text) and gobj-js
|
|
8
8
|
* 7.26.27 refuses the whole value with an ERROR. Only real numbers
|
|
9
9
|
* are written now; a camera that is not readable keeps the one
|
|
10
|
-
* saved before, and a saved camera of nulls is not restored.
|
|
10
|
+
* saved before, and a saved camera of nulls is not restored. What
|
|
11
|
+
* is saved is getPosition(), and it is brought back with
|
|
12
|
+
* translateTo() through G6's closed forms, at any zoom.
|
|
11
13
|
*
|
|
12
14
|
* The real gclass, on a G6 Graph double whose camera the test sets.
|
|
13
15
|
*
|
|
@@ -31,7 +33,14 @@ if(typeof globalThis.ResizeObserver === "undefined") {
|
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
const camera = vi.hoisted(() => ({zoom: NaN, position: [NaN, NaN], zoomed: [], moved: [],
|
|
34
|
-
size: null, viewport: null, plugins: []}));
|
|
36
|
+
size: null, viewport: null, plugins: [], g6: null}));
|
|
37
|
+
|
|
38
|
+
/* `camera.g6`, when set, is a camera that obeys G6 5.x's own closed
|
|
39
|
+
* forms, with C the canvas centre, Z the zoom and P the camera's
|
|
40
|
+
* position: getPosition() = C/Z - P, an absolute translateTo(T) leaves
|
|
41
|
+
* P = C - T/Z, zoomTo() keeps P, and a point n lands at (n - P)*Z + C.
|
|
42
|
+
* {Z, P, node}: `node` is where every card is. */
|
|
43
|
+
const CENTRE = [400, 300];
|
|
35
44
|
vi.mock("@antv/g6", async (importOriginal) => {
|
|
36
45
|
const real = await importOriginal();
|
|
37
46
|
class FakeGraph {
|
|
@@ -50,17 +59,28 @@ vi.mock("@antv/g6", async (importOriginal) => {
|
|
|
50
59
|
on() {
|
|
51
60
|
}
|
|
52
61
|
getZoom() {
|
|
53
|
-
return camera.zoom;
|
|
62
|
+
return camera.g6? camera.g6.Z: camera.zoom;
|
|
54
63
|
}
|
|
55
64
|
getPosition() {
|
|
65
|
+
const g = camera.g6;
|
|
66
|
+
if(g) {
|
|
67
|
+
return [CENTRE[0] / g.Z - g.P[0], CENTRE[1] / g.Z - g.P[1]];
|
|
68
|
+
}
|
|
56
69
|
return camera.position;
|
|
57
70
|
}
|
|
58
71
|
zoomTo(z) {
|
|
59
72
|
camera.zoomed.push(z);
|
|
73
|
+
if(camera.g6) {
|
|
74
|
+
camera.g6.Z = z;
|
|
75
|
+
}
|
|
60
76
|
return Promise.resolve();
|
|
61
77
|
}
|
|
62
78
|
translateTo(p) {
|
|
63
79
|
camera.moved.push(p);
|
|
80
|
+
const g = camera.g6;
|
|
81
|
+
if(g) {
|
|
82
|
+
g.P = [CENTRE[0] - p[0] / g.Z, CENTRE[1] - p[1] / g.Z];
|
|
83
|
+
}
|
|
64
84
|
return Promise.resolve();
|
|
65
85
|
}
|
|
66
86
|
/* Where a card lands on screen: `camera.viewport` for every one
|
|
@@ -73,15 +93,19 @@ vi.mock("@antv/g6", async (importOriginal) => {
|
|
|
73
93
|
return camera.size;
|
|
74
94
|
}
|
|
75
95
|
getElementPosition() {
|
|
76
|
-
return [0, 0];
|
|
96
|
+
return camera.g6? camera.g6.node: [0, 0];
|
|
77
97
|
}
|
|
78
98
|
getElementData() {
|
|
79
99
|
return {};
|
|
80
100
|
}
|
|
81
101
|
getCanvasCenter() {
|
|
82
|
-
return
|
|
102
|
+
return CENTRE;
|
|
83
103
|
}
|
|
84
|
-
getViewportByCanvas() {
|
|
104
|
+
getViewportByCanvas(n) {
|
|
105
|
+
const g = camera.g6;
|
|
106
|
+
if(g) {
|
|
107
|
+
return [(n[0] - g.P[0]) * g.Z + CENTRE[0], (n[1] - g.P[1]) * g.Z + CENTRE[1]];
|
|
108
|
+
}
|
|
85
109
|
return camera.viewport;
|
|
86
110
|
}
|
|
87
111
|
getData() {
|
|
@@ -156,6 +180,7 @@ beforeEach(() => {
|
|
|
156
180
|
camera.size = null;
|
|
157
181
|
camera.viewport = null;
|
|
158
182
|
camera.plugins = [];
|
|
183
|
+
camera.g6 = null;
|
|
159
184
|
});
|
|
160
185
|
|
|
161
186
|
function errors()
|
|
@@ -249,8 +274,57 @@ describe("C_YUI_GOBJ_TREE_JS does not restore a camera that shows nothing", () =
|
|
|
249
274
|
await mount(name);
|
|
250
275
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
251
276
|
expect(camera.zoomed).toEqual([0.5]);
|
|
252
|
-
|
|
277
|
+
/* getPosition() handed back to translateTo() at zoom 0.5:
|
|
278
|
+
* T = (S + C) * Z - C, not S itself */
|
|
279
|
+
expect(camera.moved).toEqual([[(10 + 400) * 0.5 - 400, (20 + 300) * 0.5 - 300]]);
|
|
280
|
+
expect(errors()).toEqual([]);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
describe("C_YUI_GOBJ_TREE_JS brings the camera back where it was, at any zoom", () => {
|
|
285
|
+
async function round_trip(zoom, P)
|
|
286
|
+
{
|
|
287
|
+
const name = `camera_${++seq}`;
|
|
288
|
+
camera.size = [800, 600];
|
|
289
|
+
camera.g6 = {Z: 1, P: [400, 300], node: [100, 50]};
|
|
290
|
+
const first = await mount(name);
|
|
291
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
292
|
+
|
|
293
|
+
/* the reader zooms and pans; the move is saved */
|
|
294
|
+
camera.g6.Z = zoom;
|
|
295
|
+
camera.g6.P = P.slice();
|
|
296
|
+
gobj_send_event(first, "EV_REFRESH", {}, first);
|
|
297
|
+
const S = [400 / zoom - P[0], 300 / zoom - P[1]];
|
|
298
|
+
expect(saved(name).zoom).toBe(zoom);
|
|
299
|
+
expect(saved(name).position[0]).toBeCloseTo(S[0], 6);
|
|
300
|
+
expect(saved(name).position[1]).toBeCloseTo(S[1], 6);
|
|
301
|
+
/* (the refresh frames that view again, later: let it) */
|
|
302
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
303
|
+
|
|
304
|
+
/* a new opening, on a fresh camera */
|
|
305
|
+
camera.g6 = {Z: 1, P: [400, 300], node: [100, 50]};
|
|
306
|
+
camera.zoomed = [];
|
|
307
|
+
await mount(name);
|
|
308
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
309
|
+
expect(camera.zoomed).toEqual([zoom]);
|
|
310
|
+
expect(camera.g6.Z).toBe(zoom);
|
|
311
|
+
expect(camera.g6.P[0]).toBeCloseTo(P[0], 6);
|
|
312
|
+
expect(camera.g6.P[1]).toBeCloseTo(P[1], 6);
|
|
313
|
+
expect(logged.filter((l) => l.level === "warning").map((l) => l.msg).join(" "))
|
|
314
|
+
.not.toMatch(/the saved view shows none of the/);
|
|
253
315
|
expect(errors()).toEqual([]);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
test("at zoom 0.3: kept, not judged to show nothing", async () => {
|
|
319
|
+
await round_trip(0.3, [150, 80]);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test("at zoom 0.8: not shifted", async () => {
|
|
323
|
+
await round_trip(0.8, [180, 20]);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("at zoom 2", async () => {
|
|
327
|
+
await round_trip(2, [90, 60]);
|
|
254
328
|
});
|
|
255
329
|
});
|
|
256
330
|
|