Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"deps": "bun run --cwd ../api build && bun run --cwd ../moq/js/libav build",
"fix": "biome check --fix",
"tauri": "tauri",
"test": "bun test src/room/metadata.test.ts"
"test": "bun test --isolate --conditions=browser --preload ./test/setup.ts src"
},
"license": "MIT",
"dependencies": {
Expand Down Expand Up @@ -48,15 +48,22 @@
"@moq/clock": "workspace:*"
},
"devDependencies": {
"@babel/core": "7.29.7",
"@babel/preset-typescript": "^7.29.0",
"@biomejs/biome": "^2.4.3",
"@happy-dom/global-registrator": "21.0.4",
"@iconify-json/material-symbols": "^1.2.33",
"@iconify-json/mdi": "^1.2.3",
"@iconify/tailwind4": "^1.0.6",
"@tailwindcss/typography": "^0.5.16",
"@tauri-apps/cli": "^2.8.4",
"@types/babel__core": "7.20.5",
"@types/bun": "^1.4.0",
"@typescript/lib-dom": "npm:@types/web@^0.0.345",
"babel-preset-solid": "1.9.15",
"globals": "^17.4.0",
"madge": "^8.0.0",
"rolldown": "^1.2.15",
"ts-expect": "^1.3.0",
"tsx": "^4.20.5",
"typescript": "^6.0.0",
Expand All @@ -65,9 +72,7 @@
"vite-plugin-solid": "^2.11.8",
"vite-plugin-static-copy": "^4.0.0",
"vite-plugin-wrangler": "^0.1.1",
"wrangler": "^4.33.0",
"@types/bun": "^1.4.0",
"rolldown": "^1.2.15"
"wrangler": "^4.33.0"
},
"packageManager": "bun@1.4.2"
}
84 changes: 84 additions & 0 deletions app/src/components/badge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { afterEach, expect, mock, test } from "bun:test";

mock.module("../tauri", () => ({ Api: null }));
const { Badge } = await import("./badge");
const setDescriptor = Object.getOwnPropertyDescriptor(navigator, "setAppBadge");
const clearDescriptor = Object.getOwnPropertyDescriptor(navigator, "clearAppBadge");
let badge: InstanceType<typeof Badge> | undefined;

async function flush() {
for (let i = 0; i < 30; i++) await Promise.resolve();
}

afterEach(async () => {
badge?.close();
badge = undefined;
await flush();
if (setDescriptor) Object.defineProperty(navigator, "setAppBadge", setDescriptor);
else Reflect.deleteProperty(navigator, "setAppBadge");
if (clearDescriptor) Object.defineProperty(navigator, "clearAppBadge", clearDescriptor);
else Reflect.deleteProperty(navigator, "clearAppBadge");
});

test("closing a badge clears it after its pending update and before a new owner's update", async () => {
const calls: (number | "clear")[] = [];
let release: (() => void) | undefined;
Object.defineProperty(navigator, "setAppBadge", {
configurable: true,
value: async (count: number) => {
calls.push(count);
if (count === 2)
await new Promise<void>((resolve) => {
release = resolve;
});
},
});
Object.defineProperty(navigator, "clearAppBadge", {
configurable: true,
value: async () => {
calls.push("clear");
},
});
badge = new Badge();
badge.set(2);
await flush();
expect(calls).toEqual([2]);
badge.close();
badge = new Badge();
badge.set(1);
await flush();
expect(calls).toEqual([2]);
if (!release) throw new Error("Missing pending badge operation");
release();
await flush();
expect(calls).toEqual([2, "clear", 1]);
badge.close();
await flush();
expect(calls).toEqual([2, "clear", 1, "clear"]);
});

test("a synchronous platform failure does not stop later badge updates or clears", async () => {
const calls: (number | "clear")[] = [];
Object.defineProperty(navigator, "setAppBadge", {
configurable: true,
value: (count: number) => {
if (count === 2) throw new Error("platform unavailable");
calls.push(count);
return Promise.resolve();
},
});
Object.defineProperty(navigator, "clearAppBadge", {
configurable: true,
value: async () => {
calls.push("clear");
},
});
badge = new Badge();
badge.set(2);
badge.set(3);
badge.close();
badge = new Badge();
badge.set(1);
await flush();
expect(calls).toEqual([3, "clear", 1]);
});
30 changes: 16 additions & 14 deletions app/src/components/badge.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
let lock = false;

