Public: anyone can read this; the crew can change it.

A client's app list orders apps by last change, newest first, and a test pins the order #15185

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/15167-a-client-s-app-list-orders-apps-by-last → main 2 commits fork 2 threads · 2 unresolved

  • Merged. torque/15167-a-client-s-app-list-orders-apps-by-last was merged into main as 8a65a5f.

Review

Sign in to review this change.
3 files changed +83 −7
src/core/client-apps.ts +21 −3
@@ -10,7 +10,7 @@ import { ago } from "../feed/rank.ts";
1010 import { appsRuntime, envsFor } from "../apps/runtime.ts";
1111 import { appsForRepos } from "./apps.ts";
1212 import { spawnedRepos } from "./next-steps.ts";
13 import { listReposOwnedBy, withoutArchived, type Repo } from "./repos.ts";
13 import { listReposOwnedBy, repoDatesFor, withoutArchived, type Repo } from "./repos.ts";
1414 import type { User } from "./users.ts";
1515
1616 export interface ClientApp {
@@ -25,7 +25,8 @@ export interface ClientApp {
2525
2626 /**
2727 * The apps `user` can see on a spawned forge, each with its agent counts; archived ones are put
28 * away, not shown. Their own come first (a first ask makes one under their name), then the forge's.
28 * away, not shown. Newest change first (the latest push), then by name; an app never pushed to
29 * sorts last, so what a client sees first is the apps with agents working on them.
2930 */
3031 export async function clientAppRows(
3132 app: App,
@@ -33,7 +34,10 @@ export async function clientAppRows(
3334 ): Promise<readonly ClientApp[]> {
3435 const own = listReposOwnedBy(app.db, user.id);
3536 const mine = new Set(own.map((r) => r.id));
36 const repos = withoutArchived([...own, ...(await spawnedRepos(app, user)).filter((r) => !mine.has(r.id))]);
37 const repos = byLastChange(
38 app.db,
39 withoutArchived([...own, ...(await spawnedRepos(app, user)).filter((r) => !mine.has(r.id))]),
40 );
3741 if (repos.length === 0) return [];
3842 const rows = app.db
3943 .query<{ repo_id: string; state: string; n: number }, string[]>(
@@ -59,6 +63,20 @@ export async function clientAppRows(
5963 });
6064 }
6165
66 /** `repos` by latest push, newest first; never pushed last; ties by name, then id. */
67 export function byLastChange(db: Database, repos: readonly Repo[]): Repo[] {
68 const dates = repoDatesFor(db, repos.map((r) => r.id));
69 const last = (r: Repo): number => {
70 const at = Date.parse(dates.get(r.id)?.pushedAt ?? "");
71 return Number.isNaN(at) ? -Infinity : at;
72 };
73 return [...repos].sort((a, b) => {
74 const [x, y] = [last(a), last(b)];
75 if (x !== y) return y > x ? 1 : -1;
76 return a.name.localeCompare(b.name) || a.id.localeCompare(b.id);

Dead logic: x === y ? 0 : ... is unreachable inside if (x !== y). Write if (x !== y) return y > x ? 1 : -1;. Do not subtract the two values, because -Infinity - -Infinity is NaN.

77 });
78 }
79
6280 /** What one app's agents are doing now, in words a client can read. */
6381 export interface ClientDoing {
6482 readonly title: string;
test/client-apps-order.test.ts added +53
@@ -0,0 +1,53 @@
1 /** A client's app list: newest change first, then name; never changed sorts last (#15167). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { clientAppRows } from "../src/core/client-apps.ts";
8 import { createUser, type User } from "../src/core/users.ts";
9
10 let dir = "";
11 let app: App;
12 let user: User;
13
14 beforeAll(async () => {
15 dir = await mkdtemp(join(tmpdir(), "forge-client-order-"));
16 app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 });
17 const made = await createUser(app.db, {
18 origin: "seed",
19 trusted: true,
20 username: "cora",
21 email: "[email protected]",
22 password: "a-long-first-login-passphrase",
23 });
24 if (!made.ok) throw new Error("user");
25 user = made.value;
26 // Named so that name order (apple, mango, zebra) is not the change order.
27 const apps: [string, string | null][] = [
28 ["apple", "2026-10-01T00:00:00.000Z"],
29 ["zebra", "2026-10-03T00:00:00.000Z"],
30 ["mango", "2026-10-02T00:00:00.000Z"],
31 ["berry", null],
32 ];
33 for (const [name, pushed] of apps) {
34 app.db.run("INSERT INTO repo (id, owner_id, name) VALUES (?, ?, ?)", [name, user.id, name]);
35 if (pushed !== null)
36 app.db.run("INSERT INTO event (id, repo_id, kind, payload, created_at) VALUES (?, ?, 'push', '{}', ?)", [`e-${name}`, name, pushed]);
37 }
38 });
39 afterAll(async () => {
40 app.db.close();
41 await rm(dir, { recursive: true, force: true });
42 });
43
44 test("apps come newest change first; one never changed comes last", async () => {
45 const rows = await clientAppRows(app, user);
46 expect(rows.map((r) => r.repo.name)).toEqual(["zebra", "mango", "apple", "berry"]);
47 });
48
49 test("equal changes fall back to name", async () => {
50 app.db.run("UPDATE event SET created_at = '2026-10-02T00:00:00.000Z' WHERE repo_id IN ('apple','zebra')");
51 const rows = await clientAppRows(app, user);
52 expect(rows.map((r) => r.repo.name)).toEqual(["apple", "mango", "zebra", "berry"]);
53 });
test/client-home.test.ts +9 −4
@@ -478,20 +478,25 @@ describe("a client's first screen", () => {
478478 app.stop();
479479 });
480480
481 test("an app made under the person's own name is on the first screen, ahead of the forge's", async () => {
481 test("an app made under the person's own name is on the first screen with the forge's", async () => {
482482 const maria = getUserByName(child.db, "maria")!;
483483 // What a first ask from the home makes: an app of maria's, not of the organisation's.
484484 const made = await createRepo(child, maria, { name: "countdown" });
485485 if (!made.ok) throw new Error(JSON.stringify(made.error));
486 // The bakery has a change; the new app has none yet, so it comes after.
487 const bakeryId = child.db.query<{ id: string }, []>("SELECT id FROM repo WHERE name = 'bakery'").get()!.id;
488 child.db.run("INSERT INTO event (id, repo_id, kind, payload) VALUES ('order-push', ?, 'push', '{}')", [bakeryId]);
486489 try {
487490 const names = (await clientAppRows(child, maria)).map((r) => `${r.repo.ownerName}/${r.repo.name}`);
488 expect(names[0]).toBe("maria/countdown");
489 expect(names).toContain("marias/bakery");
491 expect(names.indexOf("marias/bakery")).toBeGreaterThanOrEqual(0);
492 expect(names.indexOf("marias/bakery")).toBeLessThan(names.indexOf("maria/countdown"));
490493 const home = main(await page(owner));
491494 const apps = home.slice(home.indexOf('id="client-apps"'));
outdated

This edit drops the only assertion that the web home lists the apps in the order the sort produces; it now only checks that both links are present. Pin the order on the page too, for example by giving marias/bakery a push event and asserting its link comes before maria/countdown (a never-pushed app sorts last). That also covers the page door, not just clientAppRows.

492495 expect(apps).toContain('href="/maria/countdown"');
493 expect(apps.indexOf('href="/maria/countdown"')).toBeLessThan(apps.indexOf('href="/marias/bakery"'));
496 expect(apps.indexOf('href="/marias/bakery"')).toBeGreaterThanOrEqual(0);
497 expect(apps.indexOf('href="/marias/bakery"')).toBeLessThan(apps.indexOf('href="/maria/countdown"'));
494498 } finally {
499 child.db.run("DELETE FROM event WHERE id = 'order-push'");
495500 expect(moveArchived(child.db, made.value.id, true)).toBe(true);
496501 }
497502 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs