@marktiderman/genesis-cli 1.19.0 → 1.21.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.
Files changed (42) hide show
  1. package/README.md +98 -1
  2. package/dist/cli.js +682 -74
  3. package/dist/cli.js.map +1 -1
  4. package/dist/doctor.d.ts +1 -1
  5. package/dist/doctor.js +270 -8
  6. package/dist/doctor.js.map +1 -1
  7. package/dist/eslint.cjs +99 -0
  8. package/dist/eslint.cjs.map +1 -0
  9. package/dist/eslint.d.cts +68 -0
  10. package/dist/eslint.d.ts +68 -0
  11. package/dist/eslint.js +72 -0
  12. package/dist/eslint.js.map +1 -0
  13. package/dist/genesis-catalog.json +86 -14
  14. package/dist/index.js +278 -16
  15. package/dist/index.js.map +1 -1
  16. package/dist/starter/README.md +17 -5
  17. package/dist/starter/__tests__/components/setup.ts +11 -0
  18. package/dist/starter/__tests__/starter.test.ts +19 -0
  19. package/dist/starter/eslint.config.js +8 -1
  20. package/dist/starter/nativewind-env.d.ts +1 -0
  21. package/dist/starter/package.json +5 -3
  22. package/dist/starter/src/lib/app.ts +13 -5
  23. package/dist/starter/src/lib/define-starter.ts +1 -1
  24. package/dist/starter/src/lib/devtools-overlay.ts +3 -4
  25. package/dist/starter/src/lib/native-modules.ts +5 -1
  26. package/dist/starter/src/lib/offline.ts +14 -0
  27. package/dist/starter/supabase/migrations/20261024000750_support_default_tenant.sql +18 -0
  28. package/dist/starter/supabase/migrations/20261024000800_genesis_support_tickets.sql +800 -0
  29. package/dist/starter/supabase/migrations/20261025000070_starter_tenancy.sql +26 -0
  30. package/dist/starter/supabase/migrations/20261025000080_genesis_chat_tenants.sql +716 -0
  31. package/dist/starter/supabase/tests/database/chat_people.test.sql +1 -1
  32. package/dist/starter/supabase/tests/database/genesis_chat.test.sql +10 -2
  33. package/dist/starter/supabase/tests/database/genesis_chat_coaching.test.sql +8 -0
  34. package/dist/starter/supabase/tests/database/genesis_chat_search_threads.test.sql +8 -0
  35. package/dist/starter/supabase/tests/database/genesis_chat_tenants.test.sql +259 -0
  36. package/dist/starter/supabase/tests/database/genesis_support.test.sql +15 -3
  37. package/dist/starter/supabase/tests/database/genesis_support_findings.test.sql +10 -0
  38. package/dist/starter/supabase/tests/database/genesis_support_tickets.test.sql +327 -0
  39. package/dist/starter/supabase/tests/database/starter_tenancy.test.sql +19 -0
  40. package/dist/vite.js +270 -8
  41. package/dist/vite.js.map +1 -1
  42. package/package.json +18 -4
@@ -32,7 +32,7 @@ grant execute on function pg_temp.sign_in(uuid) to authenticated;
32
32
 
33
33
  -- a, b and d in one group; e and g left it (e after posting, g without); f has an observer row;
34
34
  -- c is in nothing with them.
35
- insert into genesis_chat.conversations (id, kind) values ('00000000-0000-4000-8000-0000000c9ac1', 'group');
35
+ insert into genesis_chat.conversations (id, kind, tenant_id) values ('00000000-0000-4000-8000-0000000c9ac1', 'group', '00000000-0000-4000-8000-00000000a771');
36
36
  insert into genesis_chat.members (conversation_id, user_id, role, left_at) values
37
37
  ('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a01', 'owner', null),
38
38
  ('00000000-0000-4000-8000-0000000c9ac1', '00000000-0000-4000-8000-0000000c9a02', 'member', null),