import * as Tauri from "../tauri";

async function set(count: number | undefined) {
let lock = false;
// A closed owner's pending writes must finish before the next owner's writes.
let pending = Promise.resolve();

async function set(count: number): Promise<void> {
if (Tauri.Api) {
const success = await Tauri.Api.window
.getCurrentWindow()
.setBadgeCount(count || undefined)
const success = await Promise.resolve()
.then(() => Tauri.Api?.window.getCurrentWindow().setBadgeCount(count || undefined))
.then(() => true)
.catch(() => false);
if (success) return;
}

if (navigator.setAppBadge) {
const success = await navigator
.setAppBadge(count || undefined)
if (count ? navigator.setAppBadge : navigator.clearAppBadge) {
const success = await Promise.resolve()
.then(() => (count ? navigator.setAppBadge(count) : navigator.clearAppBadge()))
.then(() => true)
.catch(() => false);
if (success) return;
Expand All @@ -34,7 +35,7 @@ async function set(count: number | undefined) {
export class Badge {
private current: number = 0;

#p?: Promise<void> = Promise.resolve();
#closed = false;

constructor() {
if (lock) throw new Error("Badge is already locked");
Expand All @@ -50,9 +51,9 @@ export class Badge {
* @param count - The number to display on the badge, or 0 to clear
*/
set(count: number): void {
if (this.#closed) throw new Error("closed");
this.current = count;
if (!this.#p) throw new Error("closed");
this.#p = this.#p.then(() => set(count));
pending = pending.then(() => set(count));
}

/**
Expand All @@ -72,8 +73,9 @@ export class Badge {
}

close() {
this.current = 0;
this.#p = undefined;
if (this.#closed) return;
this.set(0);
this.#closed = true;
lock = false;
}
}
156 changes: 156 additions & 0 deletions app/src/home.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { afterEach, expect, mock, test } from "bun:test";
import * as Json from "@moq/json";
import * as Moq from "@moq/net";
import { Signal } from "@moq/signals";
import type { JSX } from "solid-js";
import { render } from "solid-js/web";

// Exercise the real Home, favorite cards, and preview rosters without API or relay I/O.
const origin = new Moq.Origin.Producer();
const authenticated = new Signal(true);
let favorites = [
{ room: "room", created_at: 0 },
{ room: "roommate", created_at: 0 },
];
const badgeCounts: (number | "clear" | "flag")[] = [];
Object.defineProperty(navigator, "setAppBadge", {
configurable: true,
value: async (count?: number) => {
badgeCounts.push(count ?? "flag");
},
});
const clearBadge = mock(async () => {
badgeCounts.push("clear");
});
Object.defineProperty(navigator, "clearAppBadge", {
configurable: true,
value: clearBadge,
});

mock.module("./api", () => ({
client: {
authenticated,
routes: {
fave: {
all: { $get: async () => ({ ok: true, json: async () => ({ favorites }) }) },
":room": {
remove: {
$post: async ({ param }: { param: { room: string } }) => {
favorites = favorites.filter((favorite) => favorite.room !== param.room);
return { ok: true };
},
},
},
},
},
},
}));
mock.module("./components/create", () => ({ default: () => null }));
mock.module("./components/login", () => ({ default: () => null }));
mock.module("./components/profile", () => ({ default: () => null }));
mock.module("./layout/web", () => ({ default: (props: { children: JSX.Element }) => props.children }));
mock.module("./room/local", () => ({
Local: class {
close() {}
},
}));
mock.module("./tauri", () => ({ Api: null }));
mock.module("../../moq/js/watch/src/audio/render-worklet.ts?worklet", () => ({
default: async () => "blob:fake-render",
}));
mock.module("../../moq/js/publish/src/audio/capture-worklet.ts?worklet", () => ({
default: async () => "blob:fake-capture",
}));
mock.module("../../moq/js/publish/src/video/capture-worker.ts?worklet", () => ({
default: async () => "blob:fake-worker",
}));

// Keep a real Connection (and its complete type), but supply the in-memory origin.
const originalConnection = Moq.Connection;
mock.module("@moq/net", () => ({
...Moq,
Connection: class extends originalConnection {
override readonly origin = new Signal<Moq.Origin.Table | undefined>(origin);

constructor() {
super({ enabled: false });
}
},
}));
const { Home } = await import("./home");

// MoQ notifications and Solid resource/effect work settle through microtasks.
async function flush() {
for (let i = 0; i < 50; i++) await Promise.resolve();
}

const routes: Moq.Origin.Route[] = [];
const catalogs: Json.Snapshot.Producer<object>[] = [];
function announce(path: string): Moq.Origin.Route {
const route = origin.route(Moq.Path.from(path), { hidden: true });
const catalog = new Json.Snapshot.Producer<object>({ track: route.broadcast.createTrack("catalog.json") });
catalog.update({ value: {} });
catalogs.push(catalog);
routes.push(route);
route.announce();
return route;
}
let dispose: (() => void) | undefined;
afterEach(async () => {
dispose?.();
dispose = undefined;
for (const catalog of catalogs) catalog.finish();
for (const route of routes) route.close();
origin.close();
await flush();
document.body.replaceChildren();
});

test("favorites scope sibling rooms and balance the badge through joins, leaves, removal, and disposal", async () => {
const element = document.createElement("div");
document.body.append(element);
dispose = render(Home, element);
await flush();
const roomRoute = announce("room/alice/camera.hang");
await flush();
const cards = () => [...element.querySelectorAll("a[href^='/@']")].map((link) => link.parentElement?.parentElement);
const counts = () => cards().map((card) => card?.querySelector("span.text-gray-400")?.textContent ?? "0");
expect(counts()).toEqual(["1", "0"]);
expect(badgeCounts.at(-1)).toBe(1);

announce("roommate/bob/camera.hang");
await flush();
expect(counts()).toEqual(["1", "1"]);
expect(badgeCounts.at(-1)).toBe(2);

badgeCounts.length = 0;
const secondRoomRoute = announce("room/carol/camera.hang");
await flush();
expect(counts()).toEqual(["2", "1"]);
expect(badgeCounts).toEqual([3]);

roomRoute.close();
await flush();
expect(counts()).toEqual(["1", "1"]);
expect(badgeCounts.at(-1)).toBe(2);
secondRoomRoute.close();
await flush();
expect(counts()).toEqual(["0", "1"]);
expect(badgeCounts.at(-1)).toBe(1);

const remove = cards()[1]?.querySelector("button");
if (!remove) throw new Error("Missing favorite remove button");
remove.click();
await flush();
expect(cards()).toHaveLength(1);
expect(badgeCounts.at(-1)).toBe("clear");
expect(clearBadge).toHaveBeenCalled();

announce("room/dave/camera.hang");
await flush();
expect(badgeCounts.at(-1)).toBe(1);
dispose();
dispose = undefined;
await flush();
expect(badgeCounts.at(-1)).toBe("clear");
});
13 changes: 8 additions & 5 deletions app/src/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,16 @@ function FavoriteRoom(props: {
}): JSX.Element {
const [removing, setRemoving] = createSignal(false);
const [memberCount, setMemberCount] = createSignal(0);
let contribution = 0;

createEffect(() => {
const count = memberCount();
props.badge.increment(count);
return () => {
props.badge.decrement(count);
};
if (count === contribution) return;
props.badge.increment(count - contribution);
contribution = count;
});
onCleanup(() => {
if (contribution) props.badge.decrement(contribution);
});

const handleRemove = async (e: MouseEvent) => {
Expand Down Expand Up @@ -216,7 +219,7 @@ function FavoriteRoom(props: {
</button>
</div>
</div>
<PreviewRoomCompact connection={props.connection} setMemberCount={setMemberCount} />
<PreviewRoomCompact connection={props.connection} path={props.room} setMemberCount={setMemberCount} />
</div>
);
}
2 changes: 1 addition & 1 deletion app/src/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Moq from "@moq/net";
import { createAccessor as solid } from "@moq/signals/solid";
import { createEffect, For, onCleanup, Setter, Show } from "solid-js";
import { createEffect, For, onCleanup, type Setter, Show } from "solid-js";
import type { JSX } from "solid-js/jsx-runtime";
import * as Preview from "./room/preview";

Expand Down
Loading
Loading