@marktiderman/genesis-chat 0.11.4 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +162 -4
- package/dist/{chunk-ZQFREERR.js → chunk-3UNYTK62.js} +3 -3
- package/dist/{chunk-ZQFREERR.js.map → chunk-3UNYTK62.js.map} +1 -1
- package/dist/{chunk-5XYDVUPS.js → chunk-GEEJMKQF.js} +29 -4
- package/dist/chunk-GEEJMKQF.js.map +1 -0
- package/dist/{chunk-D4RPOYB7.js → chunk-H4BRVJJ7.js} +193 -30
- package/dist/chunk-H4BRVJJ7.js.map +1 -0
- package/dist/{chunk-O2QSBRSH.js → chunk-SEMN3S6Q.js} +3 -3
- package/dist/{chunk-O2QSBRSH.js.map → chunk-SEMN3S6Q.js.map} +1 -1
- package/dist/{client-DP7rhJ1U.d.ts → client-DXWyDJ2u.d.ts} +29 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/native.d.ts +355 -145
- package/dist/native.js +701 -217
- package/dist/native.js.map +1 -1
- package/dist/{outbox-C4LnqpKT.d.ts → outbox-Ctngv6Yz.d.ts} +1 -1
- package/dist/push.d.ts +2 -2
- package/dist/react.d.ts +83 -6
- package/dist/react.js +3 -3
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/{thread-CSzdqNR_.d.ts → thread-mZsRiIGz.d.ts} +1 -1
- package/dist/web.d.ts +24 -7
- package/dist/web.js +140 -67
- package/dist/web.js.map +1 -1
- package/migrations/0006_genesis_chat_read_receipts.sql +110 -0
- package/migrations/0007_genesis_chat_unread_and_delete_for_me.sql +455 -0
- package/migrations/tests/0006_genesis_chat_read_receipts.test.sql +131 -0
- package/migrations/tests/0007_genesis_chat_unread_and_delete_for_me.test.sql +178 -0
- package/package.json +17 -9
- package/src/__tests__/inbox-row-actions.test.tsx +251 -0
- package/src/__tests__/unread-delete-for-me.test.ts +84 -0
- package/src/__tests__/web.test.tsx +5 -2
- package/src/core/client.ts +30 -1
- package/src/core/types.ts +7 -0
- package/src/native/__tests__/bubble-native-feel.test.tsx +153 -0
- package/src/native/__tests__/composer-attach-menu.test.tsx +47 -0
- package/src/native/__tests__/inbox-screen-actions.test.tsx +131 -0
- package/src/native/__tests__/inbox-screen.test.tsx +29 -1
- package/src/native/__tests__/media-cache-key.test.tsx +21 -0
- package/src/native/__tests__/organise-screens.test.tsx +2 -0
- package/src/native/__tests__/receipts.test.ts +95 -0
- package/src/native/__tests__/review-lows.test.tsx +80 -0
- package/src/native/__tests__/sheets-fit.test.tsx +45 -0
- package/src/native/__tests__/stable-hooks.test.tsx +68 -0
- package/src/native/__tests__/thread-layout.test.ts +2 -2
- package/src/native/__tests__/thread-screen-audit.test.tsx +248 -0
- package/src/native/__tests__/thread-screen.test.tsx +111 -0
- package/src/native/__tests__/thread-title.test.tsx +59 -0
- package/src/native/composer.tsx +61 -26
- package/src/native/copy.ts +47 -10
- package/src/native/emoji-picker.tsx +1 -1
- package/src/native/inbox-screen.tsx +52 -27
- package/src/native/index.ts +7 -2
- package/src/native/media.tsx +7 -1
- package/src/native/message-actions-sheet.tsx +1 -1
- package/src/native/message-bubble.tsx +171 -19
- package/src/native/modules.ts +3 -1
- package/src/native/receipts.ts +102 -0
- package/src/native/schedule-sheet.tsx +8 -3
- package/src/native/stack-options.ts +35 -3
- package/src/native/thread-screen.tsx +243 -17
- package/src/native/thread-times.tsx +89 -0
- package/src/native/thread-title.tsx +111 -0
- package/src/native/typing-bubble.tsx +85 -0
- package/src/react/inbox-actions.ts +121 -0
- package/src/react/index.tsx +172 -23
- package/src/web/inbox.tsx +120 -11
- package/src/web/index.ts +1 -1
- package/src/web/memory-client.ts +40 -7
- package/dist/chunk-5XYDVUPS.js.map +0 -1
- package/dist/chunk-D4RPOYB7.js.map +0 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// @feat:chat
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import type { ThreadItem } from "../../core/thread";
|
|
4
|
+
import type { ChatMember, ChatMessage, PendingMessage } from "../../core/types";
|
|
5
|
+
import { chatNativeCopy } from "../copy";
|
|
6
|
+
import { lastSentReceipt, readWhen, receiptText } from "../receipts";
|
|
7
|
+
|
|
8
|
+
const ME = "me";
|
|
9
|
+
const SAM = "sam";
|
|
10
|
+
const LEE = "lee";
|
|
11
|
+
const KIM = "kim";
|
|
12
|
+
|
|
13
|
+
function msg(seq: number, senderId: string, extra: Partial<ChatMessage> = {}): ThreadItem {
|
|
14
|
+
return {
|
|
15
|
+
type: "message",
|
|
16
|
+
message: { id: `m${seq}`, seq, clientId: `c${seq}`, senderId, kind: "text", body: `b${seq}`, createdAt: "2026-11-22T09:00:00Z", deletedAt: null, attachments: [], reactions: [], ...extra } as ChatMessage,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function pending(clientId: string, status: PendingMessage["status"], extra: Record<string, unknown> = {}): ThreadItem {
|
|
21
|
+
return { type: "pending", pending: { clientId, input: { conversationId: "conv", clientId, body: "x" }, senderId: ME, createdAt: "2026-11-22T09:05:00Z", status, ...extra } as PendingMessage };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function member(userId: string, lastReadSeq: number, lastReadAt: string | null = null, role: ChatMember["role"] = "member"): ChatMember {
|
|
25
|
+
return { conversationId: "conv", userId, role, lastReadSeq, lastReadAt, joinedAt: "2026-11-01T00:00:00Z" };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const direct = (samRead: number, at: string | null = null) => ({ me: ME, members: [member(ME, 99), member(SAM, samRead, at)], group: false });
|
|
29
|
+
|
|
30
|
+
describe("lastSentReceipt", () => {
|
|
31
|
+
it("is Sending… while the last message you sent is still on its way", () => {
|
|
32
|
+
expect(lastSentReceipt([msg(1, SAM), pending("p1", "sending")], direct(0))).toEqual({ key: "c:p1", receipt: { state: "sending" } });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("is Sent once it is posted but the outbox still holds it (a video still uploading)", () => {
|
|
36
|
+
expect(lastSentReceipt([pending("p1", "sending", { sentMessageId: "m9" })], direct(0))?.receipt).toEqual({ state: "sent" });
|
|
37
|
+
expect(lastSentReceipt([msg(2, ME)], { ...direct(0), queued: new Set(["c2"]) })?.receipt).toEqual({ state: "sent" });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("is Delivered when the server has it and the other person hasn't read that far", () => {
|
|
41
|
+
expect(lastSentReceipt([msg(1, SAM), msg(2, ME)], direct(1))).toEqual({ key: "c:c2", receipt: { state: "delivered" } });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("is Read, with the server's read time, once their lastReadSeq reaches it", () => {
|
|
45
|
+
expect(lastSentReceipt([msg(2, ME)], direct(2, "2026-11-22T09:41:00Z"))?.receipt).toEqual({ state: "read", at: "2026-11-22T09:41:00Z" });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("is Read with no time when the server records none (before chat 0006)", () => {
|
|
49
|
+
expect(lastSentReceipt([msg(2, ME)], direct(5))?.receipt).toEqual({ state: "read", at: null });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("goes under your last message, even with replies after it, and skips deleted and system ones", () => {
|
|
53
|
+
const items = [msg(1, ME), msg(2, ME, { deletedAt: "2026-11-22T09:10:00Z" }), msg(3, null as unknown as string, { kind: "system" }), msg(4, SAM)];
|
|
54
|
+
expect(lastSentReceipt(items, direct(4))?.key).toBe("c:c1");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("shows nothing for a failed send (its bubble keeps the failure and Retry), or with none of yours", () => {
|
|
58
|
+
expect(lastSentReceipt([msg(1, ME), pending("p1", "failed")], direct(9))).toBeNull();
|
|
59
|
+
expect(lastSentReceipt([msg(1, SAM)], direct(9))).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("shows nothing when receipts are hidden", () => {
|
|
63
|
+
expect(lastSentReceipt([msg(1, ME)], { ...direct(9), receipts: "hide" })).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("in a group", () => {
|
|
67
|
+
const members = [member(ME, 9), member(SAM, 5), member(LEE, 3), member(KIM, 2), member("obs", 9, null, "observer")];
|
|
68
|
+
it("counts the others who have read it, observers aside", () => {
|
|
69
|
+
expect(lastSentReceipt([msg(3, ME)], { me: ME, members, group: true })?.receipt).toEqual({ state: "read-by", readers: [SAM, LEE], everyone: false });
|
|
70
|
+
});
|
|
71
|
+
it("is Read by everyone when every other member has", () => {
|
|
72
|
+
expect(lastSentReceipt([msg(2, ME)], { me: ME, members, group: true })?.receipt).toEqual({ state: "read-by", readers: [SAM, LEE, KIM], everyone: true });
|
|
73
|
+
});
|
|
74
|
+
it("is Delivered when nobody has yet", () => {
|
|
75
|
+
expect(lastSentReceipt([msg(6, ME)], { me: ME, members, group: true })?.receipt).toEqual({ state: "delivered" });
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("receiptText", () => {
|
|
81
|
+
const now = new Date("2026-11-22T12:00:00");
|
|
82
|
+
it("says each state", () => {
|
|
83
|
+
expect(receiptText({ state: "sending" }, chatNativeCopy)).toBe("Sending…");
|
|
84
|
+
expect(receiptText({ state: "sent" }, chatNativeCopy)).toBe("Sent");
|
|
85
|
+
expect(receiptText({ state: "delivered" }, chatNativeCopy)).toBe("Delivered");
|
|
86
|
+
expect(receiptText({ state: "read", at: null }, chatNativeCopy)).toBe("Read");
|
|
87
|
+
expect(receiptText({ state: "read-by", readers: ["a", "b"], everyone: false }, chatNativeCopy)).toBe("Read by 2");
|
|
88
|
+
expect(receiptText({ state: "read-by", readers: ["a"], everyone: true }, chatNativeCopy)).toBe("Read by everyone");
|
|
89
|
+
});
|
|
90
|
+
it("gives the time read today, and the day before that", () => {
|
|
91
|
+
const today = new Date("2026-11-22T09:41:00").toISOString();
|
|
92
|
+
expect(receiptText({ state: "read", at: today }, chatNativeCopy, now, "en-US")).toBe(`Read ${new Date(today).toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit" })}`);
|
|
93
|
+
expect(readWhen(new Date("2026-11-21T20:00:00").toISOString(), now)).toBe("Yesterday");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
// @feat:chat
|
|
3
|
+
import { act, cleanup, render, screen } from "@testing-library/react";
|
|
4
|
+
import { AccessibilityInfo, Platform, StyleSheet } from "react-native";
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import type { ThreadItem } from "../../core/thread";
|
|
7
|
+
import type { ChatMember, ChatMessage } from "../../core/types";
|
|
8
|
+
import { MessageBubble } from "../message-bubble";
|
|
9
|
+
import { lastSentReceipt } from "../receipts";
|
|
10
|
+
import { buildThreadRows, type ThreadRow } from "../rows";
|
|
11
|
+
import { ChatTypingBubble } from "../typing-bubble";
|
|
12
|
+
|
|
13
|
+
vi.mock("react-native", async (original) => ({ ...(await original<object>()), useWindowDimensions: () => ({ width: 390, height: 844, scale: 3, fontScale: 1 }) }));
|
|
14
|
+
|
|
15
|
+
const os = Platform.OS;
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
cleanup();
|
|
18
|
+
Platform.OS = os;
|
|
19
|
+
vi.restoreAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const ME = "11111111-1111-4111-8111-111111111111";
|
|
23
|
+
const SAM = "22222222-2222-4222-8222-222222222222";
|
|
24
|
+
|
|
25
|
+
function message(seq: number, senderId: string, extra: Partial<ChatMessage> = {}): ChatMessage {
|
|
26
|
+
return {
|
|
27
|
+
id: `m${seq}`, conversationId: "conv", seq, clientId: `c${seq}`, senderId, senderRole: "member", kind: "text", body: `body ${seq}`,
|
|
28
|
+
mentions: [], replyToId: null, threadRootId: null, threadCount: 0, threadLastAt: null, topic: null, visibility: "everyone",
|
|
29
|
+
silent: false, meta: {}, createdAt: `2026-10-25T10:0${seq}:00Z`, editedAt: null, deletedAt: null, attachments: [], reactions: [], ...extra,
|
|
30
|
+
} as unknown as ChatMessage;
|
|
31
|
+
}
|
|
32
|
+
const member = (userId: string, lastReadSeq: number) => ({ conversationId: "conv", userId, role: "member", lastReadSeq, lastReadAt: null, joinedAt: "2026-10-01T00:00:00Z" }) as ChatMember;
|
|
33
|
+
const styleOf = (el: Element) => StyleSheet.flatten(JSON.parse((el as HTMLElement).dataset.style ?? "null")) as Record<string, unknown> | undefined;
|
|
34
|
+
|
|
35
|
+
describe("a hidden message's status", () => {
|
|
36
|
+
it("is never shown: the others were never shown the message", () => {
|
|
37
|
+
const items: ThreadItem[] = [{ type: "message", message: message(1, ME) }, { type: "message", message: message(2, ME, { moderation: "hidden" } as Partial<ChatMessage>) }];
|
|
38
|
+
expect(lastSentReceipt(items, { me: ME, members: [member(ME, 2), member(SAM, 2)], group: false })).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("and neither is an observers-only message's: members who can't see it aren't its readers", () => {
|
|
42
|
+
const items: ThreadItem[] = [{ type: "message", message: message(1, ME, { visibility: "observers" } as Partial<ChatMessage>) }];
|
|
43
|
+
expect(lastSentReceipt(items, { me: ME, members: [member(ME, 1), member(SAM, 1)], group: false })).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("the receipt button", () => {
|
|
48
|
+
it("is a 44 pt target (28 pt tall, 8 pt of slop above and below)", () => {
|
|
49
|
+
const row = buildThreadRows([{ type: "message", message: message(1, ME) }], { me: ME, group: true }).find((x): x is Extract<ThreadRow, { type: "message" }> => x.type === "message")!;
|
|
50
|
+
render(<MessageBubble row={row} me={ME} urls={{}} group reducedMotion receipt="Read by 2" onReceiptPress={vi.fn()} testID="b" />);
|
|
51
|
+
expect(styleOf(screen.getByTestId("b-receipt"))?.minHeight).toBe(28);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("cuts the tail from the backdrop the screen draws", () => {
|
|
55
|
+
const row = buildThreadRows([{ type: "message", message: message(1, ME) }], { me: ME, group: false }).find((x): x is Extract<ThreadRow, { type: "message" }> => x.type === "message")!;
|
|
56
|
+
render(<MessageBubble row={row} me={ME} urls={{}} group={false} reducedMotion backdrop="rgb(1, 2, 3)" testID="t" />);
|
|
57
|
+
const cut = screen.getByTestId("t-tail").children[1]!;
|
|
58
|
+
expect(styleOf(cut)?.backgroundColor).toBe("rgb(1, 2, 3)");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("the typing bubble on iOS", () => {
|
|
63
|
+
it("is announced when someone starts typing (iOS has no live regions), not again for the same typer", () => {
|
|
64
|
+
Platform.OS = "ios";
|
|
65
|
+
const announce = vi.fn();
|
|
66
|
+
(AccessibilityInfo as { announceForAccessibility?: unknown }).announceForAccessibility = announce;
|
|
67
|
+
const { rerender } = render(<ChatTypingBubble label="Sam is typing…" userId={SAM} group={false} reducedMotion testID="ty" />);
|
|
68
|
+
expect(announce).toHaveBeenCalledWith("Sam is typing…");
|
|
69
|
+
act(() => rerender(<ChatTypingBubble label="Sam is typing…" userId={SAM} group={false} reducedMotion testID="ty" />));
|
|
70
|
+
expect(announce).toHaveBeenCalledTimes(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("leaves Android to its live region", () => {
|
|
74
|
+
Platform.OS = "android";
|
|
75
|
+
const announce = vi.fn();
|
|
76
|
+
(AccessibilityInfo as { announceForAccessibility?: unknown }).announceForAccessibility = announce;
|
|
77
|
+
render(<ChatTypingBubble label="Sam is typing…" userId={SAM} group={false} reducedMotion testID="ty" />);
|
|
78
|
+
expect(announce).not.toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
// @feat:chat
|
|
3
|
+
import { cleanup, render, screen, waitFor } from "@testing-library/react";
|
|
4
|
+
import { Platform } from "react-native";
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import type { ChatMessage } from "../../core/types";
|
|
7
|
+
import { ChatNativeProvider } from "../context";
|
|
8
|
+
import { ChatEmojiPickerSheet } from "../emoji-picker";
|
|
9
|
+
import { MessageActionsSheet } from "../message-actions-sheet";
|
|
10
|
+
|
|
11
|
+
vi.mock("react-native", async (original) => ({ ...(await original<object>()), useWindowDimensions: () => ({ width: 390, height: 844, scale: 3, fontScale: 1 }) }));
|
|
12
|
+
|
|
13
|
+
const os = Platform.OS;
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
cleanup();
|
|
16
|
+
Platform.OS = os;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const message = {
|
|
20
|
+
id: "m1", conversationId: "conv", seq: 1, clientId: "c1", senderId: "u2", senderRole: "member", kind: "text", body: "hi", mentions: [], replyToId: null,
|
|
21
|
+
threadRootId: null, threadCount: 0, threadLastAt: null, topic: null, visibility: "everyone", silent: false, meta: {}, createdAt: "2026-10-25T10:00:00Z",
|
|
22
|
+
editedAt: null, deletedAt: null, attachments: [], reactions: [],
|
|
23
|
+
} as unknown as ChatMessage;
|
|
24
|
+
|
|
25
|
+
describe("in-thread sheets on iOS", () => {
|
|
26
|
+
it("fit their content (a bottom panel with a grabber over a scrim), not the full page sheet", async () => {
|
|
27
|
+
Platform.OS = "ios";
|
|
28
|
+
render(
|
|
29
|
+
<ChatNativeProvider>
|
|
30
|
+
<MessageActionsSheet
|
|
31
|
+
message={message}
|
|
32
|
+
context={{ me: "u1", role: "member", canPost: true, pinned: false, canPin: false, canCopy: true }}
|
|
33
|
+
onClose={vi.fn()}
|
|
34
|
+
onReact={vi.fn()}
|
|
35
|
+
onAction={vi.fn()}
|
|
36
|
+
testID="actions"
|
|
37
|
+
/>
|
|
38
|
+
<ChatEmojiPickerSheet open onClose={vi.fn()} onSelect={vi.fn()} testID="emoji" />
|
|
39
|
+
</ChatNativeProvider>,
|
|
40
|
+
);
|
|
41
|
+
// Only the panel that fits its content has a scrim (the backdrop); the page sheet has none.
|
|
42
|
+
await waitFor(() => expect(screen.getByTestId("actions-backdrop")).toBeTruthy());
|
|
43
|
+
expect(screen.getByTestId("emoji-backdrop")).toBeTruthy();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
// @feat:chat
|
|
3
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
4
|
+
import { act, cleanup, render, waitFor } from "@testing-library/react";
|
|
5
|
+
import { useState, type ReactNode } from "react";
|
|
6
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
7
|
+
import type { ChatClient } from "../../core/client";
|
|
8
|
+
import { ChatProvider, useFileUrls, usePeople, usePins } from "../../react/index";
|
|
9
|
+
|
|
10
|
+
afterEach(cleanup);
|
|
11
|
+
|
|
12
|
+
const ME = "11111111-1111-4111-8111-111111111111";
|
|
13
|
+
|
|
14
|
+
function wrap(client: ChatClient, children: ReactNode) {
|
|
15
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
16
|
+
return (
|
|
17
|
+
<QueryClientProvider client={qc}>
|
|
18
|
+
<ChatProvider client={client} userId={ME}>
|
|
19
|
+
{children}
|
|
20
|
+
</ChatProvider>
|
|
21
|
+
</QueryClientProvider>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("the read hooks' empty results", () => {
|
|
26
|
+
it("are the same object on every render, so a row's memo holds while someone types", () => {
|
|
27
|
+
const client = { fileUrls: vi.fn(async () => ({})), pins: vi.fn(async () => []), subscribeInbox: vi.fn(() => ({ unsubscribe: vi.fn() })) } as unknown as ChatClient;
|
|
28
|
+
const seen: unknown[][] = [];
|
|
29
|
+
let rerender: () => void = () => undefined;
|
|
30
|
+
function Probe() {
|
|
31
|
+
const [, set] = useState(0);
|
|
32
|
+
rerender = () => set((n) => n + 1);
|
|
33
|
+
seen.push([useFileUrls([]), usePeople([]), usePins(null)]);
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
render(wrap(client, <Probe />));
|
|
37
|
+
act(() => rerender());
|
|
38
|
+
act(() => rerender());
|
|
39
|
+
const [first, ...rest] = seen;
|
|
40
|
+
for (const s of rest) s.forEach((v, i) => expect(v).toBe(first![i]));
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("useFileUrls", () => {
|
|
45
|
+
it("keeps the links it has while a new photo's link is signed, instead of blanking every photo", async () => {
|
|
46
|
+
let release: (v: Record<string, string>) => void = () => undefined;
|
|
47
|
+
const fileUrls = vi.fn(async (paths: string[]) => {
|
|
48
|
+
if (paths.length === 1) return { a: "https://x/a?sig=1" };
|
|
49
|
+
return new Promise<Record<string, string>>((r) => (release = r));
|
|
50
|
+
});
|
|
51
|
+
const client = { fileUrls, subscribeInbox: vi.fn(() => ({ unsubscribe: vi.fn() })) } as unknown as ChatClient;
|
|
52
|
+
const seen: Record<string, string>[] = [];
|
|
53
|
+
let setPaths: (p: string[]) => void = () => undefined;
|
|
54
|
+
function Probe() {
|
|
55
|
+
const [paths, set] = useState<string[]>(["a"]);
|
|
56
|
+
setPaths = set;
|
|
57
|
+
seen.push(useFileUrls(paths));
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
render(wrap(client, <Probe />));
|
|
61
|
+
await waitFor(() => expect(seen.at(-1)).toEqual({ a: "https://x/a?sig=1" }));
|
|
62
|
+
act(() => setPaths(["a", "b"]));
|
|
63
|
+
// Loading the new set: the old links are still there.
|
|
64
|
+
expect(seen.at(-1)).toEqual({ a: "https://x/a?sig=1" });
|
|
65
|
+
await act(async () => release({ a: "https://x/a?sig=1", b: "https://x/b?sig=1" }));
|
|
66
|
+
await waitFor(() => expect(seen.at(-1)).toEqual({ a: "https://x/a?sig=1", b: "https://x/b?sig=1" }));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -7,11 +7,11 @@ describe("chatThreadStackOptions", () => {
|
|
|
7
7
|
it("keeps the bar opaque, so the inverted list never sits under iOS's see-through bar", () => {
|
|
8
8
|
const opts = chatThreadStackOptions({ headerTitle: "Lin" }, "#f2f2f7");
|
|
9
9
|
expect(opts).toMatchObject({ title: "Lin", headerTransparent: false, headerStyle: { backgroundColor: "#f2f2f7" } });
|
|
10
|
-
expect(opts.headerLargeTitle).
|
|
10
|
+
expect(opts.headerLargeTitle).toBe(false);
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
it("never turns on a large title, which would collapse into the flipped list", () => {
|
|
14
|
-
expect(chatThreadStackOptions({ headerTitle: "Lin", large: true }, "#fff").headerLargeTitle).
|
|
14
|
+
expect(chatThreadStackOptions({ headerTitle: "Lin", large: true }, "#fff").headerLargeTitle).toBe(false);
|
|
15
15
|
});
|
|
16
16
|
|
|
17
17
|
it("lets the app's own header options win", () => {
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
// @feat:chat
|
|
3
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
4
|
+
import { act, cleanup, fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react";
|
|
5
|
+
import { forwardRef, useImperativeHandle, type ReactNode } from "react";
|
|
6
|
+
import { Text } from "react-native";
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
8
|
+
import { GenesisModulesProvider, NativeToastProvider } from "@marktiderman/genesis-ui-native/components";
|
|
9
|
+
import type { ChatClient, ConversationHandlers } from "../../core/client";
|
|
10
|
+
import type { ChatMessage } from "../../core/types";
|
|
11
|
+
import { ChatProvider, useConversation } from "../../react/index";
|
|
12
|
+
import { ChatNativeProvider } from "../context";
|
|
13
|
+
import type { ChatNativeModules, NetInfoLike } from "../modules";
|
|
14
|
+
import { ChatThreadScreen, connectionState, countUnseenBelow } from "../thread-screen";
|
|
15
|
+
|
|
16
|
+
vi.mock("react-native", async (original) => ({ ...(await original<object>()), useWindowDimensions: () => ({ width: 390, height: 844, scale: 3, fontScale: 1 }) }));
|
|
17
|
+
|
|
18
|
+
afterEach(cleanup);
|
|
19
|
+
|
|
20
|
+
const ME = "11111111-1111-4111-8111-111111111111";
|
|
21
|
+
const SAM = "22222222-2222-4222-8222-222222222222";
|
|
22
|
+
const CONV = "33333333-3333-4333-8333-333333333333";
|
|
23
|
+
|
|
24
|
+
function msg(seq: number, senderId: string): ChatMessage {
|
|
25
|
+
return {
|
|
26
|
+
id: `m${seq}`, conversationId: CONV, seq, clientId: `c${seq}`, senderId, senderRole: "member", kind: "text", body: `body ${seq}`,
|
|
27
|
+
mentions: [], replyToId: null, threadRootId: null, threadCount: 0, threadLastAt: null, topic: null, visibility: "everyone",
|
|
28
|
+
silent: false, meta: {}, createdAt: `2026-10-24T10:0${seq}:00Z`, editedAt: null, deletedAt: null, attachments: [], reactions: [],
|
|
29
|
+
} as ChatMessage;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function fakeClient(over: Partial<Record<keyof ChatClient, unknown>> = {}, onHandlers?: (h: ConversationHandlers) => void) {
|
|
33
|
+
const sub = { unsubscribe: vi.fn(), signal: vi.fn() };
|
|
34
|
+
return {
|
|
35
|
+
conversation: vi.fn(async () => ({ id: CONV, kind: "direct", title: null, settings: {}, status: "open", archivedAt: null })),
|
|
36
|
+
members: vi.fn(async () => [
|
|
37
|
+
{ conversationId: CONV, userId: ME, role: "member", lastReadSeq: 9, joinedAt: "2026-10-01T00:00:00Z" },
|
|
38
|
+
{ conversationId: CONV, userId: SAM, role: "member", lastReadSeq: 9, joinedAt: "2026-10-01T00:00:00Z" },
|
|
39
|
+
]),
|
|
40
|
+
kind: vi.fn(async () => ({ kind: "direct", label: "Direct", maxMembers: 2 })),
|
|
41
|
+
myRole: vi.fn(async () => "member"),
|
|
42
|
+
canPost: vi.fn(async () => true),
|
|
43
|
+
messages: vi.fn(async () => ({ messages: [msg(1, SAM)], hasEarlier: false })),
|
|
44
|
+
message: vi.fn(async () => null),
|
|
45
|
+
subscribeConversation: vi.fn((_id: string, _me: string, h: ConversationHandlers) => {
|
|
46
|
+
onHandlers?.(h);
|
|
47
|
+
return sub;
|
|
48
|
+
}),
|
|
49
|
+
subscribeInbox: vi.fn(() => ({ unsubscribe: vi.fn() })),
|
|
50
|
+
markRead: vi.fn(async () => 0),
|
|
51
|
+
pins: vi.fn(async () => []),
|
|
52
|
+
fileUrls: vi.fn(async () => ({})),
|
|
53
|
+
...over,
|
|
54
|
+
} as unknown as ChatClient;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function scrollingList() {
|
|
58
|
+
const seen: Record<string, unknown>[] = [];
|
|
59
|
+
const List = forwardRef<unknown, Record<string, unknown>>(function List(props, ref) {
|
|
60
|
+
seen.push(props);
|
|
61
|
+
useImperativeHandle(ref, () => ({ scrollToIndex: vi.fn(), scrollToEnd: vi.fn(), scrollToOffset: vi.fn() }));
|
|
62
|
+
return null;
|
|
63
|
+
});
|
|
64
|
+
return { List, last: () => seen.at(-1)! };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function wrap(client: ChatClient, children: ReactNode, opts: { list?: unknown; modules?: ChatNativeModules } = {}) {
|
|
68
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
69
|
+
return (
|
|
70
|
+
<QueryClientProvider client={qc}>
|
|
71
|
+
<GenesisModulesProvider modules={opts.list ? { list: opts.list as never } : {}}>
|
|
72
|
+
<ChatProvider client={client} userId={ME}>
|
|
73
|
+
<NativeToastProvider>
|
|
74
|
+
<ChatNativeProvider modules={opts.modules}>{children}</ChatNativeProvider>
|
|
75
|
+
</NativeToastProvider>
|
|
76
|
+
</ChatProvider>
|
|
77
|
+
</GenesisModulesProvider>
|
|
78
|
+
</QueryClientProvider>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
describe("the thread when nothing could be read", () => {
|
|
83
|
+
it("shows an error with Retry, not 'No messages yet', and Retry reads again", async () => {
|
|
84
|
+
let fail = true;
|
|
85
|
+
const messages = vi.fn(async () => {
|
|
86
|
+
if (fail) throw new Error("offline");
|
|
87
|
+
return { messages: [msg(1, SAM)], hasEarlier: false };
|
|
88
|
+
});
|
|
89
|
+
const list = scrollingList();
|
|
90
|
+
render(wrap(fakeClient({ messages }), <ChatThreadScreen conversationId={CONV} testID="t" />, { list: list.List }));
|
|
91
|
+
await waitFor(() => expect(screen.getByTestId("t-load-error")).toBeTruthy());
|
|
92
|
+
expect(screen.queryByTestId("t-empty")).toBeNull();
|
|
93
|
+
fail = false;
|
|
94
|
+
await act(async () => fireEvent.click(screen.getByTestId("t-load-error-retry")));
|
|
95
|
+
await waitFor(() => expect((list.last().data as unknown[] | undefined)?.length).toBeGreaterThan(0));
|
|
96
|
+
expect(screen.queryByTestId("t-load-error")).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("the thread's connection line", () => {
|
|
101
|
+
it("reads offline first, then a dropped channel, else nothing", () => {
|
|
102
|
+
expect(connectionState(false, true)).toBe("offline");
|
|
103
|
+
expect(connectionState(true, false)).toBe("connecting");
|
|
104
|
+
expect(connectionState(null, null)).toBe("online");
|
|
105
|
+
expect(connectionState(true, true)).toBe("online");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("says Waiting for network while NetInfo says offline, and Connecting when the live channel drops", async () => {
|
|
109
|
+
let netListener: ((s: { isConnected: boolean | null }) => void) | null = null;
|
|
110
|
+
const netInfo: NetInfoLike = { addEventListener: (l) => ((netListener = l), () => undefined) };
|
|
111
|
+
let handlers: ConversationHandlers | null = null;
|
|
112
|
+
render(wrap(fakeClient({}, (h) => (handlers = h)), <ChatThreadScreen conversationId={CONV} testID="t" />, { modules: { netInfo } as ChatNativeModules }));
|
|
113
|
+
await waitFor(() => expect(handlers).not.toBeNull());
|
|
114
|
+
expect(screen.queryByTestId("t-connection")).toBeNull();
|
|
115
|
+
act(() => netListener!({ isConnected: false }));
|
|
116
|
+
expect(screen.getByTestId("t-connection").textContent).toBe("Waiting for network…");
|
|
117
|
+
act(() => netListener!({ isConnected: true }));
|
|
118
|
+
act(() => handlers!.onLive?.(false));
|
|
119
|
+
expect(screen.getByTestId("t-connection").textContent).toBe("Connecting…");
|
|
120
|
+
act(() => handlers!.onLive?.(true));
|
|
121
|
+
expect(screen.queryByTestId("t-connection")).toBeNull();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("the new messages pill", () => {
|
|
126
|
+
it("counts others' messages that came after you scrolled up", () => {
|
|
127
|
+
const ms = [msg(1, SAM), msg(2, ME), msg(3, SAM), msg(4, SAM)];
|
|
128
|
+
expect(countUnseenBelow(ms, null, ME)).toBe(0);
|
|
129
|
+
expect(countUnseenBelow(ms, 1, ME)).toBe(2);
|
|
130
|
+
expect(countUnseenBelow(ms, 4, ME)).toBe(0);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("shows while you're scrolled up and new messages came in, and goes to the newest", async () => {
|
|
134
|
+
let handlers: ConversationHandlers | null = null;
|
|
135
|
+
const message = vi.fn(async (id: string) => (id === "m2" ? msg(2, SAM) : null));
|
|
136
|
+
const list = scrollingList();
|
|
137
|
+
render(wrap(fakeClient({ message }, (h) => (handlers = h)), <ChatThreadScreen conversationId={CONV} testID="t" />, { list: list.List }));
|
|
138
|
+
await waitFor(() => expect((list.last().data as unknown[] | undefined)?.length).toBeGreaterThan(0));
|
|
139
|
+
act(() => (list.last().onScroll as (e: unknown) => void)({ nativeEvent: { contentOffset: { y: 0 }, contentSize: { height: 3000 }, layoutMeasurement: { height: 600 } } }));
|
|
140
|
+
expect(screen.queryByTestId("t-new-below")).toBeNull();
|
|
141
|
+
await act(async () => handlers!.onSignal({ type: "message", conversationId: CONV, id: "m2", seq: 2, op: "insert" } as never));
|
|
142
|
+
await waitFor(() => expect(screen.getByTestId("t-new-below").getAttribute("aria-label")).toBe("1 new message"));
|
|
143
|
+
fireEvent.click(screen.getByTestId("t-new-below"));
|
|
144
|
+
await waitFor(() => expect(screen.queryByTestId("t-new-below")).toBeNull());
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe("what an app can add without forking the screen", () => {
|
|
149
|
+
it("draws its header strip above the messages and its tools over the composer, with the composer", async () => {
|
|
150
|
+
const accessory = vi.fn(({ canPost }: { canPost: boolean | null }) => <Text testID="tools">{String(canPost)}</Text>);
|
|
151
|
+
render(wrap(fakeClient(), <ChatThreadScreen conversationId={CONV} header={<Text testID="strip">Client</Text>} accessory={accessory} testID="t" />));
|
|
152
|
+
expect(screen.getByTestId("strip")).toBeTruthy();
|
|
153
|
+
await waitFor(() => expect(screen.getByTestId("tools").textContent).toBe("true"));
|
|
154
|
+
expect(accessory.mock.calls.at(-1)![0]).toMatchObject({ conversationId: CONV, composer: expect.objectContaining({ dispatch: expect.any(Function) }) });
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("doesn't mark the thread read while it isn't active, and does once it is", async () => {
|
|
158
|
+
const client = fakeClient();
|
|
159
|
+
const { rerender } = render(wrap(client, <ChatThreadScreen conversationId={CONV} active={false} testID="t" />));
|
|
160
|
+
await waitFor(() => expect(client.messages).toHaveBeenCalled());
|
|
161
|
+
await act(async () => undefined);
|
|
162
|
+
expect(client.markRead).not.toHaveBeenCalled();
|
|
163
|
+
rerender(wrap(client, <ChatThreadScreen conversationId={CONV} active testID="t" />));
|
|
164
|
+
await waitFor(() => expect(client.markRead).toHaveBeenCalled());
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("a long press on a bubble", () => {
|
|
169
|
+
it("taps a medium impact as the menu opens", async () => {
|
|
170
|
+
const haptics = { impactAsync: vi.fn(async () => undefined), selectionAsync: vi.fn(async () => undefined), notificationAsync: vi.fn(async () => undefined), ImpactFeedbackStyle: { Light: "l", Medium: "m" }, NotificationFeedbackType: { Success: 1, Warning: 2, Error: 3 } };
|
|
171
|
+
// No list module: FlatList draws the rows.
|
|
172
|
+
render(wrap(fakeClient(), <ChatThreadScreen conversationId={CONV} testID="t" />, { modules: { haptics } as unknown as ChatNativeModules }));
|
|
173
|
+
const bubble = await screen.findByTestId("t-message-m1-bubble");
|
|
174
|
+
act(() => {
|
|
175
|
+
fireEvent.contextMenu(bubble);
|
|
176
|
+
});
|
|
177
|
+
await waitFor(() => expect(haptics.impactAsync).toHaveBeenCalledWith("m"));
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("another conversation in the same screen", () => {
|
|
182
|
+
it("opens at its own first unread, not the last one's read place", async () => {
|
|
183
|
+
const OTHER = "44444444-4444-4444-8444-444444444444";
|
|
184
|
+
const client = fakeClient({
|
|
185
|
+
members: vi.fn(async (id: string) => [
|
|
186
|
+
{ conversationId: id, userId: ME, role: "member", lastReadSeq: id === CONV ? 1 : 2, joinedAt: "2026-10-01T00:00:00Z" },
|
|
187
|
+
{ conversationId: id, userId: SAM, role: "member", lastReadSeq: 9, joinedAt: "2026-10-01T00:00:00Z" },
|
|
188
|
+
]),
|
|
189
|
+
messages: vi.fn(async (id: string) => ({ messages: [msg(1, SAM), msg(2, SAM), msg(3, SAM)].map((m) => ({ ...m, id: `${id === CONV ? "a" : "b"}${m.seq}`, conversationId: id })), hasEarlier: false })),
|
|
190
|
+
});
|
|
191
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
192
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
193
|
+
<QueryClientProvider client={qc}>
|
|
194
|
+
<ChatProvider client={client} userId={ME}>{children}</ChatProvider>
|
|
195
|
+
</QueryClientProvider>
|
|
196
|
+
);
|
|
197
|
+
const { result, rerender } = renderHook(({ id }) => useConversation(id, { markRead: false }), { wrapper, initialProps: { id: CONV } });
|
|
198
|
+
await waitFor(() => expect(result.current.firstUnreadId).toBe("a2"));
|
|
199
|
+
rerender({ id: OTHER });
|
|
200
|
+
await waitFor(() => expect(result.current.firstUnreadId).toBe("b3"));
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe("after #888's review", () => {
|
|
205
|
+
function sharedList() {
|
|
206
|
+
const seen: Record<string, unknown>[] = [];
|
|
207
|
+
const scrollToIndex = vi.fn();
|
|
208
|
+
const List = forwardRef<unknown, Record<string, unknown>>(function List(props, ref) {
|
|
209
|
+
seen.push(props);
|
|
210
|
+
useImperativeHandle(ref, () => ({ scrollToIndex, scrollToEnd: vi.fn(), scrollToOffset: vi.fn() }));
|
|
211
|
+
return null;
|
|
212
|
+
});
|
|
213
|
+
return { List, scrollToIndex, last: () => seen.at(-1)! };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
it("opens at the New messages line itself, not the message after it", async () => {
|
|
217
|
+
const members = vi.fn(async () => [
|
|
218
|
+
{ conversationId: CONV, userId: ME, role: "member", lastReadSeq: 1, joinedAt: "2026-10-01T00:00:00Z" },
|
|
219
|
+
{ conversationId: CONV, userId: SAM, role: "member", lastReadSeq: 9, joinedAt: "2026-10-01T00:00:00Z" },
|
|
220
|
+
]);
|
|
221
|
+
const messages = vi.fn(async () => ({ messages: [msg(1, SAM), msg(2, SAM), msg(3, SAM)], hasEarlier: false }));
|
|
222
|
+
const list = sharedList();
|
|
223
|
+
render(wrap(fakeClient({ members, messages }), <ChatThreadScreen conversationId={CONV} testID="t" />, { list: list.List }));
|
|
224
|
+
await waitFor(() => expect(list.scrollToIndex).toHaveBeenCalled(), { timeout: 2000 });
|
|
225
|
+
const data = list.last().data as { type: string }[];
|
|
226
|
+
const at = list.scrollToIndex.mock.calls[0]![0] as { index: number; viewPosition: number };
|
|
227
|
+
expect(data[at.index]!.type).toBe("unread");
|
|
228
|
+
expect(at.viewPosition).toBe(0);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("forgets where you scrolled away when the screen shows another conversation", async () => {
|
|
232
|
+
const OTHER = "44444444-4444-4444-8444-444444444444";
|
|
233
|
+
const messages = vi.fn(async (id: string) =>
|
|
234
|
+
id === CONV ? { messages: [msg(1, SAM)], hasEarlier: false } : { messages: [msg(1, SAM), msg(2, SAM), msg(3, SAM)].map((m) => ({ ...m, id: `o${m.seq}`, conversationId: OTHER })), hasEarlier: false },
|
|
235
|
+
);
|
|
236
|
+
const list = sharedList();
|
|
237
|
+
const client = fakeClient({ messages });
|
|
238
|
+
const { rerender } = render(wrap(client, <ChatThreadScreen conversationId={CONV} testID="t" />, { list: list.List }));
|
|
239
|
+
await waitFor(() => expect((list.last().data as unknown[] | undefined)?.length).toBeGreaterThan(0));
|
|
240
|
+
// Scrolled away in the first conversation, at its seq 1.
|
|
241
|
+
act(() => (list.last().onScroll as (e: unknown) => void)({ nativeEvent: { contentOffset: { y: 0 }, contentSize: { height: 3000 }, layoutMeasurement: { height: 600 } } }));
|
|
242
|
+
rerender(wrap(client, <ChatThreadScreen conversationId={OTHER} testID="t" />, { list: list.List }));
|
|
243
|
+
await waitFor(() => expect((list.last().data as { key: string }[]).some((r) => r.key === "c:c3")).toBe(true));
|
|
244
|
+
// The other conversation's own messages aren't "new" just because their seq is past 1.
|
|
245
|
+
expect(screen.queryByTestId("t-new-below")).toBeNull();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|