@@ -13,6 +13,14 @@ insert into auth.users (id, aud, role, email, instance_id)
13
13
  select ('00000000-0000-4000-8000-00000000c0' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
14
14
  from unnest(array['a1', 'b2', 'c3', 'd4', 'e5']) n;
15
15
 
16
+ -- From 0004 every conversation belongs to a tenant: everyone in this test shares one (the
17
+ -- app's own hook comes back with the rollback).
18
+ create schema if not exists private;
19
+ create or replace function private.genesis_viewer_people(viewer uuid)
20
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
21
+ select '00000000-0000-4000-8000-0000000000f0'::uuid, viewer
22
+ $$;
23
+
16
24
  create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c0' || n)::uuid $$;
17
25
  create function pg_temp.sign_in(n text) returns void language plpgsql as $$
18
26
  begin
@@ -193,8 +201,8 @@ select throws_ok($$ select genesis_chat.add_members(pg_temp.v('grp'), array[pg_t
193
201
  -- Tickets: observers and internal notes ----------------------------------------------------
194
202
 
195
203
  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');
204
+ insert into t select 'tkt', genesis_chat.ensure_conversation('ticket', 'report', 'r-1', 'Login trouble', p_tenant_id => '00000000-0000-4000-8000-0000000000f0');
205
+ select is(genesis_chat.ensure_conversation('ticket', 'report', 'r-1', p_tenant_id => '00000000-0000-4000-8000-0000000000f0'), pg_temp.v('tkt'), 'the server finds a record''s conversation again');
198
206
  select is(genesis_chat.sync_members(pg_temp.v('tkt'), array[pg_temp.person('b2')]), 1, 'and sets its members');
199
207
  select pg_temp.sign_in('b2');
200
208
  select lives_ok($$ select genesis_chat.send_message(pg_temp.v('tkt'), gen_random_uuid(), 'I can''t sign in') $$, 'the person writes in');
@@ -12,6 +12,14 @@ insert into auth.users (id, aud, role, email, instance_id)
12
12
  select ('00000000-0000-4000-8000-00000000c2' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat2-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
13
13
  from unnest(array['a1', 'b2', 'c3', 'd4', 'e5']) n;
14
14
 
15
+ -- From 0004 every conversation belongs to a tenant: everyone in this test shares one (the
16
+ -- app's own hook comes back with the rollback).
17
+ create schema if not exists private;
18
+ create or replace function private.genesis_viewer_people(viewer uuid)
19
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
20
+ select '00000000-0000-4000-8000-0000000000f0'::uuid, viewer
21
+ $$;
22
+
15
23
  create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c2' || n)::uuid $$;
16
24
  create function pg_temp.sign_in(n text) returns void language plpgsql as $$
17
25
  begin
@@ -10,6 +10,14 @@ insert into auth.users (id, aud, role, email, instance_id)
10
10
  select ('00000000-0000-4000-8000-00000000c3' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat3-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
11
11
  from unnest(array['a1', 'b2', 'c3']) n;
12
12
 
13
+ -- From 0004 every conversation belongs to a tenant: everyone in this test shares one (the
14
+ -- app's own hook comes back with the rollback).
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
+
13
21
  create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c3' || n)::uuid $$;
14
22
  create function pg_temp.sign_in(n text) returns void language plpgsql as $$
15
23
  begin
@@ -0,0 +1,259 @@
1
+ -- @feat:chat
2
+ -- pgTAP test for 0004_genesis_chat_tenants.sql (with 0001–0003 applied).
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
4
+ -- Writes only its own users, tenants, kinds and conversations, and rolls everything back. It
5
+ -- stands in its own private.genesis_viewer_people for the length of the test (yours comes back
6
+ -- with the rollback).
7
+ begin;
8
+ create extension if not exists pgtap with schema extensions;
9
+ select plan(53);
10
+
11
+ insert into auth.users (id, aud, role, email, instance_id)
12
+ select ('00000000-0000-4000-8000-00000000c4' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat4-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
13
+ from unnest(array['a1', 'b2', 'c3', 'd4', 'e5']) n;
14
+
15
+ create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c4' || n)::uuid $$;
16
+ create function pg_temp.tenant(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000004' || 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
+ grant execute on function pg_temp.person(text), pg_temp.tenant(text), pg_temp.sign_in(text) to authenticated, service_role;
24
+
25
+ create temp table t (k text primary key, v uuid);
26
+ grant select, insert on t to authenticated, service_role;
27
+ create function pg_temp.v(k_ text) returns uuid language sql stable as $$ select v from t where k = k_ $$;
28
+ grant execute on function pg_temp.v(text) to authenticated, service_role;
29
+
30
+ -- Tenancy: a1 and b2 share t1 and t2; c3 is only in t3; d4 is staff in t1; e5 is staff in t2.
31
+ create table public.genesis_chat4_test_people (user_id uuid, tenant_id uuid, staff boolean default false);
32
+ insert into public.genesis_chat4_test_people values
33
+ (pg_temp.person('a1'), pg_temp.tenant('01'), false), (pg_temp.person('a1'), pg_temp.tenant('02'), false),
34
+ (pg_temp.person('b2'), pg_temp.tenant('01'), false), (pg_temp.person('b2'), pg_temp.tenant('02'), false),
35
+ (pg_temp.person('c3'), pg_temp.tenant('03'), false),
36
+ (pg_temp.person('d4'), pg_temp.tenant('01'), true),
37
+ (pg_temp.person('e5'), pg_temp.tenant('02'), true);
38
+ create schema if not exists private;
39
+ create or replace function private.genesis_viewer_people(viewer uuid)
40
+ returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
41
+ select p.tenant_id, p.user_id from public.genesis_chat4_test_people p where p.user_id = viewer
42
+ $$;
43
+
44
+ create function public.genesis_chat4_test_may_start(member_ids uuid[], subject_type text, subject_id text, user_id uuid)
45
+ returns boolean language sql stable set search_path = '' as $$ select true $$;
46
+ select genesis_chat.register_kind('direct', 'Direct message', p_max_members => 2,
47
+ p_start_check => 'public.genesis_chat4_test_may_start(uuid[], text, text, uuid)');
48
+ select genesis_chat.register_kind('group', 'Group', p_membership => 'admins',
49
+ p_start_check => 'public.genesis_chat4_test_may_start(uuid[], text, text, uuid)');
50
+
51
+ -- A help desk kind: staff observe and post; a ticket's tenant comes from its resolver.
52
+ create table public.genesis_chat4_test_tickets (id text primary key, tenant_id uuid);
53
+ insert into public.genesis_chat4_test_tickets values ('T-1', pg_temp.tenant('01')), ('T-2', pg_temp.tenant('01'));
54
+ create function public.genesis_chat4_test_ticket_tenant(subject_id text) returns uuid language sql stable set search_path = '' as $$
55
+ select k.tenant_id from public.genesis_chat4_test_tickets k where k.id = subject_id $$;
56
+ create function public.genesis_chat4_test_staff(conversation_id uuid, user_id uuid) returns boolean language sql stable set search_path = '' as $$
57
+ select exists (select 1 from public.genesis_chat4_test_people p where p.user_id = genesis_chat4_test_staff.user_id and p.staff) $$;
58
+ select genesis_chat.register_kind('desk', 'Help desk', p_max_members => 2,
59
+ p_observer_check => 'public.genesis_chat4_test_staff(uuid, uuid)', p_observers_post => true);
60
+
61
+ -- ── The column ────────────────────────────────────────────────────────────────
62
+
63
+ select ok((select attnotnull from pg_attribute where attrelid = 'genesis_chat.conversations'::regclass and attname = 'tenant_id'),
64
+ 'every conversation has a tenant');
65
+
66
+ -- ── Tenants of people ─────────────────────────────────────────────────────────
67
+
68
+ select is((select array_agg(x order by x) from genesis_chat.tenants_of(pg_temp.person('a1')) x),
69
+ array[pg_temp.tenant('01'), pg_temp.tenant('02')], 'a person''s tenants come from the app''s hook');
70
+ select is((select array_agg(x order by x) from genesis_chat.shared_tenants(array[pg_temp.person('a1'), pg_temp.person('d4')]) x),
71
+ array[pg_temp.tenant('01')], 'the tenants people share');
72
+ select is((select count(*)::integer from genesis_chat.shared_tenants(array[pg_temp.person('a1'), pg_temp.person('c3')])), 0,
73
+ 'and none when they share none');
74
+
75
+ -- ── Starting conversations ────────────────────────────────────────────────────
76
+
77
+ select pg_temp.sign_in('a1');
78
+ select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]) $$, '22023', null,
79
+ 'people who share several tenants choose which one');
80
+ insert into t select 'dm1', genesis_chat.start_conversation('direct', array[pg_temp.person('b2')], p_tenant_id => pg_temp.tenant('01'));
81
+ insert into t select 'dm2', genesis_chat.start_conversation('direct', array[pg_temp.person('b2')], p_tenant_id => pg_temp.tenant('02'));
82
+ select isnt(pg_temp.v('dm1'), pg_temp.v('dm2'), 'a direct conversation is one per pair per tenant');
83
+ select is(genesis_chat.start_conversation('direct', array[pg_temp.person('b2')], p_tenant_id => pg_temp.tenant('01')), pg_temp.v('dm1'),
84
+ 'and found again in its tenant');
85
+ select pg_temp.sign_in('b2');
86
+ select is((select tenant_id from genesis_chat.conversations where id = pg_temp.v('dm2')), pg_temp.tenant('02'), 'in the tenant chosen');
87
+ select pg_temp.sign_in('a1');
88
+ insert into t select 'dm3', genesis_chat.start_conversation('direct', array[pg_temp.person('d4')]);
89
+ select pg_temp.sign_in('d4');
90
+ select is((select tenant_id from genesis_chat.conversations where id = pg_temp.v('dm3')), pg_temp.tenant('01'),
91
+ 'people who share one tenant start there without choosing');
92
+ select pg_temp.sign_in('a1');
93
+ select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('c3')]) $$, '42501', null,
94
+ 'people with no tenant in common can''t start one');
95
+ select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('d4')], p_tenant_id => pg_temp.tenant('02')) $$, '42501', null,
96
+ 'nor in a tenant one of them isn''t in');
97
+ select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('b2')], p_tenant_id => pg_temp.tenant('03')) $$, '42501', null,
98
+ 'nor in a tenant neither of them is in');
99
+
100
+ -- ── Adding people ─────────────────────────────────────────────────────────────
101
+
102
+ insert into t select 'grp', genesis_chat.start_conversation('group', array[pg_temp.person('b2')], 'Crew', p_tenant_id => pg_temp.tenant('02'));
103
+ select throws_ok($$ select genesis_chat.add_members(pg_temp.v('grp'), array[pg_temp.person('d4')]) $$, '42501', null,
104
+ 'only people of the conversation''s tenant are added');
105
+ select lives_ok($$ select genesis_chat.add_members(pg_temp.v('grp'), array[pg_temp.person('e5')]) $$,
106
+ 'people of its tenant are');
107
+
108
+ -- ── Kinds that cross tenants ──────────────────────────────────────────────────
109
+
110
+ reset role;
111
+ select throws_ok($$ select genesis_chat.register_kind('pen_pal', 'Pen pal', p_cross_tenant => true,
112
+ p_observer_check => 'public.genesis_chat4_test_staff(uuid, uuid)') $$, '22023', null,
113
+ 'a kind that crosses tenants has no observers');
114
+ select genesis_chat.register_kind('pen_pal', 'Pen pal', p_membership => 'admins', p_cross_tenant => true,
115
+ p_start_check => 'public.genesis_chat4_test_may_start(uuid[], text, text, uuid)');
116
+ select pg_temp.sign_in('c3');
117
+ insert into t select 'pp', genesis_chat.start_conversation('pen_pal', array[pg_temp.person('a1')], 'Letters');
118
+ select pg_temp.sign_in('a1');
119
+ select is((select tenant_id from genesis_chat.conversations where id = pg_temp.v('pp')), pg_temp.tenant('03'),
120
+ 'people with no tenant in common start one in the starter''s tenant, where the kind allows it');
121
+ select throws_ok($$ select genesis_chat.start_conversation('pen_pal', array[pg_temp.person('c3')]) $$, '22023', null,
122
+ 'a starter in several tenants chooses one of theirs');
123
+ select lives_ok($$ select genesis_chat.start_conversation('pen_pal', array[pg_temp.person('c3')], p_tenant_id => pg_temp.tenant('02')) $$,
124
+ 'and may choose any of their own');
125
+ select throws_ok($$ select genesis_chat.start_conversation('pen_pal', array[pg_temp.person('c3')], p_tenant_id => pg_temp.tenant('03')) $$, '42501', null,
126
+ 'but not one only the others are in');
127
+ select pg_temp.sign_in('c3');
128
+ select lives_ok($$ select genesis_chat.add_members(pg_temp.v('pp'), array[pg_temp.person('e5')]) $$,
129
+ 'and people from anywhere are added');
130
+
131
+ -- ── Subjects ──────────────────────────────────────────────────────────────────
132
+
133
+ reset role;
134
+ select throws_ok($$ select genesis_chat.register_subject('desk', 'ticket', 'public.genesis_chat4_test_staff(uuid, uuid)') $$, '22023', null,
135
+ 'a resolver takes (subject_id text) and returns the tenant');
136
+ -- A conversation made before the type was renamed: it's found under its old name.
137
+ insert into genesis_chat.conversations (kind, tenant_id, subject_type, subject_id) values ('desk', pg_temp.tenant('01'), 'support_report', 'T-2');
138
+ select lives_ok($$ select genesis_chat.register_subject('desk', 'ticket', 'public.genesis_chat4_test_ticket_tenant(text)', array['support_report']) $$,
139
+ 'a subject type registers with its resolver and older names');
140
+ select throws_ok($$ select genesis_chat.register_subject('desk', 'case', 'public.genesis_chat4_test_ticket_tenant(text)', array['ticket']) $$, '23505', null,
141
+ 'one name is one type within a kind');
142
+
143
+ insert into t select 'tk1', genesis_chat.ensure_conversation('desk', 'support_report', 'T-1', 'Printer');
144
+ select is((select subject_type from genesis_chat.conversations where id = pg_temp.v('tk1')), 'ticket',
145
+ 'a conversation made under an older name is stored under the type''s own');
146
+ select is((select tenant_id from genesis_chat.conversations where id = pg_temp.v('tk1')), pg_temp.tenant('01'),
147
+ 'in the tenant its resolver names');
148
+ select is(genesis_chat.ensure_conversation('desk', 'ticket', 'T-1'), pg_temp.v('tk1'), 'and found again under either name');
149
+ select is(genesis_chat.ensure_conversation('desk', 'ticket', 'T-2'),
150
+ (select id from genesis_chat.conversations where kind = 'desk' and subject_id = 'T-2'),
151
+ 'one made before the rename is found, not made twice');
152
+ select throws_ok($$ select genesis_chat.ensure_conversation('desk', 'ticket', 'T-404') $$, '22023', null,
153
+ 'a record the resolver doesn''t know has no conversation');
154
+ select throws_ok($$ select genesis_chat.ensure_conversation('desk', 'ticket', 'T-1', p_tenant_id => pg_temp.tenant('02')) $$, '22023', null,
155
+ 'a tenant that isn''t the record''s is refused');
156
+ select throws_ok($$ select genesis_chat.ensure_conversation('desk', 'invoice', 'I-1') $$, '22023', null,
157
+ 'an unregistered type needs its tenant');
158
+ select lives_ok($$ select genesis_chat.ensure_conversation('desk', 'invoice', 'I-1', p_tenant_id => pg_temp.tenant('03')) $$,
159
+ 'and takes it');
160
+ create function private.genesis_chat_resolve_order(subject_id text) returns uuid language sql stable set search_path = '' as $$
161
+ select case when subject_id = 'O-1' then '00000000-0000-4000-8000-000000000402'::uuid end $$;
162
+ insert into t select 'ord', genesis_chat.ensure_conversation('desk', 'order', 'O-1');
163
+ select is((select tenant_id from genesis_chat.conversations where id = pg_temp.v('ord')),
164
+ pg_temp.tenant('02'), 'or a resolver named for the type names it');
165
+ select throws_ok($$ select genesis_chat.ensure_conversation('desk', 'order', 'O-404') $$, '22023', null,
166
+ 'and a record that resolver doesn''t know has no conversation');
167
+ select throws_ok($$ select genesis_chat.ensure_conversation('desk', 'Bad Type!', 'x', p_tenant_id => pg_temp.tenant('01')) $$, '22023', null,
168
+ 'an unregistered subject type must still be a type name');
169
+
170
+ -- start_conversation uses the same resolver for an unregistered type, and checks first.
171
+ create function public.genesis_chat4_test_never(member_ids uuid[], subject_type text, subject_id text, user_id uuid)
172
+ returns boolean language sql stable set search_path = '' as $$ select false $$;
173
+ select genesis_chat.register_kind('closed', 'Closed', p_start_check => 'public.genesis_chat4_test_never(uuid[], text, text, uuid)');
174
+ select pg_temp.sign_in('a1');
175
+ insert into t select 'og', genesis_chat.start_conversation('group', array[pg_temp.person('b2')], 'Order chat', 'order', 'O-1');
176
+ reset role;
177
+ select is((select tenant_id from genesis_chat.conversations where id = pg_temp.v('og')), pg_temp.tenant('02'),
178
+ 'starting one about an unregistered type takes its resolver''s tenant');
179
+ select pg_temp.sign_in('a1');
180
+ select throws_ok($$ select genesis_chat.start_conversation('group', array[pg_temp.person('b2')], null, 'order', 'O-404') $$, '22023', null,
181
+ 'and refuses a record that resolver doesn''t know');
182
+ select throws_ok($$ select genesis_chat.start_conversation('closed', array[pg_temp.person('b2')], null, 'order', 'O-404') $$, '42501', null,
183
+ 'a start check that says no is answered first, saying nothing about the record');
184
+ reset role;
185
+
186
+ -- ── Observers ─────────────────────────────────────────────────────────────────
187
+
188
+ -- d4 (staff in t1) reported T-1 themselves; a1 is its member too, so it's both of theirs.
189
+ select genesis_chat.sync_members(pg_temp.v('tk1'), array[pg_temp.person('d4')], 'member');
190
+ insert into t select 'tk3', genesis_chat.ensure_conversation('desk', 'invoice', 'I-9', p_tenant_id => pg_temp.tenant('01'));
191
+ select genesis_chat.sync_members(pg_temp.v('tk3'), array[pg_temp.person('a1')], 'member');
192
+
193
+ select ok(genesis_chat.observes(pg_temp.v('tk3'), pg_temp.person('d4')), 'staff of the conversation''s tenant observe it');
194
+ select ok(not genesis_chat.observes(pg_temp.v('tk3'), pg_temp.person('e5')), 'staff of another tenant don''t');
195
+ select ok(not genesis_chat.observes(pg_temp.v('tk1'), pg_temp.person('d4')), 'a member is never also an observer, staff or not');
196
+ select genesis_chat.sync_members(pg_temp.v('tk1'), array[]::uuid[], 'member');
197
+ select ok(not genesis_chat.observes(pg_temp.v('tk1'), pg_temp.person('d4')), 'nor once they''ve left');
198
+
199
+ -- An internal note in a conversation a staff member opened themselves stays out of their reach.
200
+ select pg_temp.sign_in('d4');
201
+ select genesis_chat.send_message(pg_temp.v('tk3'), gen_random_uuid(), 'Check the toner', p_visibility => 'observers');
202
+ select genesis_chat.send_message(pg_temp.v('tk3'), gen_random_uuid(), 'On it');
203
+ select pg_temp.sign_in('a1');
204
+ select is((select count(*)::integer from genesis_chat.messages where conversation_id = pg_temp.v('tk3')), 1,
205
+ 'the member reads the reply, not the internal note');
206
+
207
+ -- A kind whose own check decides by the record's tenant turns chat's tenant gate off: staff the
208
+ -- tenancy hook doesn't list for this tenant still observe, and a member still never does.
209
+ reset role;
210
+ select genesis_chat.register_kind('desk_app', 'App help desk', p_max_members => 2,
211
+ p_observer_check => 'public.genesis_chat4_test_staff(uuid, uuid)', p_observers_post => true,
212
+ p_observer_tenant_gate => false);
213
+ insert into t select 'app1', genesis_chat.ensure_conversation('desk_app', 'invoice', 'I-50', p_tenant_id => pg_temp.tenant('01'));
214
+ select genesis_chat.sync_members(pg_temp.v('app1'), array[pg_temp.person('e5')], 'member');
215
+ insert into t select 'app2', genesis_chat.ensure_conversation('desk_app', 'invoice', 'I-51', p_tenant_id => pg_temp.tenant('01'));
216
+ select ok(genesis_chat.observes(pg_temp.v('app2'), pg_temp.person('e5')),
217
+ 'with the gate off, the kind''s check decides: staff of another tenant observe');
218
+ select ok(not genesis_chat.observes(pg_temp.v('app1'), pg_temp.person('e5')),
219
+ 'but a member is still never an observer');
220
+ select ok(not genesis_chat.observes(pg_temp.v('app2'), pg_temp.person('a1')),
221
+ 'and the kind''s check still says no to non-staff');
222
+
223
+ -- Registering it again without the argument keeps the gate it has.
224
+ select genesis_chat.register_kind('desk_app', 'App help desk', p_max_members => 2,
225
+ p_observer_check => 'public.genesis_chat4_test_staff(uuid, uuid)', p_observers_post => true);
226
+ select is((select observer_tenant_gate from genesis_chat.kinds where kind = 'desk_app'), false,
227
+ 'registering a kind again without the gate argument keeps its gate');
228
+
229
+ -- A block that registered its kind before this migration turns the gate off by convention.
230
+ create function private.genesis_chat_ungated_desk_conv() returns boolean language sql as $$ select true $$;
231
+ select genesis_chat.register_kind('desk_conv', 'Convention desk', p_max_members => 2,
232
+ p_observer_check => 'public.genesis_chat4_test_staff(uuid, uuid)');
233
+ select is((select observer_tenant_gate from genesis_chat.kinds where kind = 'desk_conv'), false,
234
+ 'private.genesis_chat_ungated_<kind>() returning true turns the gate off');
235
+ select is((select observer_tenant_gate from genesis_chat.kinds where kind = 'desk'), true,
236
+ 'and a kind without one keeps the gate on');
237
+
238
+ -- ── Without the hook ──────────────────────────────────────────────────────────
239
+
240
+ reset role;
241
+ drop function private.genesis_viewer_people(uuid);
242
+ select is((select count(*)::integer from genesis_chat.tenants_of(pg_temp.person('a1'))), 0, 'without the hook nobody belongs to a tenant');
243
+ select ok(not genesis_chat.observes(pg_temp.v('tk3'), pg_temp.person('d4')), 'so nobody observes');
244
+ select pg_temp.sign_in('a1');
245
+ select throws_ok($$ select genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]) $$, '42501', null,
246
+ 'and nobody starts one');
247
+
248
+ -- ── Privileges ────────────────────────────────────────────────────────────────
249
+
250
+ reset role;
251
+ select ok(not has_function_privilege('authenticated', 'genesis_chat.register_subject(text, text, regprocedure, text[])', 'execute')
252
+ and not has_function_privilege('anon', 'genesis_chat.register_subject(text, text, regprocedure, text[])', 'execute'),
253
+ 'only the server registers subjects');
254
+ select ok(not has_function_privilege('authenticated', 'genesis_chat.tenants_of(uuid)', 'execute')
255
+ and not has_function_privilege('authenticated', 'genesis_chat.ensure_conversation(text, text, text, text, jsonb, uuid)', 'execute'),
256
+ 'nobody signed in asks about anyone''s tenants or ensures a conversation');
257
+
258
+ select * from finish();
259
+ rollback;
@@ -9,6 +9,16 @@
9
9
  -- request.jwt.claim.sub on older images), so pg_temp.sign_in sets both.
