33 lines
1.6 KiB
JavaScript
33 lines
1.6 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFile } from "node:fs/promises";
|
|
import test from "node:test";
|
|
|
|
const connectionStateSource = await readFile(new URL("../static/js/connection-state.js", import.meta.url), "utf8");
|
|
const connectionState = await import(`data:text/javascript;base64,${Buffer.from(connectionStateSource).toString("base64")}`);
|
|
const {
|
|
CONNECTION_ERROR_AFTER_RECONNECT_ATTEMPTS,
|
|
reconnectStatus,
|
|
} = connectionState;
|
|
|
|
test("temporary reconnects remain reconnecting", () => {
|
|
for (let attempt = 0; attempt < CONNECTION_ERROR_AFTER_RECONNECT_ATTEMPTS; attempt += 1) {
|
|
assert.equal(reconnectStatus(attempt, true), "reconnecting");
|
|
}
|
|
});
|
|
|
|
test("persistent reconnect failures become an error while online", () => {
|
|
assert.equal(reconnectStatus(CONNECTION_ERROR_AFTER_RECONNECT_ATTEMPTS, true), "error");
|
|
assert.equal(reconnectStatus(CONNECTION_ERROR_AFTER_RECONNECT_ATTEMPTS + 10, true), "error");
|
|
});
|
|
|
|
test("offline devices stay in reconnecting state instead of a hard error", () => {
|
|
assert.equal(reconnectStatus(CONNECTION_ERROR_AFTER_RECONNECT_ATTEMPTS + 10, false), "reconnecting");
|
|
});
|
|
|
|
test("frontend exceptions do not overwrite the connection indicator", async () => {
|
|
const editorSource = await readFile(new URL("../static/js/note-editor.js", import.meta.url), "utf8");
|
|
assert.doesNotMatch(editorSource, /setStatus\([^\n]*Application error/);
|
|
assert.match(editorSource, /window\.addEventListener\("error", \(\) => notifyFrontendError\(\)\)/);
|
|
assert.match(editorSource, /window\.addEventListener\("unhandledrejection", \(\) => notifyFrontendError\(\)\)/);
|
|
});
|