Files
breeze/scripts/chrome-extension-breaks-test.mjs
T
waozi 64c1b6b7db Migrate Inbe's UI onto the .kry Kryon runtime
Author the remaining app UI, platform hosts, and storage helpers in .kry,
generate Kryon's src/ui modules and headers in the Inbe build, and follow the
migrated Kryon surface (generated props ABI and Button ImageProps). Move the
Plan 9 app loop and music fallback, Android lifecycle/share titles, desktop
tray, screenshots, and style tokens to their .kry owners, and delete the C
owners they replace.

Stop background execution whenever no visible indicator is present: the
desktop tray, Chrome worker, and Android foreground service now confirm their
indicator before continuing. Report gates: make native, no-vendor-edits,
clean-text-api-check, button-api-check, version-test, proof-test, and
sync-recovery-test pass.

vendor/kryon moves to 8f1a8608.
2026-09-20 23:56:13 -03:00

194 lines
5.5 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
import vm from "node:vm";
const workerSource = fs.readFileSync("packaging/chrome-web-store/service_worker.js", "utf8");
const storage = {};
const events = {};
const notifications = [];
const tabs = [];
const alarms = [];
let idleState = "active";
function event(name) {
events[name] = [];
return {
addListener(listener) {
events[name].push(listener);
},
};
}
const chrome = {
runtime: {
getURL(path) {
return `chrome-extension://inbe/${path}`;
},
onInstalled: event("runtime.onInstalled"),
onStartup: event("runtime.onStartup"),
onConnect: event("runtime.onConnect"),
onMessage: event("runtime.onMessage"),
},
storage: {
local: {
async get(keys) {
const out = {};
for (const key of keys)
out[key] = storage[key];
return out;
},
async set(values) {
Object.assign(storage, values);
},
},
},
alarms: {
async create(name, spec) {
alarms.push({ name, spec });
},
async clear(name) {
alarms.push({ name, clear: true });
},
onAlarm: event("alarms.onAlarm"),
},
idle: {
async queryState() {
return idleState;
},
},
notifications: {
async create(id, options) {
notifications.push({ id, options });
return id;
},
async clear(id) {
notifications.push({ id, clear: true });
return true;
},
onClicked: event("notifications.onClicked"),
onButtonClicked: event("notifications.onButtonClicked"),
},
action: {
onClicked: event("action.onClicked"),
async setBadgeText(value) {
chrome.action.badgeText = value.text;
},
async setBadgeBackgroundColor(value) {
chrome.action.badgeColor = value.color;
},
},
contextMenus: {
create(item) {
chrome.contextMenus.items.push(item);
},
removeAll(callback) {
chrome.contextMenus.items = [];
if (callback) callback();
},
items: [],
onClicked: event("contextMenus.onClicked"),
},
tabs: {
create(tab) {
tabs.push(tab);
},
},
};
const context = {
chrome,
console,
Date,
globalThis: {},
setTimeout,
clearTimeout,
};
context.globalThis = context;
vm.runInNewContext(workerSource, context, {
filename: "packaging/chrome-web-store/service_worker.js",
});
const api = context.__inbeExtensionBreaks;
assert.ok(api, "service worker exposes test API");
const normalized = api.normalizeConfig({
enabled: true,
timers: [{ limitS: 10.4, durationS: 2, showSkip: false }],
});
assert.equal(normalized.enabled, true);
assert.equal(normalized.timers[0].limitS, 10);
assert.equal(normalized.timers[0].showSkip, false);
assert.equal(normalized.timers[1].limitS, 2700);
events["runtime.onInstalled"][0]({ reason: "install" });
await new Promise((resolve) => setImmediate(resolve));
assert.ok(chrome.contextMenus.items.some((item) => item.id === "break-rest-now"));
assert.ok(tabs.some((tab) => tab.url === "chrome-extension://inbe/index.html"));
await api.requestBreakNow(1);
assert.equal(storage.inbeBreakConfig.enabled, true);
assert.ok(notifications.some((entry) => entry.id === "inbe-break-1" && entry.options));
assert.equal(chrome.action.badgeText, "", "manual break does not start a hidden alarm");
let disconnectVisiblePage;
events["runtime.onConnect"][0]({
name: "inbe-visible-page",
onDisconnect: {
addListener(listener) {
disconnectVisiblePage = listener;
},
},
});
await new Promise((resolve) => setImmediate(resolve));
assert.equal(chrome.action.badgeText, "BR", "visible page enables automatic breaks");
storage.inbeBreakConfig = api.normalizeConfig({
enabled: true,
timers: [
{ enabled: true, limitS: 60, durationS: 30, postponeS: 120, maxPrompts: 3, showSkip: true, showPostpone: true },
{ enabled: false },
{ enabled: false },
],
});
storage.inbeBreakState = api.normalizeState({ lastTickMs: 1000 });
idleState = "active";
await api.tickBreaks(61000);
assert.ok(notifications.some((entry) =>
entry.id === "inbe-break-0" && entry.options && entry.options.buttons.length === 2));
await api.applyNotificationAction("inbe-break-0", 0);
assert.ok(storage.inbeBreakState.timers[0].snoozeUntilMs > Date.now(), "postpone stores snooze deadline");
disconnectVisiblePage();
await new Promise((resolve) => setImmediate(resolve));
assert.equal(chrome.action.badgeText, "", "closing the page stops the indicator");
assert.ok(alarms.some((entry) => entry.name === "inbe-break-tick" && entry.clear));
const notificationCount = notifications.filter((entry) => entry.options).length;
await api.tickBreaks(122000);
assert.equal(notifications.filter((entry) => entry.options).length, notificationCount,
"closed page cannot receive automatic reminders");
assert.equal(storage.inbeBreakState.lastTickMs, 0, "closed page resets the timer");
events["runtime.onConnect"][0]({
name: "inbe-visible-page",
onDisconnect: { addListener() {} },
});
await new Promise((resolve) => setImmediate(resolve));
storage.inbeBreakState = api.normalizeState({
lastTickMs: 1000,
timers: [{ activeS: 80, idleS: 0 }],
});
idleState = "idle";
await api.tickBreaks(61000);
assert.equal(storage.inbeBreakState.timers[0].activeS, 0, "natural idle break resets timer");
events["notifications.onClicked"][0]("inbe-break-1");
assert.ok(tabs.some((tab) => tab.url.endsWith("?inbe_launch=break-settings")));
events["contextMenus.onClicked"][0]({ menuItemId: "habits" });
assert.ok(tabs.some((tab) => tab.url.endsWith("?inbe_launch=habits")));
console.log("Chrome extension break service worker tests passed");