10
10
  begin;
11
11
  create extension if not exists pgtap with schema extensions;
12
+ -- Since support 0004 every ticket has a tenant: an app without the default-tenant hook gets one
13
+ -- here, inside this transaction only.
14
+ do $hook$ begin
15
+ if to_regprocedure('private.genesis_support_default_tenant()') is null
16
+ and to_regclass('public.ticket_agents') is not null then
17
+ execute $f$create function private.genesis_support_default_tenant() returns uuid language sql stable
18
+ set search_path = '' as 'select ''00000000-0000-4000-8000-00000000000f''::uuid'$f$;
19
+ revoke execute on function private.genesis_support_default_tenant() from public, anon, authenticated;
20
+ end if;
21
+ end $hook$;
12
22
  select plan(119);
13
23
 
14
24
  -- People: a1 reports, b2 is someone else, c3 is staff (once the hook exists),
@@ -50,15 +60,17 @@ grant execute on function pg_temp.person(text), pg_temp.sign_in(text), pg_temp.r
50
60
  drop function if exists private.genesis_support_staff(uuid);
51
61
 
52
62
  -- Shape -------------------------------------------------------------------------
53
- select has_table('public', 'support_reports', 'support_reports exists');
54
- select has_table('public', 'support_attachments', 'support_attachments exists');
63
+ -- Since support 0004 these are public.tickets and public.ticket_attachments, and the old names views.
64
+ select has_relation('public', 'support_reports', 'support_reports exists');
65
+ select has_relation('public', 'support_attachments', 'support_attachments exists');
55
66
  select has_table('public', 'support_replies', 'support_replies exists');
