@marktiderman/genesis-chat 0.9.0 → 0.10.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 +1 -1
- package/dist/native.d.ts +3 -1
- package/dist/native.js +75 -73
- package/dist/native.js.map +1 -1
- package/dist/web.d.ts +7 -3
- package/dist/web.js +269 -265
- package/dist/web.js.map +1 -1
- package/migrations/0005_genesis_chat_attachment_paths.sql +80 -0
- package/migrations/tests/0005_genesis_chat_attachment_paths.test.sql +111 -0
- package/package.json +13 -9
- package/src/__tests__/web-attachment-view.test.ts +33 -0
- package/src/__tests__/web.test.tsx +160 -3
- package/src/native/__tests__/composer.test.tsx +75 -0
- package/src/native/__tests__/organise-screens.test.tsx +1 -1
- package/src/native/__tests__/thread-screen.test.tsx +65 -1
- package/src/native/composer.tsx +53 -79
- package/src/native/context.tsx +9 -2
- package/src/native/message-bubble.tsx +2 -2
- package/src/native/modules.ts +13 -18
- package/src/native/organise-copy.ts +3 -0
- package/src/native/thread-screen.tsx +17 -2
- package/src/web/attachments.tsx +16 -3
- package/src/web/bubble.tsx +4 -2
- package/src/web/composer.tsx +2 -4
- package/src/web/dock.tsx +3 -3
- package/src/web/inbox.tsx +82 -90
- package/src/web/thread.tsx +248 -259
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
-- Genesis Chat 0005: attachment paths are tightened
|
|
2
|
+
--
|
|
3
|
+
-- Builds on 0001–0004 (never edited once released). Needs nothing from your app, and no other
|
|
4
|
+
-- package's migrations depend on where it runs after chat 0001.
|
|
5
|
+
--
|
|
6
|
+
-- 1. An attachment has a `path` only when its provider is `storage`, by a check on the table.
|
|
7
|
+
-- send_message refuses anything else with 23514; a scheduled message holding one records that
|
|
8
|
+
-- as its `error` when it comes due, as with any refusal. Genesis's own clients never send one.
|
|
9
|
+
-- 2. Read access counts `path` only on storage attachments, and `poster_path` on any provider
|
|
10
|
+
-- (a hosted video's poster lives in the bucket).
|
|
11
|
+
-- 3. Paths on other providers are cleared, with the transcript of a voice row among them, and
|
|
12
|
+
-- kept in `genesis_chat.attachment_paths_cleared` for the service role. A link keeps its `url`,
|
|
13
|
+
-- and a video its provider id.
|
|
14
|
+
--
|
|
15
|
+
-- `attachment_paths_cleared` holds object paths, which contain user ids, and nothing removes them:
|
|
16
|
+
-- account deletion doesn't touch this table, and neither does retention. Look at it once after
|
|
17
|
+
-- this runs (usually it's empty), then drop it, `drop table genesis_chat.attachment_paths_cleared`,
|
|
18
|
+
-- and sooner if a person's data must go.
|
|
19
|
+
|
|
20
|
+
create table genesis_chat.attachment_paths_cleared (
|
|
21
|
+
attachment_id uuid primary key,
|
|
22
|
+
message_id uuid not null,
|
|
23
|
+
conversation_id uuid not null,
|
|
24
|
+
provider text not null,
|
|
25
|
+
path text not null,
|
|
26
|
+
cleared_at timestamptz not null default now()
|
|
27
|
+
);
|
|
28
|
+
alter table genesis_chat.attachment_paths_cleared enable row level security;
|
|
29
|
+
revoke all on genesis_chat.attachment_paths_cleared from public, anon, authenticated;
|
|
30
|
+
grant select on genesis_chat.attachment_paths_cleared to service_role;
|
|
31
|
+
|
|
32
|
+
-- A function so its test can run it: rows are refused once the check below is on. Returns how many it cleared.
|
|
33
|
+
create function genesis_chat.clear_paths_outside_storage()
|
|
34
|
+
returns integer
|
|
35
|
+
language plpgsql
|
|
36
|
+
set search_path = ''
|
|
37
|
+
as $$
|
|
38
|
+
declare
|
|
39
|
+
cleared integer;
|
|
40
|
+
begin
|
|
41
|
+
insert into genesis_chat.attachment_paths_cleared (attachment_id, message_id, conversation_id, provider, path)
|
|
42
|
+
select id, message_id, conversation_id, provider, path
|
|
43
|
+
from genesis_chat.attachments
|
|
44
|
+
where provider <> 'storage' and path is not null
|
|
45
|
+
on conflict (attachment_id) do nothing;
|
|
46
|
+
update genesis_chat.attachments
|
|
47
|
+
set path = null,
|
|
48
|
+
transcript = case when kind = 'voice' then null else transcript end
|
|
49
|
+
where provider <> 'storage' and path is not null;
|
|
50
|
+
get diagnostics cleared = row_count;
|
|
51
|
+
return cleared;
|
|
52
|
+
end;
|
|
53
|
+
$$;
|
|
54
|
+
revoke all on function genesis_chat.clear_paths_outside_storage() from public, anon, authenticated, service_role;
|
|
55
|
+
|
|
56
|
+
select genesis_chat.clear_paths_outside_storage();
|
|
57
|
+
|
|
58
|
+
alter table genesis_chat.attachments
|
|
59
|
+
add constraint attachments_path_only_in_storage check (provider = 'storage' or path is null);
|
|
60
|
+
|
|
61
|
+
-- May the signed-in person read `object_name`? Whoever reads a message that carries it as a file
|
|
62
|
+
-- in storage or a poster; its uploader for a day before it is sent.
|
|
63
|
+
create or replace function genesis_chat.can_read_object(object_name text, object_owner text, uploaded_at timestamptz)
|
|
64
|
+
returns boolean
|
|
65
|
+
language sql
|
|
66
|
+
stable
|
|
67
|
+
security definer
|
|
68
|
+
set search_path = ''
|
|
69
|
+
as $$
|
|
70
|
+
select auth.uid() is not null and (
|
|
71
|
+
exists (
|
|
72
|
+
select 1 from genesis_chat.attachments a
|
|
73
|
+
join genesis_chat.messages m on m.id = a.message_id
|
|
74
|
+
where ((a.provider = 'storage' and a.path = object_name) or a.poster_path = object_name)
|
|
75
|
+
and m.deleted_at is null
|
|
76
|
+
and genesis_chat.can_view(m.conversation_id)
|
|
77
|
+
and (m.visibility = 'everyone' or genesis_chat.observes(m.conversation_id)))
|
|
78
|
+
or (object_owner = auth.uid()::text and split_part(object_name, '/', 2) = auth.uid()::text
|
|
79
|
+
and uploaded_at > now() - interval '1 day'))
|
|
80
|
+
$$;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- pgTAP test for 0005_genesis_chat_attachment_paths.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 and share a file; e5 and d4 talk directly, and aren't in it.
|
|
7
|
+
begin;
|
|
8
|
+
create extension if not exists pgtap with schema extensions;
|
|
9
|
+
select plan(18);
|
|
10
|
+
|
|
11
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
12
|
+
select ('00000000-0000-4000-8000-00000000c0' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
13
|
+
from unnest(array['a1', 'b2', 'd4', 'e5']) 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-0000000000f0'::uuid, viewer
|
|
19
|
+
$$;
|
|
20
|
+
|
|
21
|
+
create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c0' || 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.file(conv text, who text, name text) returns text language sql stable as $$
|
|
34
|
+
select pg_temp.v(conv) || '/' || pg_temp.person(who) || '/' || name
|
|
35
|
+
$$;
|
|
36
|
+
grant execute on function pg_temp.v(text), pg_temp.file(text, text, text) to authenticated;
|
|
37
|
+
|
|
38
|
+
create function public.genesis_chat_test_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_test_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 pg_temp.sign_in('e5');
|
|
46
|
+
insert into t select 'ed', genesis_chat.start_conversation('direct', array[pg_temp.person('d4')]);
|
|
47
|
+
|
|
48
|
+
reset role;
|
|
49
|
+
insert into storage.objects (bucket_id, name, owner_id) values
|
|
50
|
+
('genesis-chat', pg_temp.file('ab', 'a1', 'photo.jpg'), pg_temp.person('a1')::text),
|
|
51
|
+
('genesis-chat', pg_temp.file('ab', 'a1', 'poster.jpg'), pg_temp.person('a1')::text);
|
|
52
|
+
|
|
53
|
+
select pg_temp.sign_in('a1');
|
|
54
|
+
select lives_ok($$ select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), '',
|
|
55
|
+
jsonb_build_array(jsonb_build_object('kind', 'image', 'path', pg_temp.file('ab', 'a1', 'photo.jpg')))) $$,
|
|
56
|
+
'a file in storage still sends');
|
|
57
|
+
select lives_ok($$ select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), '',
|
|
58
|
+
jsonb_build_array(jsonb_build_object('kind', 'video', 'provider', 'mux', 'provider_id', 'up_1', 'poster_path', pg_temp.file('ab', 'a1', 'poster.jpg')))) $$,
|
|
59
|
+
'a hosted video with a poster still sends');
|
|
60
|
+
select lives_ok($$ select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), '',
|
|
61
|
+
jsonb_build_array(jsonb_build_object('kind', 'link', 'provider', 'external', 'url', 'https://example.test/a'))) $$,
|
|
62
|
+
'a link with no path still sends');
|
|
63
|
+
|
|
64
|
+
select pg_temp.sign_in('b2');
|
|
65
|
+
select ok(genesis_chat.can_read_object(pg_temp.file('ab', 'a1', 'photo.jpg'), pg_temp.person('a1')::text, now() - interval '2 days'),
|
|
66
|
+
'a member reads a file in storage');
|
|
67
|
+
select ok(genesis_chat.can_read_object(pg_temp.file('ab', 'a1', 'poster.jpg'), pg_temp.person('a1')::text, now() - interval '2 days'),
|
|
68
|
+
'and a hosted video''s poster');
|
|
69
|
+
|
|
70
|
+
select pg_temp.sign_in('e5');
|
|
71
|
+
select ok(not genesis_chat.can_read_object(pg_temp.file('ab', 'a1', 'photo.jpg'), pg_temp.person('a1')::text, now() - interval '2 days'),
|
|
72
|
+
'someone outside the conversation doesn''t');
|
|
73
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('ed'), gen_random_uuid(), '',
|
|
74
|
+
jsonb_build_array(jsonb_build_object('kind', 'file', 'provider', 'external', 'path', pg_temp.file('ab', 'a1', 'photo.jpg'), 'url', 'https://example.test/a'))) $$,
|
|
75
|
+
'23514', null, 'only a file in storage has a path');
|
|
76
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('ed'), gen_random_uuid(), '',
|
|
77
|
+
jsonb_build_array(jsonb_build_object('kind', 'file', 'provider', 'none', 'path', pg_temp.file('ab', 'a1', 'photo.jpg')))) $$,
|
|
78
|
+
'23514', null, 'whatever the other provider');
|
|
79
|
+
select ok(not genesis_chat.can_read_object(pg_temp.file('ab', 'a1', 'photo.jpg'), pg_temp.person('a1')::text, now() - interval '2 days'),
|
|
80
|
+
'and being refused grants nothing');
|
|
81
|
+
|
|
82
|
+
-- A row written before this migration, with the rule out of the way for the test.
|
|
83
|
+
reset role;
|
|
84
|
+
alter table genesis_chat.attachments drop constraint if exists attachments_path_only_in_storage;
|
|
85
|
+
select pg_temp.sign_in('e5');
|
|
86
|
+
insert into t select 'older', (genesis_chat.send_message(pg_temp.v('ed'), gen_random_uuid(), 'older')).id;
|
|
87
|
+
reset role;
|
|
88
|
+
insert into genesis_chat.attachments (message_id, conversation_id, position, kind, provider, path)
|
|
89
|
+
values (pg_temp.v('older'), pg_temp.v('ed'), 0, 'file', 'external', pg_temp.file('ab', 'a1', 'photo.jpg'));
|
|
90
|
+
select pg_temp.sign_in('e5');
|
|
91
|
+
select ok(not genesis_chat.can_read_object(pg_temp.file('ab', 'a1', 'photo.jpg'), pg_temp.person('a1')::text, now() - interval '2 days'),
|
|
92
|
+
'an older row that isn''t in storage grants nothing either');
|
|
93
|
+
select throws_ok($$ select * from genesis_chat.attachment_paths_cleared $$, '42501', null, 'the record of cleared paths isn''t for people');
|
|
94
|
+
select pg_temp.sign_in('b2');
|
|
95
|
+
select ok(genesis_chat.can_read_object(pg_temp.file('ab', 'a1', 'photo.jpg'), pg_temp.person('a1')::text, now() - interval '2 days'),
|
|
96
|
+
'and the member''s own access is unchanged');
|
|
97
|
+
|
|
98
|
+
-- What the migration clears, on rows the check would now refuse.
|
|
99
|
+
reset role;
|
|
100
|
+
insert into genesis_chat.attachments (message_id, conversation_id, position, kind, provider, path, url, transcript)
|
|
101
|
+
values (pg_temp.v('older'), pg_temp.v('ed'), 1, 'voice', 'none', 'x/y/voice.m4a', null, 'words'),
|
|
102
|
+
(pg_temp.v('older'), pg_temp.v('ed'), 2, 'link', 'external', 'x/y/link.jpg', 'https://example.test/keep', null);
|
|
103
|
+
select is(genesis_chat.clear_paths_outside_storage(), 3, 'the backfill clears every path outside storage');
|
|
104
|
+
select is((select count(*)::int from genesis_chat.attachments where provider <> 'storage' and path is not null), 0, 'and leaves none');
|
|
105
|
+
select is((select transcript from genesis_chat.attachments where message_id = pg_temp.v('older') and kind = 'voice'), null, 'a cleared voice row loses its transcript');
|
|
106
|
+
select is((select url from genesis_chat.attachments where message_id = pg_temp.v('older') and kind = 'link'), 'https://example.test/keep', 'a link keeps its url');
|
|
107
|
+
select is((select count(*)::int from genesis_chat.attachment_paths_cleared where message_id = pg_temp.v('older')), 3, 'and each is on record');
|
|
108
|
+
select ok(exists (select 1 from genesis_chat.attachments where provider = 'storage' and path = pg_temp.file('ab', 'a1', 'photo.jpg')), 'a file in storage keeps its path');
|
|
109
|
+
|
|
110
|
+
select * from finish();
|
|
111
|
+
rollback;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marktiderman/genesis-chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -86,14 +86,14 @@
|
|
|
86
86
|
"README.md"
|
|
87
87
|
],
|
|
88
88
|
"dependencies": {
|
|
89
|
-
"@marktiderman/genesis-features": "^0.8.
|
|
89
|
+
"@marktiderman/genesis-features": "^0.8.2",
|
|
90
90
|
"frimousse": "0.4.0"
|
|
91
91
|
},
|
|
92
92
|
"peerDependencies": {
|
|
93
93
|
"@marktiderman/genesis-ai": ">=0.1.0 <1.0.0",
|
|
94
|
-
"@marktiderman/genesis-core": ">=
|
|
95
|
-
"@marktiderman/genesis-ui": ">=2.
|
|
96
|
-
"@marktiderman/genesis-ui-native": ">=3.
|
|
94
|
+
"@marktiderman/genesis-core": ">=2.13.0 <3.0.0",
|
|
95
|
+
"@marktiderman/genesis-ui": ">=2.32.0 <3.0.0",
|
|
96
|
+
"@marktiderman/genesis-ui-native": ">=3.8.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",
|
|
@@ -174,11 +174,11 @@
|
|
|
174
174
|
},
|
|
175
175
|
"devDependencies": {
|
|
176
176
|
"@ai-sdk/anthropic": "4.0.88",
|
|
177
|
-
"@marktiderman/genesis-ai": "^0.
|
|
178
|
-
"@marktiderman/genesis-core": "^2.
|
|
177
|
+
"@marktiderman/genesis-ai": "^0.3.0",
|
|
178
|
+
"@marktiderman/genesis-core": "^2.13.0",
|
|
179
179
|
"@marktiderman/genesis-notifications": "^0.4.2",
|
|
180
|
-
"@marktiderman/genesis-ui": "^2.
|
|
181
|
-
"@marktiderman/genesis-ui-native": "^3.
|
|
180
|
+
"@marktiderman/genesis-ui": "^2.32.0",
|
|
181
|
+
"@marktiderman/genesis-ui-native": "^3.9.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",
|
|
@@ -247,6 +247,10 @@
|
|
|
247
247
|
"how": "every conversation is in a tenant from chat 0004. Add a migration before it that defines private.genesis_viewer_people(viewer uuid) returns table (tenant_id uuid, person_id uuid): one row per tenant the login is in (a single-tenant app returns its one tenant id; a personal record's tenant can be its own id). See genesis-supabase 0016's header."
|
|
248
248
|
}
|
|
249
249
|
]
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"file": "migrations/0005_genesis_chat_attachment_paths.sql",
|
|
253
|
+
"test": "migrations/tests/0005_genesis_chat_attachment_paths.test.sql"
|
|
250
254
|
}
|
|
251
255
|
],
|
|
252
256
|
"install": [
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment happy-dom
|
|
3
|
+
* @feat:chat
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it } from "vitest";
|
|
6
|
+
import type { ChatAttachment } from "../core/types";
|
|
7
|
+
import { toAttachmentView } from "../web/attachments";
|
|
8
|
+
|
|
9
|
+
const attachment = (over: Partial<ChatAttachment>): ChatAttachment => ({ id: "a", kind: "image", provider: "storage", path: null, url: null, name: "a.png", ...over }) as ChatAttachment;
|
|
10
|
+
|
|
11
|
+
describe("toAttachmentView", () => {
|
|
12
|
+
it("draws a signed picture", () => {
|
|
13
|
+
expect(toAttachmentView(attachment({ path: "p/a.png" }), { "p/a.png": "https://signed.test/a.png" })).toMatchObject({ kind: "image", url: "https://signed.test/a.png" });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("makes a picture from outside storage a link naming its host", () => {
|
|
17
|
+
expect(toAttachmentView(attachment({ provider: "external", url: "https://elsewhere.test/b.png" }), {})).toMatchObject({ kind: "file", url: "https://elsewhere.test/b.png", name: "a.png (elsewhere.test)" });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("does the same for video and voice from outside storage", () => {
|
|
21
|
+
for (const kind of ["video", "voice"] as const) {
|
|
22
|
+
expect(toAttachmentView(attachment({ kind, provider: "external", url: "https://elsewhere.test/b.mp4" }), {})).toMatchObject({ kind: "file", name: "a.png (elsewhere.test)" });
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("gives a stored file that didn't sign no URL, whatever the message carries", () => {
|
|
27
|
+
expect(toAttachmentView(attachment({ provider: "external", path: "x", url: "https://elsewhere.test/b.png" }), {})).toMatchObject({ url: null });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("keeps a link's own address", () => {
|
|
31
|
+
expect(toAttachmentView(attachment({ kind: "link", url: "https://example.test/" }), {})).toMatchObject({ kind: "link", url: "https://example.test/" });
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -6,6 +6,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
6
6
|
import { act, cleanup, configure, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
|
7
7
|
import type { ReactNode } from "react";
|
|
8
8
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
9
|
+
import { toast } from "@marktiderman/genesis-ui";
|
|
9
10
|
import { ChatProvider } from "../react";
|
|
10
11
|
import { ChatComposer, ChatInbox, ChatThread, VoiceNote, createMemoryChatClient, type MemoryChatClient, type MemoryConversationSeed } from "../web";
|
|
11
12
|
|
|
@@ -188,7 +189,7 @@ describe("ChatThread", () => {
|
|
|
188
189
|
HTMLElement.prototype.scrollIntoView = intoView;
|
|
189
190
|
try {
|
|
190
191
|
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} />);
|
|
191
|
-
const scroller = await screen.findByTestId("genesis-chat-
|
|
192
|
+
const scroller = await screen.findByTestId("genesis-chat-thread-page-messages");
|
|
192
193
|
const scrollTo = vi.fn();
|
|
193
194
|
scroller.scrollTo = scrollTo as unknown as typeof scroller.scrollTo;
|
|
194
195
|
fireEvent.click(await screen.findByTestId("genesis-chat-reply-quote"));
|
|
@@ -202,6 +203,140 @@ describe("ChatThread", () => {
|
|
|
202
203
|
}
|
|
203
204
|
});
|
|
204
205
|
|
|
206
|
+
it("the pill shows once scrolled up, and goes back to the newest", async () => {
|
|
207
|
+
const height = vi.spyOn(HTMLElement.prototype, "scrollHeight", "get").mockImplementation(() => 2000);
|
|
208
|
+
// All read, so it opens at the newest.
|
|
209
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group({ members: [{ userId: ME, lastReadSeq: 99 }, { userId: SAM }, { userId: LEE }] })] });
|
|
210
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} />);
|
|
211
|
+
const log = await screen.findByTestId("genesis-chat-thread-page-messages");
|
|
212
|
+
await screen.findByText("Hi Sam");
|
|
213
|
+
expect(screen.queryByTestId("genesis-chat-thread-page-jump-latest")).toBeNull();
|
|
214
|
+
log.scrollTop = 100;
|
|
215
|
+
fireEvent.scroll(log);
|
|
216
|
+
const pill = await screen.findByTestId("genesis-chat-thread-page-jump-latest");
|
|
217
|
+
const scrollTo = vi.fn();
|
|
218
|
+
log.scrollTo = scrollTo as unknown as typeof log.scrollTo;
|
|
219
|
+
fireEvent.click(pill);
|
|
220
|
+
expect(scrollTo).toHaveBeenCalledWith(expect.objectContaining({ top: 2000 }));
|
|
221
|
+
expect(screen.queryByTestId("genesis-chat-thread-page-jump-latest")).toBeNull();
|
|
222
|
+
height.mockRestore();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("sits on ThreadPage: a landed message keeps its row", async () => {
|
|
226
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
227
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} />);
|
|
228
|
+
const input = await screen.findByTestId("genesis-chat-input");
|
|
229
|
+
const release = client.holdNextSend();
|
|
230
|
+
fireEvent.change(input, { target: { value: "Keep my row" } });
|
|
231
|
+
fireEvent.keyDown(input, { key: "Enter" });
|
|
232
|
+
const pending = await screen.findByTestId("genesis-chat-pending-sending");
|
|
233
|
+
const row = pending.closest("[data-row-id]");
|
|
234
|
+
expect(row).not.toBeNull();
|
|
235
|
+
release();
|
|
236
|
+
await waitFor(() => expect(screen.queryByTestId("genesis-chat-pending-sending")).toBeNull());
|
|
237
|
+
// The same element: the row wasn't remounted when the server's copy landed.
|
|
238
|
+
expect(screen.getByText("Keep my row").closest("[data-row-id]")).toBe(row);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("goes to the same message twice: a second click on the quote highlights it again", async () => {
|
|
242
|
+
const client = createMemoryChatClient({
|
|
243
|
+
me: ME,
|
|
244
|
+
conversations: [group({ messages: [...group().messages!, { id: "m6", senderId: SAM, body: "Yes!", replyToId: "m3", createdAt: at(7) }] })],
|
|
245
|
+
});
|
|
246
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} />);
|
|
247
|
+
fireEvent.click(await screen.findByTestId("genesis-chat-reply-quote"));
|
|
248
|
+
await waitFor(() => expect(document.querySelector("#genesis-chat-msg-m3 .ring-2")).not.toBeNull());
|
|
249
|
+
await waitFor(() => expect(document.querySelector("#genesis-chat-msg-m3 .ring-2")).toBeNull(), { timeout: 3000 });
|
|
250
|
+
fireEvent.click(screen.getByTestId("genesis-chat-reply-quote"));
|
|
251
|
+
await waitFor(() => expect(document.querySelector("#genesis-chat-msg-m3 .ring-2")).not.toBeNull());
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
describe("a jump that doesn't land", () => {
|
|
255
|
+
afterEach(() => vi.restoreAllMocks());
|
|
256
|
+
|
|
257
|
+
it("says a load failed, not that the message is gone", async () => {
|
|
258
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
259
|
+
const real = client.messages.bind(client);
|
|
260
|
+
vi.spyOn(client, "messages").mockImplementationOnce(async (...args) => ({ ...(await real(...args)), hasEarlier: true })).mockRejectedValue(new Error("offline"));
|
|
261
|
+
const error = vi.spyOn(toast, "error");
|
|
262
|
+
const info = vi.spyOn(toast, "info");
|
|
263
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} jumpTo={{ messageId: "nope", seq: 0 }} />);
|
|
264
|
+
await waitFor(() => expect(error).toHaveBeenCalledWith("offline"));
|
|
265
|
+
await act(async () => void (await new Promise((r) => setTimeout(r, 600))));
|
|
266
|
+
expect(info).not.toHaveBeenCalled();
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("tells a screen reader only what really happened after a failed load", async () => {
|
|
270
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
271
|
+
const real = client.messages.bind(client);
|
|
272
|
+
vi.spyOn(client, "messages").mockImplementationOnce(async (...args) => ({ ...(await real(...args)), hasEarlier: true })).mockRejectedValue(new Error("offline"));
|
|
273
|
+
const error = vi.spyOn(toast, "error");
|
|
274
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} jumpTo={{ messageId: "nope", seq: 0 }} />);
|
|
275
|
+
await waitFor(() => expect(error).toHaveBeenCalled());
|
|
276
|
+
await act(async () => void (await new Promise((r) => setTimeout(r, 600))));
|
|
277
|
+
expect(screen.getAllByRole("status").some((el) => /isn't (here|available)/.test(el.textContent ?? ""))).toBe(false);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("with a deep link and a first page that failed: offers Try again, never gone, and goes there once rows come", async () => {
|
|
281
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
282
|
+
const real = client.messages.bind(client);
|
|
283
|
+
let offline = true;
|
|
284
|
+
vi.spyOn(client, "messages").mockImplementation(async (...args) => {
|
|
285
|
+
if (offline) throw new Error("offline");
|
|
286
|
+
return real(...args);
|
|
287
|
+
});
|
|
288
|
+
const info = vi.spyOn(toast, "info");
|
|
289
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} jumpTo={{ messageId: "m3" }} />);
|
|
290
|
+
expect(await screen.findByText("Couldn't load this conversation")).toBeTruthy();
|
|
291
|
+
await act(async () => void (await new Promise((r) => setTimeout(r, 600))));
|
|
292
|
+
expect(info).not.toHaveBeenCalled();
|
|
293
|
+
offline = false;
|
|
294
|
+
fireEvent.click(screen.getByRole("button", { name: "Try again" }));
|
|
295
|
+
await waitFor(() => expect(document.querySelector("#genesis-chat-msg-m3 .ring-2")).not.toBeNull());
|
|
296
|
+
expect(info).not.toHaveBeenCalled();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("keeps a deep link through a failed first page even with a message of yours waiting to send", async () => {
|
|
300
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
301
|
+
const real = client.messages.bind(client);
|
|
302
|
+
let offline = true;
|
|
303
|
+
vi.spyOn(client, "messages").mockImplementation(async (...args) => {
|
|
304
|
+
if (offline) throw new Error("offline");
|
|
305
|
+
return real(...args);
|
|
306
|
+
});
|
|
307
|
+
const info = vi.spyOn(toast, "info");
|
|
308
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} jumpTo={{ messageId: "m3" }} />);
|
|
309
|
+
await screen.findByText("Couldn't load this conversation");
|
|
310
|
+
const release = client.holdNextSend();
|
|
311
|
+
fireEvent.change(await screen.findByTestId("genesis-chat-input"), { target: { value: "Still here" } });
|
|
312
|
+
fireEvent.keyDown(screen.getByTestId("genesis-chat-input"), { key: "Enter" });
|
|
313
|
+
await screen.findByTestId("genesis-chat-pending-sending");
|
|
314
|
+
await act(async () => void (await new Promise((r) => setTimeout(r, 600))));
|
|
315
|
+
expect(info).not.toHaveBeenCalled();
|
|
316
|
+
offline = false;
|
|
317
|
+
release();
|
|
318
|
+
await act(async () => {
|
|
319
|
+
await screen.findByTestId("genesis-chat-thread-page");
|
|
320
|
+
});
|
|
321
|
+
expect(info).not.toHaveBeenCalled();
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("says it's gone when the thread has loaded and no row answers", async () => {
|
|
325
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
326
|
+
const info = vi.spyOn(toast, "info");
|
|
327
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} jumpTo={{ messageId: "nope", seq: 0 }} />);
|
|
328
|
+
await waitFor(() => expect(info).toHaveBeenCalledWith("That message isn't here any more"));
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("finds a deleted message: its tombstone is a row", async () => {
|
|
332
|
+
const client = createMemoryChatClient({ me: ME, conversations: [group()] });
|
|
333
|
+
const info = vi.spyOn(toast, "info");
|
|
334
|
+
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} jumpTo={{ messageId: "m4" }} />);
|
|
335
|
+
await waitFor(() => expect(document.querySelector("#genesis-chat-msg-m4 .ring-2")).not.toBeNull());
|
|
336
|
+
expect(info).not.toHaveBeenCalled();
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
205
340
|
it("says why you can't post when only admins can", async () => {
|
|
206
341
|
const client = createMemoryChatClient({ me: ME, conversations: [group({ canPost: false, settings: { who_posts: "admins" } })] });
|
|
207
342
|
mount(client, <ChatThread conversationId="c1" resolveUser={resolveUser} />);
|
|
@@ -232,8 +367,8 @@ describe("ChatThread", () => {
|
|
|
232
367
|
act(() => client.activity("c1", LEE, "typing"));
|
|
233
368
|
await waitFor(() => expect(screen.getByTestId("genesis-chat-activity").textContent).toContain("Lee is typing…"));
|
|
234
369
|
act(() => void client.receive("c1", LEE, "Coming now"));
|
|
235
|
-
await waitFor(() => expect(screen.getByTestId("genesis-chat-announcer").textContent).
|
|
236
|
-
expect(screen.getByTestId("genesis-chat-announcer").getAttribute("aria-live")).toBe("polite");
|
|
370
|
+
await waitFor(() => expect(screen.getByTestId("genesis-chat-thread-page-messages-announcer").textContent).toContain("Lee: Coming now"));
|
|
371
|
+
expect(screen.getByTestId("genesis-chat-thread-page-messages-announcer").getAttribute("aria-live")).toBe("polite");
|
|
237
372
|
});
|
|
238
373
|
});
|
|
239
374
|
|
|
@@ -279,6 +414,28 @@ describe("ChatInbox", () => {
|
|
|
279
414
|
expect((await screen.findByTestId("genesis-chat-inbox-row-d1")).textContent).toContain("Check-in");
|
|
280
415
|
});
|
|
281
416
|
|
|
417
|
+
it("sits on InboxPage: Home and End move too, and a failed read offers Try again", async () => {
|
|
418
|
+
const client = createMemoryChatClient({
|
|
419
|
+
me: ME,
|
|
420
|
+
conversations: [
|
|
421
|
+
{ id: "d1", members: [{ userId: ME }, { userId: SAM }], messages: [{ senderId: SAM, body: "One", createdAt: at(10) }] },
|
|
422
|
+
{ id: "d2", members: [{ userId: ME }, { userId: LEE }], messages: [{ senderId: LEE, body: "Two", createdAt: at(9) }] },
|
|
423
|
+
],
|
|
424
|
+
});
|
|
425
|
+
mount(client, <ChatInbox resolveUser={resolveUser} onSelect={() => undefined} />);
|
|
426
|
+
const first = await screen.findByTestId("genesis-chat-inbox-row-d1");
|
|
427
|
+
first.focus();
|
|
428
|
+
fireEvent.keyDown(first, { key: "End" });
|
|
429
|
+
expect(document.activeElement).toBe(screen.getByTestId("genesis-chat-inbox-row-d2"));
|
|
430
|
+
cleanup();
|
|
431
|
+
|
|
432
|
+
const failing = createMemoryChatClient({ me: ME, conversations: [] });
|
|
433
|
+
vi.spyOn(failing, "inbox").mockRejectedValue(new Error("offline"));
|
|
434
|
+
mount(failing, <ChatInbox resolveUser={resolveUser} onSelect={() => undefined} />);
|
|
435
|
+
expect(await screen.findByText("Couldn't load your conversations")).toBeTruthy();
|
|
436
|
+
expect(screen.getByRole("button", { name: "Try again" })).toBeTruthy();
|
|
437
|
+
});
|
|
438
|
+
|
|
282
439
|
it("shows an empty state when there are none", async () => {
|
|
283
440
|
const client = createMemoryChatClient({ me: ME, conversations: [] });
|
|
284
441
|
mount(client, <ChatInbox resolveUser={resolveUser} onSelect={() => undefined} />);
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { act, cleanup, fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react";
|
|
4
4
|
import type { ReactNode } from "react";
|
|
5
5
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import { GenesisModulesProvider } from "@marktiderman/genesis-ui-native/components";
|
|
6
7
|
import { ChatComposer } from "../composer";
|
|
7
8
|
import { ChatNativeProvider } from "../context";
|
|
8
9
|
import type { ChatNativeModules } from "../modules";
|
|
@@ -46,6 +47,80 @@ describe("ChatComposer", () => {
|
|
|
46
47
|
await waitFor(() => expect((screen.getByTestId("cmp-input") as HTMLInputElement).value).toBe(""));
|
|
47
48
|
});
|
|
48
49
|
|
|
50
|
+
it("taps once when the app gave haptics to both Genesis and chat", () => {
|
|
51
|
+
const fake = () => ({ impactAsync: vi.fn(async () => undefined), selectionAsync: vi.fn(async () => undefined), notificationAsync: vi.fn(async () => undefined), ImpactFeedbackStyle: { Light: "l", Medium: "m" }, NotificationFeedbackType: { Success: "s", Warning: "w", Error: "e" } });
|
|
52
|
+
const chat = fake();
|
|
53
|
+
const genesis = fake();
|
|
54
|
+
render(
|
|
55
|
+
<GenesisModulesProvider modules={{ haptics: genesis } as never}>
|
|
56
|
+
<ChatNativeProvider modules={{ haptics: chat } as unknown as ChatNativeModules}>
|
|
57
|
+
<Harness />
|
|
58
|
+
</ChatNativeProvider>
|
|
59
|
+
</GenesisModulesProvider>,
|
|
60
|
+
);
|
|
61
|
+
fireEvent.change(screen.getByTestId("cmp-input"), { target: { value: "hello" } });
|
|
62
|
+
fireEvent.click(screen.getByTestId("cmp-send"));
|
|
63
|
+
expect(genesis.impactAsync).toHaveBeenCalledTimes(1);
|
|
64
|
+
expect(chat.impactAsync).not.toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("uses the module the app gave Genesis when it gave chat none, and still taps once", () => {
|
|
68
|
+
const genesis = { impactAsync: vi.fn(async () => undefined), selectionAsync: vi.fn(async () => undefined), notificationAsync: vi.fn(async () => undefined), ImpactFeedbackStyle: { Light: "l", Medium: "m" }, NotificationFeedbackType: { Success: "s", Warning: "w", Error: "e" } };
|
|
69
|
+
render(
|
|
70
|
+
<GenesisModulesProvider modules={{ haptics: genesis } as never}>
|
|
71
|
+
<ChatNativeProvider>
|
|
72
|
+
<Harness />
|
|
73
|
+
</ChatNativeProvider>
|
|
74
|
+
</GenesisModulesProvider>,
|
|
75
|
+
);
|
|
76
|
+
fireEvent.change(screen.getByTestId("cmp-input"), { target: { value: "hello" } });
|
|
77
|
+
fireEvent.click(screen.getByTestId("cmp-send"));
|
|
78
|
+
expect(genesis.impactAsync).toHaveBeenCalledTimes(1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("sits on NativeComposerBar: while access loads, a restored draft can't be sent and the input is read-only", async () => {
|
|
82
|
+
const drafts = memoryDraftStorage();
|
|
83
|
+
await drafts.setItem(draftKey("u1", "c1"), JSON.stringify({ text: "half written" }));
|
|
84
|
+
const onSend = vi.fn();
|
|
85
|
+
render(
|
|
86
|
+
<ChatNativeProvider modules={{ drafts }}>
|
|
87
|
+
<Harness canPost={null as unknown as boolean} onSend={onSend} />
|
|
88
|
+
</ChatNativeProvider>,
|
|
89
|
+
);
|
|
90
|
+
await waitFor(() => expect((screen.getByTestId("cmp-input") as HTMLInputElement).value).toBe("half written"));
|
|
91
|
+
expect(screen.getByTestId("cmp-input").hasAttribute("disabled")).toBe(true);
|
|
92
|
+
expect(screen.getByTestId("cmp-send").getAttribute("aria-disabled")).toBe("true");
|
|
93
|
+
fireEvent.click(screen.getByTestId("cmp-send"));
|
|
94
|
+
expect(onSend).not.toHaveBeenCalled();
|
|
95
|
+
expect((screen.getByTestId("cmp-input") as HTMLInputElement).value).toBe("half written");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("sends attachments with no text through the bar, and the mic gives way to Send", async () => {
|
|
99
|
+
const imagePicker = { launchImageLibraryAsync: vi.fn(async () => ({ canceled: false, assets: [{ uri: "file:///a.jpg", fileName: "a.jpg", mimeType: "image/jpeg", width: 10, height: 10 }] })) };
|
|
100
|
+
const audio = {
|
|
101
|
+
useAudioRecorder: () => ({ prepareToRecordAsync: vi.fn(), record: vi.fn(), stop: vi.fn(), getStatus: vi.fn(), uri: null }),
|
|
102
|
+
useAudioPlayer: vi.fn(),
|
|
103
|
+
useAudioPlayerStatus: vi.fn(),
|
|
104
|
+
RecordingPresets: { HIGH_QUALITY: {} },
|
|
105
|
+
requestRecordingPermissionsAsync: vi.fn(),
|
|
106
|
+
setAudioModeAsync: vi.fn(),
|
|
107
|
+
};
|
|
108
|
+
const onSend = vi.fn();
|
|
109
|
+
render(
|
|
110
|
+
<ChatNativeProvider modules={{ imagePicker, audio } as unknown as ChatNativeModules}>
|
|
111
|
+
<Harness onSend={onSend} />
|
|
112
|
+
</ChatNativeProvider>,
|
|
113
|
+
);
|
|
114
|
+
expect(screen.getByTestId("cmp-mic")).toBeTruthy();
|
|
115
|
+
expect(screen.queryByTestId("cmp-send")).toBeNull();
|
|
116
|
+
fireEvent.click(screen.getByTestId("cmp-attach"));
|
|
117
|
+
await waitFor(() => expect(screen.getByTestId("cmp-attachments")).toBeTruthy());
|
|
118
|
+
expect(screen.queryByTestId("cmp-mic")).toBeNull();
|
|
119
|
+
fireEvent.click(screen.getByTestId("cmp-send"));
|
|
120
|
+
expect(onSend).toHaveBeenCalledTimes(1);
|
|
121
|
+
expect(onSend.mock.calls[0]![0].input.body).toBe("");
|
|
122
|
+
});
|
|
123
|
+
|
|
49
124
|
it("restores the thread's draft", async () => {
|
|
50
125
|
const drafts = memoryDraftStorage();
|
|
51
126
|
await drafts.setItem(draftKey("u1", "c1"), JSON.stringify({ text: "half written" }));
|
|
@@ -132,7 +132,7 @@ describe("ChatThreadScreen scheduled send", () => {
|
|
|
132
132
|
await waitFor(() => expect(input.hasAttribute("disabled")).toBe(false));
|
|
133
133
|
fireEvent.change(input, { target: { value: "see you tomorrow" } });
|
|
134
134
|
const send = screen.getByTestId("t-composer-send");
|
|
135
|
-
expect(send.getAttribute("data-a11y-actions")).toBe("
|
|
135
|
+
expect(send.getAttribute("data-a11y-actions")).toBe("schedule"); // VoiceOver's way to it
|
|
136
136
|
fireEvent.contextMenu(send);
|
|
137
137
|
fireEvent.click(await screen.findByTestId("t-schedule-pick-0"));
|
|
138
138
|
await waitFor(() => expect(called(client, "schedule")).toHaveLength(1));
|