@marktiderman/genesis-cli 1.7.0 → 1.9.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 +26 -4
- package/dist/cli.js +933 -319
- package/dist/cli.js.map +1 -1
- package/dist/{config-CrZbM3jD.d.ts → config-QC_Ewwwp.d.ts} +27 -3
- package/dist/doctor.d.ts +68 -4
- package/dist/doctor.js +654 -87
- package/dist/doctor.js.map +1 -1
- package/dist/expo.d.ts +2 -2
- package/dist/expo.js.map +1 -1
- package/dist/genesis-catalog.json +164 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +666 -101
- package/dist/index.js.map +1 -1
- package/dist/starter/.env.example +3 -0
- package/dist/starter/README.md +7 -0
- package/dist/starter/__tests__/app-config.test.ts +27 -0
- package/dist/starter/__tests__/chat-outboxes.test.ts +64 -0
- package/dist/starter/__tests__/check-in-photos.test.ts +5 -3
- package/dist/starter/__tests__/components/fake-supabase.ts +8 -0
- package/dist/starter/__tests__/components/messages.test.tsx +33 -0
- package/dist/starter/__tests__/components/settings.test.tsx +34 -0
- package/dist/starter/__tests__/components/setup.ts +13 -0
- package/dist/starter/__tests__/components/tabs-feature-off.test.tsx +1 -1
- package/dist/starter/__tests__/components/tabs.test.tsx +2 -2
- package/dist/starter/__tests__/inline-copy.ts +1 -1
- package/dist/starter/__tests__/starter.test.ts +117 -3
- package/dist/starter/app.config.ts +19 -2
- package/dist/starter/genesis.config.json +2 -2
- package/dist/starter/jest.config.js +2 -1
- package/dist/starter/metro.config.js +1 -1
- package/dist/starter/package.json +9 -0
- package/dist/starter/src/app/(app)/(home)/check-in.tsx +4 -38
- package/dist/starter/src/app/(app)/_layout.tsx +25 -3
- package/dist/starter/src/app/(app)/messages/[id].tsx +63 -0
- package/dist/starter/src/app/(app)/messages/_layout.tsx +11 -0
- package/dist/starter/src/app/(app)/messages/check-in.tsx +9 -0
- package/dist/starter/src/app/(app)/messages/index.tsx +38 -0
- package/dist/starter/src/app/(app)/messages/search.tsx +39 -0
- package/dist/starter/src/app/(app)/notifications/index.tsx +7 -13
- package/dist/starter/src/app/(app)/settings/availability.tsx +16 -0
- package/dist/starter/src/app/(app)/settings/index.tsx +8 -3
- package/dist/starter/src/app/(app)/settings/notifications.tsx +6 -3
- package/dist/starter/src/copy.ts +19 -0
- package/dist/starter/src/lib/chat-outboxes.ts +50 -0
- package/dist/starter/src/lib/chat-video.ts +57 -0
- package/dist/starter/src/lib/chat.tsx +319 -0
- package/dist/starter/src/lib/check-in-screen.tsx +43 -0
- package/dist/starter/src/lib/define-starter.ts +9 -0
- package/dist/starter/src/lib/env.ts +4 -0
- package/dist/starter/src/lib/links.ts +2 -0
- package/dist/starter/src/lib/notifications.ts +30 -12
- package/dist/starter/src/lib/push.ts +21 -5
- package/dist/starter/starter.config.ts +1 -0
- package/dist/starter/supabase/config.toml +5 -0
- package/dist/starter/supabase/migrations/20261025000000_genesis_chat.sql +1836 -0
- package/dist/starter/supabase/migrations/20261025000050_genesis_chat_coaching.sql +934 -0
- package/dist/starter/supabase/migrations/20261025000060_genesis_chat_search_threads.sql +85 -0
- package/dist/starter/supabase/migrations/20261025000100_genesis_notifications.sql +2057 -0
- package/dist/starter/supabase/migrations/20261026000000_chat_people.sql +57 -0
- package/dist/starter/supabase/tests/database/chat_people.test.sql +112 -0
- package/dist/starter/supabase/tests/database/genesis_chat.test.sql +254 -0
- package/dist/starter/supabase/tests/database/genesis_chat_coaching.test.sql +269 -0
- package/dist/starter/supabase/tests/database/genesis_chat_search_threads.test.sql +50 -0
- package/dist/starter/supabase/tests/database/genesis_notifications.test.sql +473 -0
- package/dist/vite.js +651 -86
- package/dist/vite.js.map +1 -1
- package/package.json +3 -3
- package/dist/starter/supabase/tests/database/genesis_push.test.sql +0 -407
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- Names and avatars for chat (`ChatProvider loadPeople`, through `loadChatPeople` in
|
|
3
|
+
-- src/lib/chat.tsx). genesis-chat stores user ids only, and the starter has no profiles table:
|
|
4
|
+
-- a login's name and picture are its auth metadata (`name` or `full_name`, `avatar_url` or
|
|
5
|
+
-- `picture`), set at sign-up or by Sign in with Apple.
|
|
6
|
+
--
|
|
7
|
+
-- auth.users isn't readable through the API, so this function reads it for the caller, and
|
|
8
|
+
-- returns only the name and avatar (never the email, phone or the rest of the metadata), only
|
|
9
|
+
-- for the people the caller can already see in chat:
|
|
10
|
+
-- - the caller;
|
|
11
|
+
-- - current members of a conversation the caller can read (genesis_chat.can_view: a member,
|
|
12
|
+
-- or an observer the kind's check lets in): the people the members policy shows, so not
|
|
13
|
+
-- observer rows and not those who left;
|
|
14
|
+
-- - the authors of messages the caller can read there (visibility 'everyone'), so an old
|
|
15
|
+
-- message keeps its author's name after they leave.
|
|
16
|
+
-- Anyone else is left out, so asking about a stranger's id returns no row. At most 200 ids.
|
|
17
|
+
-- Each check starts from the asked-about person (members_user_idx, messages_client_unique)
|
|
18
|
+
-- and calls can_view once per conversation of theirs.
|
|
19
|
+
--
|
|
20
|
+
-- An app with its own profiles drops this and passes its own `loadPeople`.
|
|
21
|
+
|
|
22
|
+
create or replace function public.chat_people(p_user_ids uuid[])
|
|
23
|
+
returns table (user_id uuid, name text, avatar_url text)
|
|
24
|
+
language sql
|
|
25
|
+
stable
|
|
26
|
+
security definer
|
|
27
|
+
set search_path = ''
|
|
28
|
+
as $$
|
|
29
|
+
select
|
|
30
|
+
u.id,
|
|
31
|
+
nullif(btrim(coalesce(u.raw_user_meta_data ->> 'name', u.raw_user_meta_data ->> 'full_name')), ''),
|
|
32
|
+
nullif(btrim(coalesce(u.raw_user_meta_data ->> 'avatar_url', u.raw_user_meta_data ->> 'picture')), '')
|
|
33
|
+
from auth.users u
|
|
34
|
+
where auth.uid() is not null
|
|
35
|
+
and u.id = any (p_user_ids[1:200])
|
|
36
|
+
and (
|
|
37
|
+
u.id = auth.uid()
|
|
38
|
+
or exists (
|
|
39
|
+
select 1 from genesis_chat.members theirs
|
|
40
|
+
where theirs.user_id = u.id and theirs.left_at is null and theirs.role <> 'observer'
|
|
41
|
+
and genesis_chat.can_view(theirs.conversation_id)
|
|
42
|
+
)
|
|
43
|
+
or exists (
|
|
44
|
+
select 1 from (
|
|
45
|
+
select distinct m.conversation_id from genesis_chat.messages m
|
|
46
|
+
where m.sender_id = u.id and m.visibility = 'everyone' and m.deleted_at is null
|
|
47
|
+
) authored
|
|
48
|
+
where genesis_chat.can_view(authored.conversation_id)
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
$$;
|
|
52
|
+
|
|
53
|
+
revoke execute on function public.chat_people(uuid[]) from public, anon;
|
|
54
|
+
grant execute on function public.chat_people(uuid[]) to authenticated, service_role;
|
|
55
|
+
|
|
56
|
+
comment on function public.chat_people(uuid[]) is
|
|
57
|
+
'Name and avatar (auth metadata) for the caller and people in conversations the caller can read. Genesis starter chat.';
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- pgTAP test for 20261026000000_chat_people.sql: names and avatars only, only for people who
|
|
3
|
+
-- share a conversation with the caller. Writes only its own users and conversation; rolls back.
|
|
4
|
+
begin;
|
|
5
|
+
create extension if not exists pgtap with schema extensions;
|
|
6
|
+
select plan(13);
|
|
7
|
+
|
|
8
|
+
insert into auth.users (id, aud, role, email, instance_id, raw_user_meta_data)
|
|
9
|
+
values
|
|
10
|
+
('00000000-0000-4000-8000-0000000c9a01', 'authenticated', 'authenticated', 'chat-people-a@example.test', '00000000-0000-0000-0000-000000000000',
|
|
11
|
+
'{"name": "Avery Stone", "avatar_url": "https://example.test/a.png", "phone_secret": "x"}'),
|
|
12
|
+
('00000000-0000-4000-8000-0000000c9a02', 'authenticated', 'authenticated', 'chat-people-b@example.test', '00000000-0000-0000-0000-000000000000',
|
|
13
|
+
'{"full_name": "Blair Quinn", "picture": "https://example.test/b.png"}'),
|
|
14
|
+
('00000000-0000-4000-8000-0000000c9a03', 'authenticated', 'authenticated', 'chat-people-c@example.test', '00000000-0000-0000-0000-000000000000',
|
|
15
|
+
'{"name": "Casey Stranger"}'),
|
|
16
|
+
('00000000-0000-4000-8000-0000000c9a04', 'authenticated', 'authenticated', 'chat-people-d@example.test', '00000000-0000-0000-0000-000000000000',
|
|
17
|
+
'{"name": " "}'),
|
|
18
|
+
('00000000-0000-4000-8000-0000000c9a05', 'authenticated', 'authenticated', 'chat-people-e@example.test', '00000000-0000-0000-0000-000000000000',
|
|
19
|
+
'{"name": "Emery Left"}'),
|
|
20
|
+
('00000000-0000-4000-8000-0000000c9a06', 'authenticated', 'authenticated', 'chat-people-f@example.test', '00000000-0000-0000-0000-000000000000',
|
|
21
|
+
'{"name": "Finley Observer"}'),
|
|
22
|
+
('00000000-0000-4000-8000-0000000c9a07', 'authenticated', 'authenticated', 'chat-people-g@example.test', '00000000-0000-0000-0000-000000000000',
|
|
23
|
+
'{"name": "Gray Quiet"}');
|
|
24
|
+
|
|
25
|
+
create function pg_temp.sign_in(id uuid) returns void language plpgsql as $$
|
|
26
|
+
begin
|
|
27
|
+
perform set_config('request.jwt.claims', json_build_object('sub', id, 'role', 'authenticated')::text, true);
|
|
28
|
+
perform set_config('request.jwt.claim.sub', id::text, true);
|
|
29
|
+
perform set_config('role', 'authenticated', true);
|
|
30
|
+
end $$;
|
|
31
|
+
grant execute on function pg_temp.sign_in(uuid) to authenticated;
|
|
32
|
+
|
|
33
|
+
-- a, b and d in one group; e and g left it (e after posting, g without); f has an observer row;
|
|
34
|
+
-- c is in nothing with them.
|
|
35
|
+
insert into genesis_chat.conversations (id, kind) values ('00000000-0000-4000-8000-0000000c9ac1', 'group');
|
|
36
|
+
insert into genesis_chat.members (conversation_id, user_id, role, left_at) values
|
|
37
|
+
('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a01', 'owner', null),
|
|
38
|
+
('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a02', 'member', null),
|
|
39
|
+
('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a04', 'member', null),
|
|
40
|
+
('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a05', 'member', now()),
|
|
41
|
+
('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a06', 'observer', null),
|
|
42
|
+
('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a07', 'member', now());
|
|
43
|
+
insert into genesis_chat.messages (conversation_id, seq, sender_id, sender_role, body) values
|
|
44
|
+
('00000000-0000-4000-8000-0000000c9ac1', 1, '00000000-0000-4000-8000-0000000c9a05', 'member', 'before I go');
|
|
45
|
+
|
|
46
|
+
create temp table people_ids (id uuid);
|
|
47
|
+
insert into people_ids values
|
|
48
|
+
('00000000-0000-4000-8000-0000000c9a01'), ('00000000-0000-4000-8000-0000000c9a02'), ('00000000-0000-4000-8000-0000000c9a03'),
|
|
49
|
+
('00000000-0000-4000-8000-0000000c9a04'), ('00000000-0000-4000-8000-0000000c9a05'), ('00000000-0000-4000-8000-0000000c9a06'),
|
|
50
|
+
('00000000-0000-4000-8000-0000000c9a07');
|
|
51
|
+
grant select on people_ids to authenticated, anon;
|
|
52
|
+
|
|
53
|
+
select pg_temp.sign_in('00000000-0000-4000-8000-0000000c9a01');
|
|
54
|
+
|
|
55
|
+
select results_eq(
|
|
56
|
+
$$ select user_id, name, avatar_url from public.chat_people((select array_agg(id) from people_ids)) order by user_id $$,
|
|
57
|
+
$$ values
|
|
58
|
+
('00000000-0000-4000-8000-0000000c9a01'::uuid, 'Avery Stone'::text, 'https://example.test/a.png'::text),
|
|
59
|
+
('00000000-0000-4000-8000-0000000c9a02'::uuid, 'Blair Quinn', 'https://example.test/b.png'),
|
|
60
|
+
('00000000-0000-4000-8000-0000000c9a04'::uuid, null, null),
|
|
61
|
+
('00000000-0000-4000-8000-0000000c9a05'::uuid, 'Emery Left', null) $$,
|
|
62
|
+
'a member sees the names and avatars of the people in their conversations, their own, and an author who left'
|
|
63
|
+
);
|
|
64
|
+
select is((select count(*) from public.chat_people(array['00000000-0000-4000-8000-0000000c9a06'::uuid])), 0::bigint,
|
|
65
|
+
'not an observer row (the members policy hides it; it grants nothing)');
|
|
66
|
+
select is((select count(*) from public.chat_people(array['00000000-0000-4000-8000-0000000c9a07'::uuid])), 0::bigint,
|
|
67
|
+
'not someone who left without posting');
|
|
68
|
+
select is((select count(*) from public.chat_people(array['00000000-0000-4000-8000-0000000c9a03'::uuid])), 0::bigint,
|
|
69
|
+
'no row for someone the caller shares no conversation with');
|
|
70
|
+
select is((select name from public.chat_people(array['00000000-0000-4000-8000-0000000c9a02'::uuid])), 'Blair Quinn',
|
|
71
|
+
'full_name when there is no name (Sign in with Apple)');
|
|
72
|
+
select is((select name from public.chat_people(array['00000000-0000-4000-8000-0000000c9a04'::uuid])), null,
|
|
73
|
+
'a blank name is no name');
|
|
74
|
+
select is(
|
|
75
|
+
(select array_agg(p.parameter_name::text order by p.ordinal_position) from information_schema.parameters p
|
|
76
|
+
join information_schema.routines r on r.specific_name = p.specific_name
|
|
77
|
+
where r.routine_schema = 'public' and r.routine_name = 'chat_people' and p.parameter_mode = 'OUT'),
|
|
78
|
+
array['user_id', 'name', 'avatar_url'],
|
|
79
|
+
'returns only the id, name and avatar: never the email or the rest of the metadata'
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
select pg_temp.sign_in('00000000-0000-4000-8000-0000000c9a03');
|
|
83
|
+
select results_eq(
|
|
84
|
+
$$ select user_id from public.chat_people((select array_agg(id) from people_ids)) $$,
|
|
85
|
+
$$ values ('00000000-0000-4000-8000-0000000c9a03'::uuid) $$,
|
|
86
|
+
'someone in no conversation with them sees only themself'
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
-- Someone who left still sees nobody they no longer share a conversation with.
|
|
90
|
+
select pg_temp.sign_in('00000000-0000-4000-8000-0000000c9a05');
|
|
91
|
+
select results_eq(
|
|
92
|
+
$$ select user_id from public.chat_people((select array_agg(id) from people_ids)) $$,
|
|
93
|
+
$$ values ('00000000-0000-4000-8000-0000000c9a05'::uuid) $$,
|
|
94
|
+
'a member who left reads no one from the conversation they left'
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
select pg_temp.sign_in('00000000-0000-4000-8000-0000000c9a01');
|
|
98
|
+
select is((select count(*) from public.chat_people(array_cat(array_fill('00000000-0000-4000-8000-0000000c9a03'::uuid, array[200]), array['00000000-0000-4000-8000-0000000c9a02'::uuid]))), 0::bigint,
|
|
99
|
+
'only the first 200 ids are read');
|
|
100
|
+
|
|
101
|
+
-- A signed-in role with no user (a NULL auth.uid()) reads no one.
|
|
102
|
+
select set_config('request.jwt.claims', '{"role":"authenticated"}', true);
|
|
103
|
+
select set_config('request.jwt.claim.sub', '', true);
|
|
104
|
+
select is((select count(*) from public.chat_people((select array_agg(id) from people_ids))), 0::bigint,
|
|
105
|
+
'no user (NULL auth.uid()): no rows');
|
|
106
|
+
|
|
107
|
+
reset role;
|
|
108
|
+
select ok(not has_function_privilege('anon', 'public.chat_people(uuid[])', 'execute'), 'anon may not call it');
|
|
109
|
+
select ok(has_function_privilege('authenticated', 'public.chat_people(uuid[])', 'execute'), 'a signed-in person may');
|
|
110
|
+
|
|
111
|
+
select * from finish();
|
|
112
|
+
rollback;
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- pgTAP test for 0001_genesis_chat.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 in nobody's conversation; d4 is staff of the
|
|
7
|
+
-- test's "ticket" kind (an observer who may post); e5 leaves a group.
|
|
8
|
+
begin;
|
|
9
|
+
create extension if not exists pgtap with schema extensions;
|
|
10
|
+
select plan(79);
|
|
11
|
+
|
|
12
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
13
|
+
select ('00000000-0000-4000-8000-00000000c0' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
14
|
+
from unnest(array['a1', 'b2', 'c3', 'd4', 'e5']) n;
|
|
15
|
+
|
|
16
|
+
create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c0' || n)::uuid $$;
|
|
17
|
+
create function pg_temp.sign_in(n text) returns void language plpgsql as $$
|
|
18
|
+
begin
|
|
19
|
+
perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
|
|
20
|
+
perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
|
|
21
|
+
perform set_config('role', 'authenticated', true);
|
|
22
|
+
end $$;
|
|
23
|
+
create function pg_temp.as_anon() returns void language plpgsql as $$
|
|
24
|
+
begin
|
|
25
|
+
perform set_config('request.jwt.claims', '{"role":"anon"}', true);
|
|
26
|
+
perform set_config('request.jwt.claim.sub', '', true);
|
|
27
|
+
perform set_config('role', 'anon', true);
|
|
28
|
+
end $$;
|
|
29
|
+
create function pg_temp.as_server() returns void language plpgsql as $$
|
|
30
|
+
begin
|
|
31
|
+
perform set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
|
32
|
+
perform set_config('request.jwt.claim.sub', '', true);
|
|
33
|
+
perform set_config('role', 'service_role', true);
|
|
34
|
+
end $$;
|
|
35
|
+
grant execute on function pg_temp.person(text), pg_temp.sign_in(text), pg_temp.as_anon(), pg_temp.as_server() to anon, authenticated, service_role;
|
|
36
|
+
|
|
37
|
+
create temp table t (k text primary key, v uuid);
|
|
38
|
+
grant select, insert, update on t to authenticated, service_role;
|
|
39
|
+
create function pg_temp.v(k_ text) returns uuid language sql stable as $$ select v from t where k = k_ $$;
|
|
40
|
+
grant execute on function pg_temp.v(text) to authenticated, service_role;
|
|
41
|
+
|
|
42
|
+
-- The app's rules: anyone may message a1, b2 or x5 (not c3); staff (d4) observe tickets.
|
|
43
|
+
create function public.genesis_chat_test_may_start(member_ids uuid[], subject_type text, subject_id text, user_id uuid)
|
|
44
|
+
returns boolean language sql stable set search_path = '' as $$
|
|
45
|
+
select not (('00000000-0000-4000-8000-00000000c0c3')::uuid = any (member_ids))
|
|
46
|
+
$$;
|
|
47
|
+
create function public.genesis_chat_test_staff(conversation_id uuid, user_id uuid)
|
|
48
|
+
returns boolean language sql stable set search_path = '' as $$
|
|
49
|
+
select user_id = '00000000-0000-4000-8000-00000000c0d4'::uuid
|
|
50
|
+
$$;
|
|
51
|
+
select genesis_chat.register_kind('direct', 'Direct message', p_max_members => 2,
|
|
52
|
+
p_start_check => 'public.genesis_chat_test_may_start(uuid[], text, text, uuid)');
|
|
53
|
+
select genesis_chat.register_kind('group', 'Group', p_membership => 'admins',
|
|
54
|
+
p_start_check => 'public.genesis_chat_test_may_start(uuid[], text, text, uuid)');
|
|
55
|
+
select genesis_chat.register_kind('ticket', 'Support ticket', p_threads => false,
|
|
56
|
+
p_observer_check => 'public.genesis_chat_test_staff(uuid, uuid)', p_observers_post => true);
|
|
57
|
+
|
|
58
|
+
select throws_ok($$ select genesis_chat.register_kind('bad', 'Bad', p_observer_check => 'public.genesis_chat_test_may_start(uuid[], text, text, uuid)') $$,
|
|
59
|
+
'22023', null, 'an observer check must take (uuid, uuid)');
|
|
60
|
+
|
|
61
|
+
-- Direct conversations -----------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
select pg_temp.sign_in('a1');
|
|
64
|
+
insert into t select 'dm', genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]);
|
|
65
|
+
select is(genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]), pg_temp.v('dm'), 'a direct conversation is found again, not made twice');
|
|
66
|
+
select pg_temp.sign_in('b2');
|
|
67
|
+
select is(genesis_chat.start_conversation('direct', array[pg_temp.person('a1')]), pg_temp.v('dm'), 'from either side');
|
|
68
|
+
select pg_temp.sign_in('a1');
|
|
69
|
+
select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('c3')]) $$, '42501', null,
|
|
70
|
+
'the start check decides who you may message');
|
|
71
|
+
select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('b2'), pg_temp.person('e5')]) $$, '22023', null,
|
|
72
|
+
'a direct conversation has two people');
|
|
73
|
+
select throws_ok($$ select genesis_chat.start_conversation('ticket', array[pg_temp.person('b2')]) $$, '42501', null,
|
|
74
|
+
'a kind without a start check is started by the server only');
|
|
75
|
+
|
|
76
|
+
-- Sending ---------------------------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
insert into t values ('c1', gen_random_uuid());
|
|
79
|
+
select is((genesis_chat.send_message(pg_temp.v('dm'), pg_temp.v('c1'), 'Hi **b2**')).seq, 1::bigint, 'the first message is number 1');
|
|
80
|
+
insert into t select 'm1', id from genesis_chat.messages where client_id = pg_temp.v('c1');
|
|
81
|
+
select is((genesis_chat.send_message(pg_temp.v('dm'), pg_temp.v('c1'), 'Hi **b2**')).id, pg_temp.v('m1'), 'sending the same client_id again returns the first message');
|
|
82
|
+
select is((select count(*)::integer from genesis_chat.messages where conversation_id = pg_temp.v('dm')), 1, 'and posts nothing new');
|
|
83
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), ' ') $$, '22023', null,
|
|
84
|
+
'a message needs text or an attachment');
|
|
85
|
+
select is((genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), 'Second', p_reply_to_id => pg_temp.v('m1'),
|
|
86
|
+
p_mentions => array[pg_temp.person('b2'), pg_temp.person('c3')])).mentions,
|
|
87
|
+
array[pg_temp.person('b2')], 'mentions keep only members');
|
|
88
|
+
select is((select last_read_seq from genesis_chat.members where conversation_id = pg_temp.v('dm') and user_id = pg_temp.person('a1')),
|
|
89
|
+
2::bigint, 'the sender has read their own messages');
|
|
90
|
+
select ok(exists (select 1 from genesis_chat.inbox() i where i.conversation_id = pg_temp.v('dm') and i.unread = 0
|
|
91
|
+
and i.other_user_id = pg_temp.person('b2')), 'the sender''s inbox shows nothing unread and the other person');
|
|
92
|
+
|
|
93
|
+
select pg_temp.sign_in('b2');
|
|
94
|
+
select is((select unread from genesis_chat.inbox() where conversation_id = pg_temp.v('dm')), 2, 'the other person has two unread');
|
|
95
|
+
select ok((select mentioned from genesis_chat.inbox() where conversation_id = pg_temp.v('dm')), 'and was mentioned');
|
|
96
|
+
select is(genesis_chat.mark_read(pg_temp.v('dm'), 1), 1::bigint, 'reading up to the first');
|
|
97
|
+
select is(genesis_chat.mark_read(pg_temp.v('dm'), 0), 1::bigint, 'read state never moves back');
|
|
98
|
+
select is(genesis_chat.mark_read(pg_temp.v('dm'), 99), 2::bigint, 'nor past the latest');
|
|
99
|
+
select is(genesis_chat.unread_total(), 0, 'nothing waits for them');
|
|
100
|
+
|
|
101
|
+
-- Who reads what -------------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
select pg_temp.sign_in('c3');
|
|
104
|
+
select is((select count(*)::integer from genesis_chat.messages where conversation_id = pg_temp.v('dm')), 0, 'someone outside reads no messages');
|
|
105
|
+
select is((select count(*)::integer from genesis_chat.conversations where id = pg_temp.v('dm')), 0, 'nor the conversation');
|
|
106
|
+
select is((select count(*)::integer from genesis_chat.members where conversation_id = pg_temp.v('dm')), 0, 'nor who is in it');
|
|
107
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), 'Let me in') $$, '42501', null, 'and can''t post');
|
|
108
|
+
select throws_ok($$ select genesis_chat.mark_read(pg_temp.v('dm')) $$, '42501', null, 'or mark it read');
|
|
109
|
+
select throws_ok($$ select genesis_chat.react(pg_temp.v('m1'), '👍') $$, '42501', null, 'or react');
|
|
110
|
+
select ok(not genesis_chat.can_join_topic('genesis-chat:conv:' || pg_temp.v('dm')), 'or join its live topic');
|
|
111
|
+
select ok(not genesis_chat.can_join_topic('genesis-chat:user:' || pg_temp.person('a1')), 'or anyone else''s inbox topic');
|
|
112
|
+
select ok(genesis_chat.can_join_topic('genesis-chat:user:' || pg_temp.person('c3')), 'but joins their own');
|
|
113
|
+
select ok(not genesis_chat.can_join_topic('genesis-chat:user:' || pg_temp.person('c3'), true), 'without writing to it');
|
|
114
|
+
select pg_temp.sign_in('a1');
|
|
115
|
+
select ok(genesis_chat.can_join_topic('genesis-chat:conv:' || pg_temp.v('dm'), true), 'a member signals typing on the conversation''s topic');
|
|
116
|
+
|
|
117
|
+
select pg_temp.as_anon();
|
|
118
|
+
select throws_ok($$ select count(*) from genesis_chat.messages $$, '42501', null, 'anon reaches nothing');
|
|
119
|
+
select lives_ok($$ select count(*) from storage.objects where bucket_id <> 'genesis-chat' $$, 'anon still reads other buckets: chat''s storage policies don''t apply to it');
|
|
120
|
+
|
|
121
|
+
-- Someone outside never passes a role check (my_role is null for them).
|
|
122
|
+
reset role;
|
|
123
|
+
insert into t select 'm2', id from genesis_chat.messages where conversation_id = pg_temp.v('dm') and seq = 2;
|
|
124
|
+
select pg_temp.sign_in('c3');
|
|
125
|
+
select throws_ok($$ select genesis_chat.update_conversation(pg_temp.v('dm'), 'Mine now') $$, '42501', null, 'someone outside can''t rename a conversation');
|
|
126
|
+
select throws_ok($$ select genesis_chat.set_status(pg_temp.v('dm'), 'solved') $$, '42501', null, 'or set its status');
|
|
127
|
+
select throws_ok($$ select genesis_chat.delete_message(pg_temp.v('m2')) $$,
|
|
128
|
+
'42501', null, 'or delete its messages');
|
|
129
|
+
select throws_ok($$ select genesis_chat.pin_message(pg_temp.v('m2')) $$,
|
|
130
|
+
'42501', null, 'or pin them');
|
|
131
|
+
reset role;
|
|
132
|
+
select is((select title from genesis_chat.conversations where id = pg_temp.v('dm')), null, 'and nothing changed');
|
|
133
|
+
|
|
134
|
+
-- Edits, reactions, deletes -----------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
select pg_temp.sign_in('a1');
|
|
137
|
+
select is((genesis_chat.edit_message(pg_temp.v('m1'), 'Hi again')).body, 'Hi again', 'the sender edits their message');
|
|
138
|
+
select pg_temp.sign_in('b2');
|
|
139
|
+
select throws_ok($$ select genesis_chat.edit_message(pg_temp.v('m1'), 'Not mine') $$, '42501', null, 'nobody else does');
|
|
140
|
+
select lives_ok($$ select genesis_chat.react(pg_temp.v('m1'), '🔥') $$, 'a member reacts');
|
|
141
|
+
select is((select count(*)::integer from genesis_chat.reactions where message_id = pg_temp.v('m1')), 1, 'and both see it');
|
|
142
|
+
select throws_ok($$ select genesis_chat.delete_message(pg_temp.v('m1')) $$, '42501', null, 'in a direct conversation, only the sender deletes');
|
|
143
|
+
select pg_temp.sign_in('a1');
|
|
144
|
+
select lives_ok($$ select genesis_chat.delete_message(pg_temp.v('m1')) $$, 'the sender deletes it');
|
|
145
|
+
select ok((select deleted_at is not null and body = '' from genesis_chat.messages where id = pg_temp.v('m1')),
|
|
146
|
+
'a placeholder stays, without its text');
|
|
147
|
+
select is((select count(*)::integer from genesis_chat.reactions where message_id = pg_temp.v('m1')), 0, 'and its reactions go');
|
|
148
|
+
|
|
149
|
+
-- Files ------------------------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
select ok(genesis_chat.can_upload(pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/photo-1.jpg'), 'a member uploads into their folder');
|
|
152
|
+
select ok(not genesis_chat.can_upload(pg_temp.v('dm') || '/' || pg_temp.person('b2') || '/photo-1.jpg'), 'not someone else''s');
|
|
153
|
+
select ok(not genesis_chat.can_upload(pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/page.svg'), 'and never an SVG');
|
|
154
|
+
select pg_temp.sign_in('c3');
|
|
155
|
+
select ok(not genesis_chat.can_upload(pg_temp.v('dm') || '/' || pg_temp.person('c3') || '/photo-1.jpg'), 'someone outside uploads nothing');
|
|
156
|
+
reset role;
|
|
157
|
+
insert into storage.objects (bucket_id, name, owner_id) values
|
|
158
|
+
('genesis-chat', pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/voice-1.m4a', pg_temp.person('a1')::text),
|
|
159
|
+
('genesis-chat', pg_temp.v('dm') || '/' || pg_temp.person('b2') || '/theirs.jpg', pg_temp.person('b2')::text);
|
|
160
|
+
|
|
161
|
+
select pg_temp.sign_in('a1');
|
|
162
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), '',
|
|
163
|
+
jsonb_build_array(jsonb_build_object('kind', 'image', 'path', pg_temp.v('dm') || '/' || pg_temp.person('b2') || '/theirs.jpg'))) $$,
|
|
164
|
+
'22023', null, 'a message carries only files its sender uploaded');
|
|
165
|
+
insert into t select 'voice', (genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), '',
|
|
166
|
+
jsonb_build_array(jsonb_build_object('kind', 'voice', 'path', pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/voice-1.m4a',
|
|
167
|
+
'mime', 'audio/mp4', 'duration_ms', 4200, 'waveform', jsonb_build_array(10, 80, 200, -5))))).id;
|
|
168
|
+
select is((select waveform from genesis_chat.attachments where message_id = pg_temp.v('voice')), array[10, 80, 100, 0]::smallint[],
|
|
169
|
+
'a voice note keeps its waveform, clamped to 0–100');
|
|
170
|
+
select is((select transcript_status from genesis_chat.attachments where message_id = pg_temp.v('voice')), 'pending', 'and waits for a transcript');
|
|
171
|
+
select pg_temp.sign_in('b2');
|
|
172
|
+
select ok(genesis_chat.can_read_object(pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/voice-1.m4a', pg_temp.person('a1')::text, now()),
|
|
173
|
+
'the other member reads the file once it is sent');
|
|
174
|
+
select pg_temp.sign_in('c3');
|
|
175
|
+
select ok(not genesis_chat.can_read_object(pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/voice-1.m4a', pg_temp.person('a1')::text, now()),
|
|
176
|
+
'someone outside never does');
|
|
177
|
+
|
|
178
|
+
-- Groups -------------------------------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
select pg_temp.sign_in('a1');
|
|
181
|
+
insert into t select 'grp', genesis_chat.start_conversation('group', array[pg_temp.person('b2'), pg_temp.person('e5')], 'Cohort');
|
|
182
|
+
select is(genesis_chat.my_role(pg_temp.v('grp')), 'owner', 'whoever starts a group owns it');
|
|
183
|
+
select genesis_chat.update_conversation(pg_temp.v('grp'), p_settings => '{"who_posts": "admins"}');
|
|
184
|
+
select pg_temp.sign_in('b2');
|
|
185
|
+
select ok(not genesis_chat.can_post(pg_temp.v('grp')), 'a one-way group: members read but don''t post');
|
|
186
|
+
select pg_temp.sign_in('e5');
|
|
187
|
+
select lives_ok($$ select genesis_chat.remove_member(pg_temp.v('grp')) $$, 'a member leaves a group');
|
|
188
|
+
select ok(not genesis_chat.can_view(pg_temp.v('grp')), 'and no longer reads it');
|
|
189
|
+
select pg_temp.sign_in('a1');
|
|
190
|
+
select throws_ok($$ select genesis_chat.add_members(pg_temp.v('grp'), array[pg_temp.person('c3')]) $$, '42501', null,
|
|
191
|
+
'the start check also decides who may be added');
|
|
192
|
+
|
|
193
|
+
-- Tickets: observers and internal notes ----------------------------------------------------
|
|
194
|
+
|
|
195
|
+
select pg_temp.as_server();
|
|
196
|
+
insert into t select 'tkt', genesis_chat.ensure_conversation('ticket', 'report', 'r-1', 'Login trouble');
|
|
197
|
+
select is(genesis_chat.ensure_conversation('ticket', 'report', 'r-1'), pg_temp.v('tkt'), 'the server finds a record''s conversation again');
|
|
198
|
+
select is(genesis_chat.sync_members(pg_temp.v('tkt'), array[pg_temp.person('b2')]), 1, 'and sets its members');
|
|
199
|
+
select pg_temp.sign_in('b2');
|
|
200
|
+
select lives_ok($$ select genesis_chat.send_message(pg_temp.v('tkt'), gen_random_uuid(), 'I can''t sign in') $$, 'the person writes in');
|
|
201
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('tkt'), gen_random_uuid(), 'note', p_visibility => 'observers') $$, '42501', null,
|
|
202
|
+
'only observers write internal notes');
|
|
203
|
+
select pg_temp.sign_in('d4');
|
|
204
|
+
select is(genesis_chat.my_role(pg_temp.v('tkt')), 'observer', 'staff observe the ticket without being in it');
|
|
205
|
+
select lives_ok($$ select genesis_chat.send_message(pg_temp.v('tkt'), gen_random_uuid(), 'Looking now') $$, 'and reply');
|
|
206
|
+
select lives_ok($$ select genesis_chat.send_message(pg_temp.v('tkt'), gen_random_uuid(), 'Their session expired', p_visibility => 'observers') $$,
|
|
207
|
+
'and leave an internal note');
|
|
208
|
+
select is((select count(*)::integer from genesis_chat.observed('ticket')), 1, 'the ticket is in staff''s queue');
|
|
209
|
+
select pg_temp.sign_in('b2');
|
|
210
|
+
select is((select count(*)::integer from genesis_chat.messages where conversation_id = pg_temp.v('tkt')), 2, 'the person never sees the note');
|
|
211
|
+
select is((select unread from genesis_chat.inbox() where conversation_id = pg_temp.v('tkt')), 1, 'nor counts it as unread');
|
|
212
|
+
select pg_temp.sign_in('d4');
|
|
213
|
+
select ok(not genesis_chat.can_view(pg_temp.v('dm')), 'observing one kind opens nothing else');
|
|
214
|
+
|
|
215
|
+
-- The runner ------------------------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
select pg_temp.as_server();
|
|
218
|
+
select ok(exists (select 1 from genesis_chat.push_targets(
|
|
219
|
+
(select id from genesis_chat.messages where conversation_id = pg_temp.v('dm') and seq = 2)) p
|
|
220
|
+
where p.user_id = pg_temp.person('b2') and p.mentioned), 'push goes to the other member, marked as mentioned');
|
|
221
|
+
select ok(exists (select 1 from genesis_chat.effects where key = 'transcribe:' ||
|
|
222
|
+
(select id from genesis_chat.attachments where message_id = pg_temp.v('voice'))), 'a voice note queues its transcript');
|
|
223
|
+
select pg_temp.sign_in('a1');
|
|
224
|
+
select genesis_chat.delete_message(pg_temp.v('voice'));
|
|
225
|
+
select pg_temp.as_server();
|
|
226
|
+
select ok(exists (select 1 from genesis_chat.effects where kind = 'storage.remove'
|
|
227
|
+
and payload ->> 'path' = pg_temp.v('dm') || '/' || pg_temp.person('a1') || '/voice-1.m4a'), 'a deleted file is queued for removal');
|
|
228
|
+
select pg_temp.sign_in('a1');
|
|
229
|
+
select throws_ok($$ select * from genesis_chat.claim_effects() $$, '42501', null, 'only the server works the outbox');
|
|
230
|
+
|
|
231
|
+
-- Hosted video and the badge ------------------------------------------------------------------
|
|
232
|
+
|
|
233
|
+
select pg_temp.sign_in('a1');
|
|
234
|
+
insert into t select 'vid', (genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), '',
|
|
235
|
+
jsonb_build_array(jsonb_build_object('kind', 'video', 'provider', 'mux', 'provider_id', 'upload-abc', 'status', 'ready')))).id;
|
|
236
|
+
select is((select status from genesis_chat.attachments where message_id = pg_temp.v('vid')), 'pending',
|
|
237
|
+
'a hosted video starts pending whatever the sender says; only its webhook makes it ready');
|
|
238
|
+
select pg_temp.sign_in('b2');
|
|
239
|
+
select throws_ok($$ select genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), '',
|
|
240
|
+
jsonb_build_array(jsonb_build_object('kind', 'video', 'provider', 'mux', 'provider_id', 'upload-abc'))) $$,
|
|
241
|
+
'23505', null, 'nobody can attach someone else''s hosted video by copying its id');
|
|
242
|
+
select genesis_chat.mark_read(pg_temp.v('dm'));
|
|
243
|
+
select pg_temp.sign_in('a1');
|
|
244
|
+
select genesis_chat.send_message(pg_temp.v('dm'), gen_random_uuid(), 'In a thread', p_thread_root_id => pg_temp.v('vid'));
|
|
245
|
+
select pg_temp.sign_in('b2');
|
|
246
|
+
select is(genesis_chat.unread_total(), (select sum(unread)::integer from genesis_chat.inbox()),
|
|
247
|
+
'the badge counts exactly what the inbox does (a thread reply counts on its thread, not here)');
|
|
248
|
+
select is((select unread from genesis_chat.inbox() where conversation_id = pg_temp.v('tkt')), 1,
|
|
249
|
+
'a member''s count has the staff reply, never the internal note');
|
|
250
|
+
select is(genesis_chat.unread_total() - (select coalesce(sum(unread), 0)::integer from genesis_chat.inbox()), 0,
|
|
251
|
+
'nor in their badge');
|
|
252
|
+
|
|
253
|
+
select * from finish();
|
|
254
|
+
rollback;
|