56
67
  select has_table('public', 'support_ideas', 'support_ideas exists');
57
68
  select has_table('public', 'support_idea_votes', 'support_idea_votes exists');
58
69
  select col_is_pk('public', 'support_idea_votes', array['idea_id', 'user_id'], 'one vote per person per idea');
59
70
  select ok(
60
71
  (select bool_and(relrowsecurity) from pg_class where oid in (
61
- 'public.support_reports'::regclass, 'public.support_attachments'::regclass, 'public.support_attachment_purges'::regclass,
72
+ coalesce(to_regclass('public.tickets'), 'public.support_reports'::regclass),
73
+ coalesce(to_regclass('public.ticket_attachments'), 'public.support_attachments'::regclass), 'public.support_attachment_purges'::regclass,
62
74
  'public.support_replies'::regclass, 'public.support_ideas'::regclass, 'public.support_idea_votes'::regclass)),
63
75
  'RLS is on for every support table'
64
76
  );
@@ -10,6 +10,16 @@
10
10
  -- hook says so), nobody else's.
11
11
  begin;
12
12
  create extension if not exists pgtap with schema extensions;
13
+ -- Since support 0004 every ticket has a tenant: an app without the default-tenant hook gets one
14
+ -- here, inside this transaction only.
15
+ do $hook$ begin
16
+ if to_regprocedure('private.genesis_support_default_tenant()') is null
17
+ and to_regclass('public.ticket_agents') is not null then
18
+ execute $f$create function private.genesis_support_default_tenant() returns uuid language sql stable
19
+ set search_path = '' as 'select ''00000000-0000-4000-8000-00000000000f''::uuid'$f$;
20
+ revoke execute on function private.genesis_support_default_tenant() from public, anon, authenticated;
21
+ end if;
22
+ end $hook$;
13
23
  select plan(32);
14
24
 
15
25
  insert into auth.users (id, aud, role, email, instance_id)