Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

test(shared): cover read, reaction and upload boundaries (#1189)

Generc701a6161dae
diff --git a/scripts/ci/upload-google-play.test.ts b/scripts/ci/upload-google-play.test.ts
index b4ccd23c9..ce2ccacad 100644
--- a/scripts/ci/upload-google-play.test.ts
+++ b/scripts/ci/upload-google-play.test.ts
@@ -33,6 +33,7 @@ describe("verified bundle to Google Play internal draft", () => {
it("uploads the bound bytes, preserves active releases and reads the committed draft/hash back", async () => {
const { calls, request } = fixture()
const progress: Record<string, unknown> = {}
+ const expectedBytes = Buffer.from(bytes)
const result = await uploadDraft(receipt, bytes, "offline-token", request, progress)
expect(result).toEqual({ package: "ai.alook.android", track: "internal", status: "draft", versionCode: 10,
sha256: hash, editId: "write-edit", committed: true, readback: true })
@@ -49,7 +50,7 @@ describe("verified bundle to Google Play internal draft", () => {
["GET", "/androidpublisher/v3/applications/ai.alook.android/edits/read-edit/bundles"],
["DELETE", "/androidpublisher/v3/applications/ai.alook.android/edits/read-edit"],
])
- expect(calls[3].options.body).toBe(bytes)
+ expect(calls[3].options.body).toEqual(expectedBytes)
expect(new URL(calls[3].url).searchParams.get("uploadType")).toBe("media")
expect(JSON.parse(calls[5].options.body as string)).toEqual({ track: "internal", releases: [previous, publishedDraft] })
expect(calls[6].options.body).toBeUndefined()
@@ -75,8 +76,8 @@ describe("verified bundle to Google Play internal draft", () => {
expect(calls).toHaveLength(0)
})
- it.each([10, 11])("refuses reused or lower version code when Play already has %s", async versionCode => {
- const { request, calls } = fixture({ 1: { bundles: [{ versionCode }] } })
+ it.each([["bundles", 10], ["bundles", 11], ["apks", 10], ["apks", 11]] as const)("refuses reused or lower version code when Play %s already has %s", async (kind, versionCode) => {
+ const { request, calls } = fixture({ [kind === "bundles" ? 1 : 2]: { [kind]: [{ versionCode }] } })
await expect(uploadDraft(receipt, bytes, "offline-token", request)).rejects.toThrow("start a new workflow run")
expect(calls.filter(call => call.options.method === "POST")).toHaveLength(1)
expect(calls.at(-1)?.options.method).toBe("DELETE")
diff --git a/src/shared/test/queries/community-mention-revision.test.ts b/src/shared/test/queries/community-mention-revision.test.ts
index c6bde1985..490a3f788 100644
--- a/src/shared/test/queries/community-mention-revision.test.ts
+++ b/src/shared/test/queries/community-mention-revision.test.ts
@@ -74,6 +74,10 @@ describe("community mention revision effects", () => {
it("guards bounded mention repairs and versions only real mark-all or dismiss effects", async () => {
expect(conditions()).toEqual({ throughOne: 1, exactFive: 1 })
+ sqlite.prepare("UPDATE community_mention SET read = 1 WHERE id = 'mention-1'").run()
+ expect(conditions()).toEqual({ throughOne: 0, exactFive: 1 })
+ sqlite.prepare("UPDATE community_mention SET read = 0 WHERE id = 'mention-1'").run()
+
;(markMessageMentionsReadBuilder(db, "u1", "m5") as any).run()
expect(conditions()).toEqual({ throughOne: 1, exactFive: 0 })
diff --git a/src/shared/test/queries/community-reaction-details.test.ts b/src/shared/test/queries/community-reaction-details.test.ts
index 33b5a0220..bc900c048 100644
--- a/src/shared/test/queries/community-reaction-details.test.ts
+++ b/src/shared/test/queries/community-reaction-details.test.ts
@@ -103,13 +103,15 @@ describe("getReactionDetailsActors against real SQLite", () => {
user("bot-live", { bot: true, owner: "owner-live" })
user("bot-orphan", { bot: true, owner: "owner-gone" })
user("outsider")
+ user("foreign-0001")
for (const id of ["active", "deleted", "bot-live", "bot-orphan"]) member("server-1", id)
member("server-2", "outsider")
+ member("server-1", "foreign-0001")
for (const id of ["active", "departed", "deleted", "bot-live", "bot-orphan", "outsider"]) {
reaction("message-1", id, "πŸ‘")
}
reaction("message-1", "active", "πŸ”₯")
- reaction("message-2", "outsider", "πŸŽ‰")
+ reaction("message-2", "foreign-0001", "πŸŽ‰")
const actors = await getReactionDetailsActors(db, "message-1", {
kind: "server",
@@ -137,6 +139,9 @@ describe("getReactionDetailsActors against real SQLite", () => {
user(id, { deleted: id === "deleted-dm" })
reaction("message-dm", id, "πŸ‘")
}
+ user("foreign-dm-0002")
+ participant("dm-1", "foreign-dm-0002")
+ reaction("message-other", "foreign-dm-0002", "πŸŽ‰")
participant("dm-1", "access")
participant("dm-1", "notify", "notify")
participant("dm-2", "other-dm")
diff --git a/src/shared/test/queries/community-read-state-bump-cursor.test.ts b/src/shared/test/queries/community-read-state-bump-cursor.test.ts
index 226fe5085..5073173b2 100644
--- a/src/shared/test/queries/community-read-state-bump-cursor.test.ts
+++ b/src/shared/test/queries/community-read-state-bump-cursor.test.ts
@@ -87,13 +87,15 @@ describe("bumpReadCursor", () => {
expect(db.__insertCalls[0].onConflict.setWhere).toBeDefined();
});
- it("idempotent β€” a lower/equal seq than what's already recorded is a no-op (never regresses)", async () => {
+ it.each([
+ { id: "m_2", createdAt: "2026-07-05T09:00:00.000Z", seq: 2 },
+ { id: "m_5", createdAt: "2026-07-05T10:00:00.000Z", seq: 5 },
+ ])("idempotent β€” a lower/equal seq than what's already recorded is a no-op (never regresses): $seq", async (incoming) => {
const db = createMockDb([
- [{ id: "m_2", createdAt: "2026-07-05T09:00:00.000Z", seq: 2 }], // resolves to an OLDER message
- [{ lastReadAt: "2026-07-05T10:00:00.000Z", lastReadMessageId: "m_5", lastReadSeq: 5 }], // already ahead
+ [incoming],
+ [{ lastReadAt: "2026-07-05T10:00:00.000Z", lastReadMessageId: "m_5", lastReadSeq: 5 }],
]);
- const result = await bumpReadCursor(db, "u_1", { channelId: "ch_1" }, 2);
- // MAX semantics: existing pointer wins, no write happens.
+ const result = await bumpReadCursor(db, "u_1", { channelId: "ch_1" }, incoming.seq);
expect(result).toEqual({ id: "m_5", createdAt: "2026-07-05T10:00:00.000Z", seq: 5 });
expect(db.insert).not.toHaveBeenCalled();
});
diff --git a/src/shared/test/queries/community-read-state-sqlite.test.ts b/src/shared/test/queries/community-read-state-sqlite.test.ts
index 121ed8885..0556391d9 100644
--- a/src/shared/test/queries/community-read-state-sqlite.test.ts
+++ b/src/shared/test/queries/community-read-state-sqlite.test.ts
@@ -1,9 +1,12 @@
import Sqlite from "better-sqlite3"
import { drizzle } from "drizzle-orm/better-sqlite3"
+import { eq } from "drizzle-orm"
+import { user } from "../../src/db/schema"
import { afterEach, beforeEach, describe, expect, it } from "vitest"
import type { Database } from "../../src/db"
import {
advanceReadStateRevisionBuilder,
+ advanceReadStateRevisionWhenAnyBuilder,
markReadToMessageBuilder,
getChannelReadStateSnapshot,
} from "../../src/db/queries/community/read-state"
@@ -15,6 +18,7 @@ describe("community read-state SQLite monotonicity", () => {
beforeEach(() => {
sqlite = new Sqlite(":memory:")
sqlite.exec(`
+ CREATE TABLE user (id TEXT PRIMARY KEY);
CREATE TABLE community_read_state (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
@@ -84,4 +88,28 @@ describe("community read-state SQLite monotonicity", () => {
await expect(getChannelReadStateSnapshot(db, { userId: "viewer", channelId: "missing" })).resolves.toEqual({ revision: 4, readState: null })
await expect(getChannelReadStateSnapshot(db, { userId: "missing", channelId: "c1" })).resolves.toEqual({ revision: 0, readState: null })
})
+
+ describe("advanceReadStateRevisionWhenAnyBuilder real SQLite", () => {
+ it.each([
+ { left: 0, right: 0, expectedRevision: 0 },
+ { left: 1, right: 0, expectedRevision: 1 },
+ { left: 0, right: 1, expectedRevision: 1 },
+ { left: 1, right: 1, expectedRevision: 1 },
+ ])("advances only the target account for effects $left/$right", ({ left, right, expectedRevision }) => {
+ sqlite.exec("INSERT INTO user (id) VALUES ('target'), ('neighbor')")
+ sqlite.exec("INSERT INTO community_read_state_revision (user_id, revision) VALUES ('neighbor', 9)")
+
+ const rows = (advanceReadStateRevisionWhenAnyBuilder(db, "target", [
+ eq(user.id, left ? "target" : "neighbor"),
+ eq(user.id, right ? "target" : "neighbor"),
+ ]) as any).all()
+
+ expect(rows).toEqual(expectedRevision === 0 ? [] : [{ revision: 1 }])
+ expect(sqlite.prepare(`
+ SELECT user_id AS userId, revision FROM community_read_state_revision ORDER BY user_id
+ `).all()).toEqual(expectedRevision === 0
+ ? [{ userId: "neighbor", revision: 9 }]
+ : [{ userId: "neighbor", revision: 9 }, { userId: "target", revision: 1 }])
+ })
+ })
})
diff --git a/src/shared/test/queries/community-read-state.test.ts b/src/shared/test/queries/community-read-state.test.ts
index 59072a9f7..579c8be0e 100644
--- a/src/shared/test/queries/community-read-state.test.ts
+++ b/src/shared/test/queries/community-read-state.test.ts
@@ -1,7 +1,6 @@
import { describe, it, expect, vi } from "vitest";
import * as readStateQueries from "../../src/db/queries/community/read-state";
import { communityReadState } from "../../src/db/community-schema";
-import { sql } from "drizzle-orm";
// `markReadToMessageBuilder` is the canonical channel/DM read-state upsert
// under the invariant unification (plan #4). It's used both stand-alone (DM
@@ -18,21 +17,6 @@ function createInsertBuilderMock() {
return chain;
}
-describe("community/read-state exports", () => {
- it("exports markReadToMessageBuilder + markReadToMessage", () => {
- expect(typeof readStateQueries.markReadToMessage).toBe("function");
- });
-
- it("builds one revision guard from multiple canonical effects", () => {
- const db = makeMassMarkDbMock();
- expect(readStateQueries.advanceReadStateRevisionWhenAnyBuilder(
- db,
- "u_1",
- [sql`1`, sql`0`],
- )).toBeDefined();
- });
-});
-
describe("markReadToMessageWithRevision", () => {
it("batches one exact canonical message boundary with its account revision", async () => {
const db = makeMassMarkDbMock(12);
@@ -142,8 +126,6 @@ describe("markReadToMessageBuilder β€” channel branch", () => {
// unread predicate never sees a human's read.
lastReadSeq: 42,
});
- // The invariant: values and set carry the same aligned tuple.
- expect(valuesArg.lastReadAt).toBe(valuesArg.lastReadMessageId ? "2026-07-03T00:00:00Z" : undefined);
const conflictArg = db.onConflictDoUpdate.mock.calls[0][0];
expect(conflictArg.set).toMatchObject({
lastReadAt: "2026-07-03T00:00:00Z",
@@ -168,22 +150,6 @@ describe("markReadToMessageBuilder β€” channel branch", () => {
// partial-index targetWhere clause.
expect(conflictArg.targetWhere).toBeUndefined();
});
-
- it("includes a monotone setWhere so stale-createdAt PUTs cannot regress the pointer", () => {
- const db = createInsertBuilderMock();
- readStateQueries.markReadToMessageBuilder(db, {
- userId: "u_1",
- channelId: "c_1",
- message: { id: "m_42", createdAt: "2026-07-03T00:00:00Z", seq: 42 },
- });
- const conflictArg = db.onConflictDoUpdate.mock.calls[0][0];
- // The setWhere is a Drizzle SQL fragment; we can't easily assert its
- // exact structure without evaluating it, but its mere presence is
- // the load-bearing invariant here β€” its absence would silently
- // reintroduce the "channel switch β†’ return β†’ stale mid-viewport row
- // overwrites the newer pointer" bug.
- expect(conflictArg.setWhere).toBeDefined();
- });
});
describe("markReadToMessageBuilder β€” DM is a channel", () => {
@@ -364,19 +330,6 @@ describe("markAllServerChannelsRead", () => {
spy.mockRestore();
});
- it("each upsert carries a seq monotone guard", async () => {
- const db = makeMassMarkDbMock();
- const messageModule = await import("../../src/db/queries/community/message");
- const spy = vi.spyOn(messageModule, "getLatestMessagesByChannelIds").mockResolvedValue([
- { channelId: "c_a", id: "m_a_new", createdAt: "2026-07-05T10:00:00Z", seq: 20 },
- ]);
-
- await readStateQueries.markAllServerChannelsRead(db, "u_1", ["c_a"]);
- expect(db.__conflicts[0].setWhere).toBeDefined();
- expect(db.batch).toHaveBeenCalledTimes(1);
- spy.mockRestore();
- });
-
it("returns the current revision when the account has no DMs", async () => {
const db = makeMassMarkDbMock(13);
const dmModule = await import("../../src/db/queries/community/dm");
diff --git a/src/web/src/lib/community/jev-send-dedup.test.ts b/src/web/src/lib/community/jev-send-dedup.test.ts
index a0d84deaf..4fe3d3ab3 100644
--- a/src/web/src/lib/community/jev-send-dedup.test.ts
+++ b/src/web/src/lib/community/jev-send-dedup.test.ts
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import type { Database } from "@alook/shared"
-const mocks = vi.hoisted(() => ({ recent: vi.fn(), author: vi.fn(), pending: vi.fn(), previous: vi.fn(), decide: vi.fn(), create: vi.fn() }))
+const mocks = vi.hoisted(() => ({ recent: vi.fn(), author: vi.fn(), previous: vi.fn(), decide: vi.fn(), create: vi.fn() }))
vi.mock("@alook/shared", async () => {
const actual = await vi.importActual<typeof import("@alook/shared")>("@alook/shared")
return { ...actual, queries: { ...actual.queries, communityMessage: {
@@ -31,7 +31,6 @@ describe("bot send duplicate judgment", () => {
vi.resetAllMocks()
mocks.recent.mockResolvedValue([row(3), row(2, 90_000), row(1, 180_000)])
mocks.author.mockResolvedValue({ name: "helper", discriminator: "5678" })
- mocks.pending.mockResolvedValue([])
mocks.previous.mockResolvedValue([])
mocks.create.mockReturnValue({ name: "openrouter", decide: mocks.decide })
mocks.decide.mockResolvedValue({ answers: { duplicate: { type: "noul", noul: 0.9 } }, model: "test" })
@@ -46,7 +45,6 @@ describe("bot send duplicate judgment", () => {
recent_messages: [1, 2, 3].map((seq) => ({ handle: `other${seq}#1234`, content: `Release update ${seq}.` })),
})
expect(mocks.author).toHaveBeenCalledExactlyOnceWith(input.db, "bot")
- expect(mocks.pending).not.toHaveBeenCalled()
expect(mocks.previous).not.toHaveBeenCalled()
expect(mocks.recent).toHaveBeenCalledExactlyOnceWith(input.db, "channel")
expect(mocks.create.mock.calls[0][0].threshold).toBe(0.5)