@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,131 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- pgTAP test for 0006_genesis_chat_read_receipts.sql.
|
|
3
|
+
-- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
|
|
4
|
+
-- It writes only its own users, kinds and conversations, and rolls everything back.
|
|
5
|
+
--
|
|
6
|
+
-- People: a1 and b2 talk directly.
|
|
7
|
+
begin;
|
|
8
|
+
create extension if not exists pgtap with schema extensions;
|
|
9
|
+
select plan(17);
|
|
10
|
+
|
|
11
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
12
|
+
select ('00000000-0000-4000-8000-00000000c6' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat-6-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
13
|
+
from unnest(array['a1', 'b2']) n;
|
|
14
|
+
|
|
15
|
+
create schema if not exists private;
|
|
16
|
+
create or replace function private.genesis_viewer_people(viewer uuid)
|
|
17
|
+
returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
|
|
18
|
+
select '00000000-0000-4000-8000-0000000000f6'::uuid, viewer
|
|
19
|
+
$$;
|
|
20
|
+
|
|
21
|
+
create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c6' || n)::uuid $$;
|
|
22
|
+
create function pg_temp.sign_in(n text) returns void language plpgsql as $$
|
|
23
|
+
begin
|
|
24
|
+
perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
|
|
25
|
+
perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
|
|
26
|
+
perform set_config('role', 'authenticated', true);
|
|
27
|
+
end $$;
|
|
28
|
+
grant execute on function pg_temp.person(text), pg_temp.sign_in(text) to authenticated;
|
|
29
|
+
|
|
30
|
+
create temp table t (k text primary key, v uuid);
|
|
31
|
+
grant select, insert, update on t to authenticated;
|
|
32
|
+
create function pg_temp.v(k_ text) returns uuid language sql stable as $$ select v from t where k = k_ $$;
|
|
33
|
+
create function pg_temp.read_at(n text) returns timestamptz language sql stable as $$
|
|
34
|
+
select last_read_at from genesis_chat.members where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person(n)
|
|
35
|
+
$$;
|
|
36
|
+
grant execute on function pg_temp.v(text), pg_temp.read_at(text) to authenticated;
|
|
37
|
+
|
|
38
|
+
create function public.genesis_chat_test6_may_start(member_ids uuid[], subject_type text, subject_id text, user_id uuid)
|
|
39
|
+
returns boolean language sql stable set search_path = '' as $$ select true $$;
|
|
40
|
+
select genesis_chat.register_kind('direct', 'Direct message', p_max_members => 2,
|
|
41
|
+
p_start_check => 'public.genesis_chat_test6_may_start(uuid[], text, text, uuid)');
|
|
42
|
+
|
|
43
|
+
select pg_temp.sign_in('a1');
|
|
44
|
+
insert into t select 'ab', genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]);
|
|
45
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'hello');
|
|
46
|
+
|
|
47
|
+
select is(pg_temp.read_at('a1'), null, 'posting moves your read place past your own message only: no read time');
|
|
48
|
+
select is(pg_temp.read_at('b2'), null, 'nobody has read it yet');
|
|
49
|
+
|
|
50
|
+
select pg_temp.sign_in('b2');
|
|
51
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
52
|
+
select isnt(pg_temp.read_at('b2'), null, 'reading someone else''s message records when');
|
|
53
|
+
|
|
54
|
+
-- A fixed earlier time, so "unchanged" is visible within one transaction (now() doesn't move).
|
|
55
|
+
reset role;
|
|
56
|
+
update genesis_chat.members set last_read_at = '2000-01-01T00:00:00Z' where conversation_id = pg_temp.v('ab');
|
|
57
|
+
|
|
58
|
+
select pg_temp.sign_in('b2');
|
|
59
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
60
|
+
select is(pg_temp.read_at('b2'), '2000-01-01T00:00:00Z'::timestamptz, 'reading again with nothing new keeps the time');
|
|
61
|
+
|
|
62
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'hi back');
|
|
63
|
+
select is(pg_temp.read_at('b2'), '2000-01-01T00:00:00Z'::timestamptz, 'replying moves the place past your own message: the time stays');
|
|
64
|
+
|
|
65
|
+
select pg_temp.sign_in('a1');
|
|
66
|
+
select is(pg_temp.read_at('b2'), '2000-01-01T00:00:00Z'::timestamptz, 'the other member sees the read time');
|
|
67
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'and again');
|
|
68
|
+
select isnt(pg_temp.read_at('a1'), '2000-01-01T00:00:00Z'::timestamptz, 'posting past a message you hadn''t read counts as reading it');
|
|
69
|
+
|
|
70
|
+
-- An observer's place moving records nothing.
|
|
71
|
+
reset role;
|
|
72
|
+
update genesis_chat.members set role = 'observer', last_read_at = null where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2');
|
|
73
|
+
update genesis_chat.members set last_read_seq = last_read_seq + 1 where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2');
|
|
74
|
+
select is(pg_temp.read_at('b2'), null, 'an observer gets no read time');
|
|
75
|
+
|
|
76
|
+
select has_column('genesis_chat', 'members', 'last_read_at', 'members carry last_read_at');
|
|
77
|
+
|
|
78
|
+
-- A voice note (a message with attachments and no text) is read like any other.
|
|
79
|
+
reset role;
|
|
80
|
+
update genesis_chat.members set role = 'member', last_read_at = '2000-01-01T00:00:00Z' where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2');
|
|
81
|
+
insert into genesis_chat.messages (conversation_id, seq, sender_id, sender_role, body) values (pg_temp.v('ab'), 0, pg_temp.person('a1'), 'member', '');
|
|
82
|
+
select pg_temp.sign_in('b2');
|
|
83
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
84
|
+
select isnt(pg_temp.read_at('b2'), '2000-01-01T00:00:00Z'::timestamptz, 'reading past a message with no text (a voice note) records when');
|
|
85
|
+
|
|
86
|
+
-- Reading twice in one transaction (now() fixed, so last_read_at unchanged) still sends the hint:
|
|
87
|
+
-- the hint follows the read place, not the time.
|
|
88
|
+
reset role;
|
|
89
|
+
select ok(genesis_chat.read_passed(pg_temp.v('ab'), pg_temp.person('b2'), 0, 1), 'a read place moving past someone else''s message counts, whatever the time');
|
|
90
|
+
|
|
91
|
+
-- A hidden (moderated) or deleted message isn't read: no read time for it.
|
|
92
|
+
update genesis_chat.members set role = 'member', last_read_at = null where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2');
|
|
93
|
+
select pg_temp.sign_in('a1');
|
|
94
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'hidden one');
|
|
95
|
+
reset role;
|
|
96
|
+
update genesis_chat.messages set moderation = 'hidden'
|
|
97
|
+
where conversation_id = pg_temp.v('ab') and seq = (select last_seq from genesis_chat.conversations where id = pg_temp.v('ab'));
|
|
98
|
+
select pg_temp.sign_in('b2');
|
|
99
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
100
|
+
select is(pg_temp.read_at('b2'), null, 'reading past a hidden message records no read time');
|
|
101
|
+
|
|
102
|
+
select pg_temp.sign_in('a1');
|
|
103
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'deleted one');
|
|
104
|
+
reset role;
|
|
105
|
+
update genesis_chat.messages set deleted_at = now()
|
|
106
|
+
where conversation_id = pg_temp.v('ab') and seq = (select last_seq from genesis_chat.conversations where id = pg_temp.v('ab'));
|
|
107
|
+
select pg_temp.sign_in('b2');
|
|
108
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
109
|
+
select is(pg_temp.read_at('b2'), null, 'reading past a deleted message records no read time');
|
|
110
|
+
|
|
111
|
+
-- Clients send only client events on a conversation's topic: never a read (or message) hint.
|
|
112
|
+
reset role;
|
|
113
|
+
grant insert on realtime.messages to authenticated;
|
|
114
|
+
grant usage on all sequences in schema realtime to authenticated;
|
|
115
|
+
select pg_temp.sign_in('a1');
|
|
116
|
+
select set_config('realtime.topic', 'genesis-chat:conv:' || pg_temp.v('ab'), true);
|
|
117
|
+
select throws_ok(
|
|
118
|
+
$$ insert into realtime.messages (topic, extension, event, payload) values (realtime.topic(), 'broadcast', 'read', jsonb_build_object('user_id', pg_temp.person('b2'), 'seq', 1000000)) $$,
|
|
119
|
+
'42501', null, 'a member can''t broadcast a read hint (a forged receipt)');
|
|
120
|
+
select throws_ok(
|
|
121
|
+
$$ insert into realtime.messages (topic, extension, event, payload) values (realtime.topic(), 'broadcast', 'message', '{}'::jsonb) $$,
|
|
122
|
+
'42501', null, 'nor a message hint');
|
|
123
|
+
select lives_ok(
|
|
124
|
+
$$ insert into realtime.messages (topic, extension, event, payload) values (realtime.topic(), 'broadcast', 'activity', '{"activity":"typing"}'::jsonb) $$,
|
|
125
|
+
'a member can still say they are typing');
|
|
126
|
+
select lives_ok(
|
|
127
|
+
$$ insert into realtime.messages (topic, extension, event, payload) values (realtime.topic(), 'presence', 'track', '{}'::jsonb) $$,
|
|
128
|
+
'and use presence');
|
|
129
|
+
|
|
130
|
+
select * from finish();
|
|
131
|
+
rollback;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- pgTAP test for 0007_genesis_chat_unread_and_delete_for_me.sql.
|
|
3
|
+
-- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
|
|
4
|
+
-- It writes only its own users, kinds and conversations, and rolls everything back.
|
|
5
|
+
--
|
|
6
|
+
-- People: a1 and b2 talk directly; c3 is someone else.
|
|
7
|
+
begin;
|
|
8
|
+
create extension if not exists pgtap with schema extensions;
|
|
9
|
+
select plan(49);
|
|
10
|
+
|
|
11
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
12
|
+
select ('00000000-0000-4000-8000-00000000c7' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat-7-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
13
|
+
from unnest(array['a1', 'b2', 'c3']) n;
|
|
14
|
+
|
|
15
|
+
create schema if not exists private;
|
|
16
|
+
create or replace function private.genesis_viewer_people(viewer uuid)
|
|
17
|
+
returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
|
|
18
|
+
select '00000000-0000-4000-8000-0000000000f7'::uuid, viewer
|
|
19
|
+
$$;
|
|
20
|
+
|
|
21
|
+
create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c7' || n)::uuid $$;
|
|
22
|
+
create function pg_temp.sign_in(n text) returns void language plpgsql as $$
|
|
23
|
+
begin
|
|
24
|
+
perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
|
|
25
|
+
perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
|
|
26
|
+
perform set_config('role', 'authenticated', true);
|
|
27
|
+
end $$;
|
|
28
|
+
grant execute on function pg_temp.person(text), pg_temp.sign_in(text) to authenticated;
|
|
29
|
+
|
|
30
|
+
create temp table t (k text primary key, v uuid);
|
|
31
|
+
grant select, insert, update on t to authenticated;
|
|
32
|
+
create function pg_temp.v(k_ text) returns uuid language sql stable as $$ select v from t where k = k_ $$;
|
|
33
|
+
create function pg_temp.unread() returns integer language sql stable as $$
|
|
34
|
+
select unread from genesis_chat.inbox() where conversation_id = pg_temp.v('ab')
|
|
35
|
+
$$;
|
|
36
|
+
create function pg_temp.listed() returns integer language sql stable as $$
|
|
37
|
+
select count(*)::integer from genesis_chat.inbox() where conversation_id = pg_temp.v('ab')
|
|
38
|
+
$$;
|
|
39
|
+
create function pg_temp.visible() returns integer language sql stable as $$
|
|
40
|
+
select count(*)::integer from genesis_chat.messages where conversation_id = pg_temp.v('ab') and thread_root_id is null
|
|
41
|
+
$$;
|
|
42
|
+
grant execute on function pg_temp.v(text), pg_temp.unread(), pg_temp.listed(), pg_temp.visible() to authenticated;
|
|
43
|
+
|
|
44
|
+
-- Capture the hints genesis_chat.broadcast sends (the stub realtime.send drops them).
|
|
45
|
+
create table realtime.test7_sent (topic text, event text, payload jsonb);
|
|
46
|
+
create or replace function realtime.send(payload jsonb, event text, topic text, private boolean default true)
|
|
47
|
+
returns void language sql as $$ insert into realtime.test7_sent values (topic, event, payload) $$;
|
|
48
|
+
create function pg_temp.hinted(n text) returns integer language sql stable security definer as $$
|
|
49
|
+
select count(*)::integer from realtime.test7_sent
|
|
50
|
+
where topic = 'genesis-chat:user:' || pg_temp.person(n) and event = 'inbox' and payload->>'conversation_id' = pg_temp.v('ab')::text
|
|
51
|
+
$$;
|
|
52
|
+
grant execute on function pg_temp.hinted(text) to authenticated;
|
|
53
|
+
|
|
54
|
+
create function public.genesis_chat_test7_may_start(member_ids uuid[], subject_type text, subject_id text, user_id uuid)
|
|
55
|
+
returns boolean language sql stable set search_path = '' as $$ select true $$;
|
|
56
|
+
select genesis_chat.register_kind('direct', 'Direct message', p_max_members => 2,
|
|
57
|
+
p_start_check => 'public.genesis_chat_test7_may_start(uuid[], text, text, uuid)');
|
|
58
|
+
|
|
59
|
+
select pg_temp.sign_in('a1');
|
|
60
|
+
insert into t select 'ab', genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]);
|
|
61
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'hello');
|
|
62
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'how was the week?');
|
|
63
|
+
|
|
64
|
+
-- ── Mark as Unread ──────────────────────────────────────────────────────────────
|
|
65
|
+
select is(pg_temp.unread(), 0, 'a1''s own messages are not unread for a1');
|
|
66
|
+
select lives_ok($$ select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => true) $$, 'a1 marks it unread');
|
|
67
|
+
select is(pg_temp.unread(), 1, 'it counts as one unread, though the last message is a1''s own');
|
|
68
|
+
select is((select count(*)::int from genesis_chat.member_state where user_id = pg_temp.person('a1') and marked_unread), 1, 'a1 reads their own mark');
|
|
69
|
+
select is(genesis_chat.unread_total(), 1, 'and in the badge');
|
|
70
|
+
select pg_temp.sign_in('b2');
|
|
71
|
+
select is(pg_temp.unread(), 2, 'b2''s unread is untouched by a1''s mark (b2 hasn''t read a1''s two)');
|
|
72
|
+
select is((select count(*)::int from genesis_chat.member_state where user_id = pg_temp.person('a1')), 0, 'b2 can''t see that a1 marked it unread');
|
|
73
|
+
select hasnt_column('genesis_chat', 'members', 'marked_unread', 'members, which everyone in it reads, doesn''t hold the mark');
|
|
74
|
+
select hasnt_column('genesis_chat', 'members', 'cleared_seq', 'or what a member deleted');
|
|
75
|
+
select ok(pg_temp.hinted('a1') > 0, 'marking it unread sends a1''s own devices an inbox hint');
|
|
76
|
+
select pg_temp.sign_in('a1');
|
|
77
|
+
select is((select last_read_seq from genesis_chat.members where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('a1')),
|
|
78
|
+
(select last_seq from genesis_chat.conversations where id = pg_temp.v('ab')), 'the read place doesn''t move back');
|
|
79
|
+
|
|
80
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
81
|
+
select is(pg_temp.unread(), 0, 'reading it clears the mark');
|
|
82
|
+
select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => true);
|
|
83
|
+
select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => false);
|
|
84
|
+
select is(pg_temp.unread(), 0, 'and so does unmarking it');
|
|
85
|
+
|
|
86
|
+
-- ── Delete for me ───────────────────────────────────────────────────────────────
|
|
87
|
+
-- A photo and a link in the history too: their file and preview go with it.
|
|
88
|
+
reset role;
|
|
89
|
+
insert into storage.objects (bucket_id, name, owner_id)
|
|
90
|
+
values ('genesis-chat', pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text);
|
|
91
|
+
select genesis_chat.save_link_preview('https://example.com/page', 'ok', 'Example page');
|
|
92
|
+
select pg_temp.sign_in('a1');
|
|
93
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), '',
|
|
94
|
+
jsonb_build_array(jsonb_build_object('kind', 'image', 'path', pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg')));
|
|
95
|
+
insert into t select 'linked', (genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'look at https://example.com/page')).id;
|
|
96
|
+
|
|
97
|
+
select genesis_chat.pin_message(pg_temp.v('linked'));
|
|
98
|
+
reset role;
|
|
99
|
+
update genesis_chat.members set archived = true where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2');
|
|
100
|
+
select pg_temp.sign_in('b2');
|
|
101
|
+
select is(pg_temp.visible(), 4, 'b2 reads all four messages');
|
|
102
|
+
select is((select count(*)::int from genesis_chat.pins where conversation_id = pg_temp.v('ab')), 1, 'and the pin');
|
|
103
|
+
select ok(genesis_chat.can_read_object(pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text, now() - interval '2 days'), 'and the photo');
|
|
104
|
+
select is((select count(*)::int from genesis_chat.link_previews_for(pg_temp.v('linked'))), 1, 'and the link''s preview');
|
|
105
|
+
select lives_ok($$ select genesis_chat.delete_conversation_for_me(pg_temp.v('ab')) $$, 'b2 deletes the conversation for themselves');
|
|
106
|
+
select is(pg_temp.listed(), 0, 'it leaves b2''s inbox');
|
|
107
|
+
select ok(pg_temp.hinted('b2') > 0, 'and sends b2''s own devices an inbox hint');
|
|
108
|
+
select is((select count(*)::int from genesis_chat.pins where conversation_id = pg_temp.v('ab')), 0, 'its pins leave b2''s pinned bar');
|
|
109
|
+
select is((select archived from genesis_chat.members where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2')), false, 'and it''s no longer archived, so a new message brings it to the inbox');
|
|
110
|
+
select is(pg_temp.visible(), 0, 'and its history leaves b2''s thread');
|
|
111
|
+
select ok(not genesis_chat.can_read_object(pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text, now() - interval '2 days'), 'its files can''t be fetched by b2');
|
|
112
|
+
select is((select count(*)::int from genesis_chat.link_previews_for(pg_temp.v('linked'))), 0, 'nor its link previews');
|
|
113
|
+
select is((select count(*)::int from genesis_chat.search_messages('week')), 0, 'search doesn''t find what b2 deleted');
|
|
114
|
+
select is(genesis_chat.unread_total(), 0, 'nor does the badge count it');
|
|
115
|
+
|
|
116
|
+
select pg_temp.sign_in('a1');
|
|
117
|
+
select is(pg_temp.visible(), 4, 'a1 still reads everything');
|
|
118
|
+
select ok(genesis_chat.can_read_object(pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text, now() - interval '2 days'), 'and still fetches the photo');
|
|
119
|
+
select is(pg_temp.listed(), 1, 'and still has it in the inbox');
|
|
120
|
+
select is((select count(*)::int from genesis_chat.pins where conversation_id = pg_temp.v('ab')), 1, 'and the pin');
|
|
121
|
+
|
|
122
|
+
-- What b2 can't read on the main timeline doesn't bring it back: a thread reply, an observers-only
|
|
123
|
+
-- note, or a message moderation hid.
|
|
124
|
+
insert into t select 'reply', (genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'a reply in the old thread', p_thread_root_id => pg_temp.v('linked'))).id;
|
|
125
|
+
insert into t select 'hidden', (genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'something rude')).id;
|
|
126
|
+
reset role;
|
|
127
|
+
update genesis_chat.messages set moderation = 'hidden' where id = pg_temp.v('hidden');
|
|
128
|
+
insert into genesis_chat.messages (conversation_id, seq, sender_id, sender_role, body, visibility)
|
|
129
|
+
select pg_temp.v('ab'), c.last_seq + 1, pg_temp.person('a1'), 'member', 'a note only observers read', 'observers'
|
|
130
|
+
from genesis_chat.conversations c where c.id = pg_temp.v('ab');
|
|
131
|
+
update genesis_chat.conversations set last_seq = last_seq + 1 where id = pg_temp.v('ab');
|
|
132
|
+
select pg_temp.sign_in('b2');
|
|
133
|
+
select is(pg_temp.listed(), 0, 'a thread reply, an observers-only note or a hidden message doesn''t bring it back to b2');
|
|
134
|
+
select is(genesis_chat.unread_total(), 0, 'nor into b2''s badge');
|
|
135
|
+
reset role;
|
|
136
|
+
select is((select count(*)::int from genesis_chat.push_targets(pg_temp.v('reply')) where user_id = pg_temp.person('b2')), 0,
|
|
137
|
+
'nor is b2 notified of the reply in the thread they deleted');
|
|
138
|
+
select pg_temp.sign_in('a1');
|
|
139
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'are you there?');
|
|
140
|
+
|
|
141
|
+
select pg_temp.sign_in('b2');
|
|
142
|
+
select is(pg_temp.listed(), 1, 'a new message brings it back to b2''s inbox');
|
|
143
|
+
select is(pg_temp.visible(), 1, 'with only what came after');
|
|
144
|
+
select is(pg_temp.unread(), 1, 'and only that unread');
|
|
145
|
+
reset role;
|
|
146
|
+
select is((select count(*)::int from genesis_chat.push_targets((select id from genesis_chat.messages where conversation_id = pg_temp.v('ab') and body = 'are you there?'))
|
|
147
|
+
where user_id = pg_temp.person('b2')), 1, 'and the message that brings it back notifies b2');
|
|
148
|
+
|
|
149
|
+
select pg_temp.sign_in('c3');
|
|
150
|
+
select throws_ok($$ select genesis_chat.delete_conversation_for_me(pg_temp.v('ab')) $$, '42501', null, 'someone not in it can''t delete it for anyone');
|
|
151
|
+
select throws_ok($$ select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => true) $$, '42501', null, 'or mark it unread');
|
|
152
|
+
|
|
153
|
+
reset role;
|
|
154
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ab')), 8, 'Delete for me deleted nothing: every message is still stored');
|
|
155
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ab') and deleted_at is not null), 0, 'and none is marked deleted');
|
|
156
|
+
select is((select cleared_seq from genesis_chat.member_state where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('a1')), null, 'a1''s own view was never cleared');
|
|
157
|
+
|
|
158
|
+
-- A conversation with no messages yet still shows in the inbox (nothing deleted: cleared_seq is null).
|
|
159
|
+
select pg_temp.sign_in('a1');
|
|
160
|
+
insert into t select 'ac', genesis_chat.start_conversation('direct', array[pg_temp.person('c3')]);
|
|
161
|
+
select is((select count(*)::int from genesis_chat.inbox() where conversation_id = pg_temp.v('ac')), 1, 'a new, empty conversation is listed');
|
|
162
|
+
|
|
163
|
+
-- Delete up to what you saw: a message that came in after it isn't deleted unseen.
|
|
164
|
+
insert into t select 'seen', (genesis_chat.send_message(pg_temp.v('ac'), gen_random_uuid(), 'first')).id;
|
|
165
|
+
select genesis_chat.send_message(pg_temp.v('ac'), gen_random_uuid(), 'second, after c3 looked');
|
|
166
|
+
select pg_temp.sign_in('c3');
|
|
167
|
+
select lives_ok($$ select genesis_chat.delete_conversation_for_me(pg_temp.v('ac'), (select seq from genesis_chat.messages where id = pg_temp.v('seen'))) $$,
|
|
168
|
+
'c3 deletes up to the message they saw');
|
|
169
|
+
select is((select count(*)::int from genesis_chat.inbox() where conversation_id = pg_temp.v('ac')), 1, 'the message after it keeps the conversation in c3''s inbox');
|
|
170
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ac')), 1, 'and only it is left in c3''s thread');
|
|
171
|
+
select genesis_chat.delete_conversation_for_me(pg_temp.v('ac'), 1);
|
|
172
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ac')), 1, 'deleting again with an older place never brings history back');
|
|
173
|
+
select genesis_chat.delete_conversation_for_me(pg_temp.v('ac'), 1000000);
|
|
174
|
+
select is((select cleared_seq from genesis_chat.member_state where conversation_id = pg_temp.v('ac') and user_id = pg_temp.person('c3')),
|
|
175
|
+
(select last_seq from genesis_chat.conversations where id = pg_temp.v('ac')), 'a place past the latest message stops at it');
|
|
176
|
+
|
|
177
|
+
select * from finish();
|
|
178
|
+
rollback;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marktiderman/genesis-chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -91,9 +91,9 @@
|
|
|
91
91
|
},
|
|
92
92
|
"peerDependencies": {
|
|
93
93
|
"@marktiderman/genesis-ai": ">=0.1.0 <1.0.0",
|
|
94
|
-
"@marktiderman/genesis-core": ">=2.
|
|
95
|
-
"@marktiderman/genesis-ui": ">=
|
|
96
|
-
"@marktiderman/genesis-ui-native": ">=3.
|
|
94
|
+
"@marktiderman/genesis-core": ">=2.21.0 <3.0.0",
|
|
95
|
+
"@marktiderman/genesis-ui": ">=3.3.0 <4.0.0",
|
|
96
|
+
"@marktiderman/genesis-ui-native": ">=3.17.0 <4.0.0",
|
|
97
97
|
"@supabase/supabase-js": "^2.118.0",
|
|
98
98
|
"@tanstack/react-query": "^5.0.0",
|
|
99
99
|
"hls.js": "^1.5.0",
|
|
@@ -173,19 +173,19 @@
|
|
|
173
173
|
}
|
|
174
174
|
},
|
|
175
175
|
"devDependencies": {
|
|
176
|
-
"@ai-sdk/anthropic": "4.0.
|
|
176
|
+
"@ai-sdk/anthropic": "4.0.105",
|
|
177
177
|
"@marktiderman/genesis-ai": "^0.3.1",
|
|
178
|
-
"@marktiderman/genesis-core": "^2.
|
|
178
|
+
"@marktiderman/genesis-core": "^2.21.0",
|
|
179
179
|
"@marktiderman/genesis-notifications": "^0.4.7",
|
|
180
|
-
"@marktiderman/genesis-ui": "^3.
|
|
181
|
-
"@marktiderman/genesis-ui-native": "^3.
|
|
180
|
+
"@marktiderman/genesis-ui": "^3.3.0",
|
|
181
|
+
"@marktiderman/genesis-ui-native": "^3.17.0",
|
|
182
182
|
"@supabase/supabase-js": "^2.118.0",
|
|
183
183
|
"@tanstack/react-query": "^5.105.0",
|
|
184
184
|
"@testing-library/react": "^16.3.2",
|
|
185
185
|
"@types/node": "^22",
|
|
186
186
|
"@types/react": "^19.2.14",
|
|
187
187
|
"@types/react-dom": "^19.2.3",
|
|
188
|
-
"ai": "7.0.
|
|
188
|
+
"ai": "7.0.173",
|
|
189
189
|
"happy-dom": "^21.0.4",
|
|
190
190
|
"hls.js": "1.7.4",
|
|
191
191
|
"lucide-react": "^1.28.0",
|
|
@@ -251,6 +251,14 @@
|
|
|
251
251
|
{
|
|
252
252
|
"file": "migrations/0005_genesis_chat_attachment_paths.sql",
|
|
253
253
|
"test": "migrations/tests/0005_genesis_chat_attachment_paths.test.sql"
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
"file": "migrations/0006_genesis_chat_read_receipts.sql",
|
|
257
|
+
"test": "migrations/tests/0006_genesis_chat_read_receipts.test.sql"
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
"file": "migrations/0007_genesis_chat_unread_and_delete_for_me.sql",
|
|
261
|
+
"test": "migrations/tests/0007_genesis_chat_unread_and_delete_for_me.test.sql"
|
|
254
262
|
}
|
|
255
263
|
],
|
|
256
264
|
"install": [
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment happy-dom
|
|
3
|
+
* @feat:chat
|
|
4
|
+
* The inbox's row actions, built once (`inboxRowActions`) for the phone and the web, run from the
|
|
5
|
+
* row (`useInboxActions`), and refreshed through the named prefix keys.
|
|
6
|
+
*/
|
|
7
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
8
|
+
import { act, cleanup, configure, fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react";
|
|
9
|
+
import type { ReactNode } from "react";
|
|
10
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
11
|
+
import type { ChatClient } from "../core/client";
|
|
12
|
+
import type { ChatInboxItem } from "../core/types";
|
|
13
|
+
import { ChatProvider, chatKeys, defaultInboxActionLabels, inboxRowActions, useConversation, useInboxActions, type ChatInboxActions } from "../react";
|
|
14
|
+
import type { ChatMessage } from "../core/types";
|
|
15
|
+
import { ChatInbox, createMemoryChatClient, useConfirmDialog } from "../web";
|
|
16
|
+
|
|
17
|
+
afterEach(cleanup);
|
|
18
|
+
configure({ asyncUtilTimeout: 4000 });
|
|
19
|
+
|
|
20
|
+
const ME = "00000000-0000-4000-8000-0000000000a1";
|
|
21
|
+
const C = "00000000-0000-4000-8000-0000000000c1";
|
|
22
|
+
|
|
23
|
+
const item = (over: Partial<ChatInboxItem> = {}): ChatInboxItem =>
|
|
24
|
+
({
|
|
25
|
+
conversationId: C, kind: "direct", title: "Sam", subjectType: null, subjectId: null, status: null, role: "member",
|
|
26
|
+
otherUserId: null, memberCount: 2, lastSeq: 7, lastReadSeq: 7, unread: 0, mentioned: false, muted: false, pinned: false,
|
|
27
|
+
archived: false, lastMessageAt: new Date().toISOString(), lastMessage: null, ...over,
|
|
28
|
+
}) as ChatInboxItem;
|
|
29
|
+
|
|
30
|
+
type Fake = { [K in keyof ChatInboxActions]: ReturnType<typeof vi.fn<ChatInboxActions[K]>> };
|
|
31
|
+
const fakeActions = (): Fake =>
|
|
32
|
+
({
|
|
33
|
+
toggleUnread: vi.fn(async () => undefined),
|
|
34
|
+
togglePinned: vi.fn(async () => undefined),
|
|
35
|
+
toggleMuted: vi.fn(async () => undefined),
|
|
36
|
+
setArchived: vi.fn(async () => undefined),
|
|
37
|
+
deleteForMe: vi.fn(async () => undefined),
|
|
38
|
+
}) as unknown as Fake;
|
|
39
|
+
|
|
40
|
+
describe("inboxRowActions", () => {
|
|
41
|
+
it("is Messages' set, in its order, sides and colors", () => {
|
|
42
|
+
const actions = inboxRowActions(item(), fakeActions(), defaultInboxActionLabels, { confirm: async () => true });
|
|
43
|
+
expect(actions.map((a) => a.id)).toEqual(["unread", "pin", "alerts", "delete", "archive"]);
|
|
44
|
+
const by = (id: string) => actions.find((a) => a.id === id)!;
|
|
45
|
+
expect(by("unread")).toMatchObject({ tone: "info", swipe: { side: "leading", full: true }, icon: "message-unread", label: "Mark as Unread" });
|
|
46
|
+
expect(by("pin")).toMatchObject({ accent: "orange", swipe: { side: "leading" } });
|
|
47
|
+
expect(by("alerts")).toMatchObject({ accent: "purple", swipe: { side: "trailing" }, label: "Hide Alerts" });
|
|
48
|
+
expect(by("delete")).toMatchObject({ destructive: true, confirms: true, swipe: { side: "trailing", full: true } });
|
|
49
|
+
expect(by("archive")).toMatchObject({ swipe: false });
|
|
50
|
+
// An unread, pinned, muted, archived row offers the other half of each toggle, and no Archive.
|
|
51
|
+
const other = inboxRowActions(item({ unread: 2, pinned: true, muted: true, archived: true }), fakeActions(), defaultInboxActionLabels, { confirm: async () => true });
|
|
52
|
+
expect(other.map((a) => a.label)).toEqual(["Mark read", "Unpin", "Show Alerts", "Delete"]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("asks before Delete for me, and deletes only on yes; a failure reaches onError", async () => {
|
|
56
|
+
const act = fakeActions();
|
|
57
|
+
const confirm = vi.fn(async () => false);
|
|
58
|
+
const onError = vi.fn();
|
|
59
|
+
const row = item();
|
|
60
|
+
const del = () => inboxRowActions(row, act, defaultInboxActionLabels, { confirm, onError }).find((a) => a.id === "delete")!;
|
|
61
|
+
del().onPress();
|
|
62
|
+
await waitFor(() => expect(confirm).toHaveBeenCalledWith("Delete this conversation?", expect.stringMatching(/removed for you/), "Delete"));
|
|
63
|
+
expect(act.deleteForMe).not.toHaveBeenCalled();
|
|
64
|
+
confirm.mockResolvedValueOnce(true);
|
|
65
|
+
act.deleteForMe.mockRejectedValueOnce(new Error("Offline"));
|
|
66
|
+
del().onPress();
|
|
67
|
+
await waitFor(() => expect(act.deleteForMe).toHaveBeenCalledWith(row));
|
|
68
|
+
await waitFor(() => expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: "Offline" })));
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
function harness(over: Record<string, unknown> = {}) {
|
|
73
|
+
const client = {
|
|
74
|
+
inbox: vi.fn(async () => []),
|
|
75
|
+
subscribeInbox: vi.fn(() => ({ unsubscribe: vi.fn() })),
|
|
76
|
+
unreadTotal: vi.fn(async () => 0),
|
|
77
|
+
markRead: vi.fn(async () => 7),
|
|
78
|
+
setMySettings: vi.fn(async () => {}),
|
|
79
|
+
deleteConversationForMe: vi.fn(async () => {}),
|
|
80
|
+
...over,
|
|
81
|
+
} as unknown as ChatClient & Record<string, ReturnType<typeof vi.fn>>;
|
|
82
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
83
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
84
|
+
<QueryClientProvider client={qc}>
|
|
85
|
+
<ChatProvider client={client} userId={ME}>
|
|
86
|
+
{children}
|
|
87
|
+
</ChatProvider>
|
|
88
|
+
</QueryClientProvider>
|
|
89
|
+
);
|
|
90
|
+
return { client, qc, ...renderHook(() => useInboxActions(), { wrapper }) };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
describe("useInboxActions", () => {
|
|
94
|
+
it("runs from the row: each toggle reads the row's state", async () => {
|
|
95
|
+
const { client, result } = harness();
|
|
96
|
+
await act(() => result.current.toggleUnread(item({ unread: 0 })));
|
|
97
|
+
expect(client.setMySettings).toHaveBeenLastCalledWith(C, { markedUnread: true });
|
|
98
|
+
await act(() => result.current.toggleUnread(item({ unread: 3 })));
|
|
99
|
+
expect(client.markRead).toHaveBeenLastCalledWith(C);
|
|
100
|
+
await act(() => result.current.togglePinned(item({ pinned: true })));
|
|
101
|
+
expect(client.setMySettings).toHaveBeenLastCalledWith(C, { pinned: false });
|
|
102
|
+
await act(() => result.current.toggleMuted(item()));
|
|
103
|
+
expect(client.setMySettings).toHaveBeenLastCalledWith(C, { mutedUntil: "9999-12-31T00:00:00.000Z" });
|
|
104
|
+
await act(() => result.current.toggleMuted(item(), "2026-12-01T00:00:00.000Z"));
|
|
105
|
+
expect(client.setMySettings).toHaveBeenLastCalledWith(C, { mutedUntil: "2026-12-01T00:00:00.000Z" });
|
|
106
|
+
await act(() => result.current.toggleMuted(item({ muted: true })));
|
|
107
|
+
expect(client.setMySettings).toHaveBeenLastCalledWith(C, { mutedUntil: null });
|
|
108
|
+
await act(() => result.current.setArchived(item(), true));
|
|
109
|
+
expect(client.setMySettings).toHaveBeenLastCalledWith(C, { archived: true });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("Delete for me goes up to the row's lastSeq and forgets the conversation; every action refreshes through the prefix keys", async () => {
|
|
113
|
+
const { client, qc, result } = harness();
|
|
114
|
+
qc.setQueryData([...chatKeys.conversation(C), "messages", null, null], { pages: [] });
|
|
115
|
+
qc.setQueryData(chatKeys.pins(C), ["m1"]);
|
|
116
|
+
qc.setQueryData(chatKeys.search("hello", null), []);
|
|
117
|
+
qc.setQueryData(chatKeys.inbox({ archived: false }), []);
|
|
118
|
+
const invalidate = vi.spyOn(qc, "invalidateQueries");
|
|
119
|
+
await act(() => result.current.deleteForMe(item({ lastSeq: 9 })));
|
|
120
|
+
expect(client.deleteConversationForMe).toHaveBeenCalledWith(C, 9);
|
|
121
|
+
expect(qc.getQueryData([...chatKeys.conversation(C), "messages", null, null])).toBeUndefined();
|
|
122
|
+
expect(qc.getQueryData(chatKeys.pins(C))).toBeUndefined();
|
|
123
|
+
expect(qc.getQueryData(chatKeys.search("hello", null))).toBeUndefined();
|
|
124
|
+
expect(invalidate).toHaveBeenCalledWith({ queryKey: chatKeys.inboxAll() });
|
|
125
|
+
expect(invalidate).toHaveBeenCalledWith({ queryKey: chatKeys.unread() });
|
|
126
|
+
expect(chatKeys.inbox({ archived: false }).slice(0, 2)).toEqual([...chatKeys.inboxAll()]);
|
|
127
|
+
expect(chatKeys.search("hello").slice(0, 2)).toEqual([...chatKeys.searchAll()]);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("useConfirmDialog", () => {
|
|
132
|
+
it("answers an open question no when it unmounts", async () => {
|
|
133
|
+
const { result, unmount } = renderHook(() => useConfirmDialog());
|
|
134
|
+
let asked!: Promise<boolean>;
|
|
135
|
+
act(() => {
|
|
136
|
+
asked = result.current.confirm("Delete this conversation?", "Gone for you.", "Delete");
|
|
137
|
+
});
|
|
138
|
+
unmount();
|
|
139
|
+
await expect(asked).resolves.toBe(false);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("the memory client's archive", () => {
|
|
144
|
+
it("stores Archive, reports it on the row, leaves it out of the inbox and lists it under archived", async () => {
|
|
145
|
+
const B = "00000000-0000-4000-8000-0000000000b2";
|
|
146
|
+
const client = createMemoryChatClient({
|
|
147
|
+
me: ME,
|
|
148
|
+
conversations: [
|
|
149
|
+
{ id: "c1", members: [{ userId: ME }, { userId: B }] },
|
|
150
|
+
{ id: "c2", members: [{ userId: ME }, { userId: B }], archived: true },
|
|
151
|
+
],
|
|
152
|
+
});
|
|
153
|
+
expect((await client.inbox()).map((i) => i.conversationId)).toEqual(["c1"]);
|
|
154
|
+
expect(await client.inbox({ archived: true })).toEqual([expect.objectContaining({ conversationId: "c2", archived: true })]);
|
|
155
|
+
await client.setMySettings("c1", { archived: true });
|
|
156
|
+
expect(await client.inbox()).toEqual([]);
|
|
157
|
+
expect((await client.inbox({ archived: true })).map((i) => i.conversationId).sort()).toEqual(["c1", "c2"]);
|
|
158
|
+
await client.setMySettings("c2", { archived: false });
|
|
159
|
+
expect(await client.inbox()).toEqual([expect.objectContaining({ conversationId: "c2", archived: false })]);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("Delete for me while a catch-up is under way", () => {
|
|
164
|
+
const msg = (seq: number) => ({ id: `m${seq}`, conversationId: C, seq, threadRootId: null, topic: null, body: `hi ${seq}`, senderId: ME, createdAt: new Date().toISOString() }) as unknown as ChatMessage;
|
|
165
|
+
it("drops what the earlier fetch brings back, and a load started afterwards still fills the thread", async () => {
|
|
166
|
+
let release: (v: { messages: ChatMessage[]; hasEarlier: boolean }) => void = () => {};
|
|
167
|
+
let held = true;
|
|
168
|
+
const client = {
|
|
169
|
+
inbox: vi.fn(async () => []),
|
|
170
|
+
subscribeInbox: vi.fn(() => ({ unsubscribe: vi.fn() })),
|
|
171
|
+
unreadTotal: vi.fn(async () => 0),
|
|
172
|
+
conversation: vi.fn(async () => ({ id: C, kind: "direct" })),
|
|
173
|
+
kind: vi.fn(async () => ({ maxMembers: 2 })),
|
|
174
|
+
members: vi.fn(async () => []),
|
|
175
|
+
myRole: vi.fn(async () => "member"),
|
|
176
|
+
canPost: vi.fn(async () => true),
|
|
177
|
+
markRead: vi.fn(async () => 7),
|
|
178
|
+
deleteConversationForMe: vi.fn(async () => {}),
|
|
179
|
+
messages: vi.fn(async (_id: string, o: { afterSeq?: number }) => {
|
|
180
|
+
if (o.afterSeq === undefined) return { messages: [], hasEarlier: false };
|
|
181
|
+
if (held) return new Promise((r) => (release = r));
|
|
182
|
+
return { messages: [msg(10)], hasEarlier: false };
|
|
183
|
+
}),
|
|
184
|
+
subscribeConversation: vi.fn((_id: string, _u: string, h: { onJoin?: () => void }) => {
|
|
185
|
+
setTimeout(() => h.onJoin?.(), 0);
|
|
186
|
+
return { unsubscribe: vi.fn(), signal: vi.fn() };
|
|
187
|
+
}),
|
|
188
|
+
} as unknown as ChatClient;
|
|
189
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
190
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
191
|
+
<QueryClientProvider client={qc}>
|
|
192
|
+
<ChatProvider client={client} userId={ME}>{children}</ChatProvider>
|
|
193
|
+
</QueryClientProvider>
|
|
194
|
+
);
|
|
195
|
+
const { result } = renderHook(() => ({ convo: useConversation(C, { markRead: false }), actions: useInboxActions() }), { wrapper });
|
|
196
|
+
// The catch-up on join is waiting on its page of messages.
|
|
197
|
+
await waitFor(() => expect(client.messages).toHaveBeenCalledWith(C, expect.objectContaining({ afterSeq: 0 })));
|
|
198
|
+
await act(() => result.current.actions.deleteForMe(item({ lastSeq: 9 })));
|
|
199
|
+
const key = [...chatKeys.conversation(C), "messages", null, null];
|
|
200
|
+
expect(qc.getQueryData(key)).toBeUndefined();
|
|
201
|
+
// The old page arrives after the delete: it holds the deleted history, and is dropped.
|
|
202
|
+
await act(async () => release({ messages: [msg(1), msg(2)], hasEarlier: false }));
|
|
203
|
+
await act(async () => new Promise((r) => setTimeout(r, 10)));
|
|
204
|
+
expect((qc.getQueryData(key) as { messages: ChatMessage[] } | undefined)?.messages.some((m) => m.seq < 10)).not.toBe(true);
|
|
205
|
+
// A catch-up started after the delete (a new message brought the conversation back) fills it.
|
|
206
|
+
held = false;
|
|
207
|
+
await act(() => result.current.convo.refetch());
|
|
208
|
+
await waitFor(() => expect((qc.getQueryData(key) as { messages: ChatMessage[] }).messages.map((m) => m.seq)).toEqual([10]));
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("ChatInbox (web) row actions", () => {
|
|
213
|
+
function mount(ui: ReactNode, client = createMemoryChatClient({ me: ME, conversations: [{ id: "c1", title: "Morning crew", members: [{ userId: ME }, { userId: "00000000-0000-4000-8000-0000000000b2" }], messages: [] }] })) {
|
|
214
|
+
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
215
|
+
render(
|
|
216
|
+
<QueryClientProvider client={queryClient}>
|
|
217
|
+
<ChatProvider client={client} userId={ME}>
|
|
218
|
+
{ui}
|
|
219
|
+
</ChatProvider>
|
|
220
|
+
</QueryClientProvider>,
|
|
221
|
+
);
|
|
222
|
+
return client;
|
|
223
|
+
}
|
|
224
|
+
const resolveUser = () => ({ name: "Someone" });
|
|
225
|
+
const openMenu = async (testID: string) => {
|
|
226
|
+
const more = await screen.findByTestId(`${testID}-more`);
|
|
227
|
+
fireEvent.pointerDown(more, new PointerEvent("pointerdown", { bubbles: true, button: 0, ctrlKey: false }));
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
it("offers each row's actions in its More menu, and asks before Delete for me", async () => {
|
|
231
|
+
const client = mount(<ChatInbox resolveUser={resolveUser} onSelect={() => {}} />);
|
|
232
|
+
await openMenu("genesis-chat-inbox-row-c1");
|
|
233
|
+
expect(await screen.findByTestId("genesis-chat-inbox-row-c1-action-unread")).toBeTruthy();
|
|
234
|
+
expect(screen.getByTestId("genesis-chat-inbox-row-c1-action-archive")).toBeTruthy();
|
|
235
|
+
fireEvent.click(screen.getByTestId("genesis-chat-inbox-row-c1-action-delete"));
|
|
236
|
+
const confirm = await screen.findByTestId("genesis-chat-inbox-confirm");
|
|
237
|
+
expect(screen.getByText("Delete this conversation?")).toBeTruthy();
|
|
238
|
+
expect(client.calls.some((c) => c.method === "deleteConversationForMe")).toBe(false);
|
|
239
|
+
fireEvent.click(confirm);
|
|
240
|
+
await waitFor(() => expect(client.calls.some((c) => c.method === "deleteConversationForMe")).toBe(true));
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("takes rowActions to reshape them", async () => {
|
|
244
|
+
const rowActions = vi.fn((_item: ChatInboxItem, defaults: { id: string }[]) => defaults.filter((a) => a.id === "pin"));
|
|
245
|
+
mount(<ChatInbox resolveUser={resolveUser} onSelect={() => {}} rowActions={rowActions as never} />);
|
|
246
|
+
await openMenu("genesis-chat-inbox-row-c1");
|
|
247
|
+
expect(await screen.findByTestId("genesis-chat-inbox-row-c1-action-pin")).toBeTruthy();
|
|
248
|
+
expect(screen.queryByTestId("genesis-chat-inbox-row-c1-action-delete")).toBeNull();
|
|
249
|
+
expect(rowActions).toHaveBeenCalledWith(expect.objectContaining({ conversationId: "c1" }), expect.any(Array));
|
|
250
|
+
});
|
|
251
|
+
});
|