105 lines
3.0 KiB
TypeScript
105 lines
3.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
describeRuntimeCapabilities,
|
|
type CapabilityOverrideMap,
|
|
type InstalledRuntimeCapabilities,
|
|
} from "../../src/contracts/runtime-capabilities.ts";
|
|
import { SERVICE_WORKER_SCRIPT_PATH } from "../../src/contracts/service-worker.ts";
|
|
|
|
const DEFAULTS: CapabilityOverrideMap = Object.freeze({
|
|
REALTIME: "DEFAULT",
|
|
WEB_WORKER: "DEFAULT",
|
|
SERVICE_WORKER: "DEFAULT",
|
|
OFFLINE_COMMANDS: "DEFAULT",
|
|
});
|
|
|
|
const EMPTY: InstalledRuntimeCapabilities = Object.freeze({
|
|
realtime: Object.freeze([]),
|
|
webWorkers: Object.freeze([]),
|
|
serviceWorker: null,
|
|
offlineCommands: null,
|
|
});
|
|
|
|
const SELECTED: InstalledRuntimeCapabilities = Object.freeze({
|
|
realtime: Object.freeze([]),
|
|
webWorkers: Object.freeze([]),
|
|
serviceWorker: Object.freeze({
|
|
mode: "ACTIVE" as const,
|
|
scriptPath: SERVICE_WORKER_SCRIPT_PATH,
|
|
handlers: Object.freeze(["PWA_STATIC_ASSETS" as const]),
|
|
}),
|
|
offlineCommands: null,
|
|
});
|
|
|
|
describe("runtime capability snapshot", () => {
|
|
it("describes every capability id in a fixed order", () => {
|
|
const snapshot = describeRuntimeCapabilities(EMPTY, DEFAULTS);
|
|
|
|
expect(snapshot.map((status) => status.capabilityId)).toEqual([
|
|
"REALTIME",
|
|
"WEB_WORKER",
|
|
"SERVICE_WORKER",
|
|
"OFFLINE_COMMANDS",
|
|
]);
|
|
});
|
|
|
|
it("reports nothing selected and nothing active for an empty selection", () => {
|
|
const snapshot = describeRuntimeCapabilities(EMPTY, DEFAULTS);
|
|
|
|
for (const status of snapshot) {
|
|
expect(status.selected).toBe(0);
|
|
expect(status.active).toBe(0);
|
|
expect(status.override).toBe("DEFAULT");
|
|
}
|
|
});
|
|
|
|
it("keeps a selected capability active while the override is DEFAULT", () => {
|
|
const snapshot = describeRuntimeCapabilities(SELECTED, DEFAULTS);
|
|
const serviceWorker = snapshot.find(
|
|
(status) => status.capabilityId === "SERVICE_WORKER",
|
|
);
|
|
|
|
expect(serviceWorker).toMatchObject({
|
|
selected: 1,
|
|
active: 1,
|
|
override: "DEFAULT",
|
|
});
|
|
});
|
|
|
|
it("keeps a disabled capability selected but not active", () => {
|
|
const snapshot = describeRuntimeCapabilities(SELECTED, {
|
|
...DEFAULTS,
|
|
SERVICE_WORKER: "DISABLED",
|
|
});
|
|
const serviceWorker = snapshot.find(
|
|
(status) => status.capabilityId === "SERVICE_WORKER",
|
|
);
|
|
|
|
expect(serviceWorker).toMatchObject({
|
|
selected: 1,
|
|
active: 0,
|
|
override: "DISABLED",
|
|
});
|
|
});
|
|
|
|
it("cannot activate a capability that was never selected", () => {
|
|
const snapshot = describeRuntimeCapabilities(EMPTY, {
|
|
...DEFAULTS,
|
|
REALTIME: "DEFAULT",
|
|
});
|
|
const realtime = snapshot.find(
|
|
(status) => status.capabilityId === "REALTIME",
|
|
);
|
|
|
|
expect(realtime).toMatchObject({ selected: 0, active: 0 });
|
|
});
|
|
|
|
it("returns a frozen snapshot and frozen entries", () => {
|
|
const snapshot = describeRuntimeCapabilities(EMPTY, DEFAULTS);
|
|
|
|
expect(Object.isFrozen(snapshot)).toBe(true);
|
|
expect(snapshot.every((status) => Object.isFrozen(status))).toBe(true);
|
|
});
|
|
});
|