@jsb188/mday 1.1.10 → 1.1.11

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 (119) hide show
  1. package/package.json +105 -54
  2. package/src/constants/agentDoc.ts +12 -0
  3. package/src/constants/agentMemory.ts +154 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/campaign.ts +127 -0
  6. package/src/constants/dataTable.ts +35 -9
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +20 -4
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/integration.ts +8 -9
  13. package/src/constants/log.ts +7 -410
  14. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  15. package/src/constants/logTaxonomy.generated.ts +1103 -0
  16. package/src/constants/noteDoc.ts +77 -0
  17. package/src/constants/organization.ts +13 -15
  18. package/src/constants/pageRoute.ts +23 -0
  19. package/src/constants/schedule.ts +220 -0
  20. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  21. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  22. package/src/constants/sheet.ts +154 -6
  23. package/src/constants/sheetRegionSources.generated.ts +578 -0
  24. package/src/constants/square.ts +6 -6
  25. package/src/constants/storage.ts +56 -7
  26. package/src/constants/usage.ts +79 -0
  27. package/src/schemas/editLogEntry.ts +1010 -777
  28. package/src/schemas/fieldDocsQuery.ts +12 -0
  29. package/src/schemas/inboundContact.ts +60 -55
  30. package/src/schemas/logsQuery.ts +55 -49
  31. package/src/schemas/organization.ts +49 -46
  32. package/src/types/account.d.ts +4 -1
  33. package/src/types/agentDoc.d.ts +64 -0
  34. package/src/types/agentMemory.d.ts +74 -0
  35. package/src/types/campaign.d.ts +683 -0
  36. package/src/types/dataTable.d.ts +175 -256
  37. package/src/types/docAccess.d.ts +96 -0
  38. package/src/types/email.d.ts +83 -12
  39. package/src/types/fieldDoc.d.ts +273 -0
  40. package/src/types/inboundContact.d.ts +2 -1
  41. package/src/types/log.d.ts +209 -341
  42. package/src/types/noteDoc.d.ts +86 -0
  43. package/src/types/organization.d.ts +83 -40
  44. package/src/types/pageRoute.d.ts +48 -0
  45. package/src/types/schedule.d.ts +391 -0
  46. package/src/types/sheet.d.ts +862 -30
  47. package/src/types/storage.d.ts +206 -125
  48. package/src/types/usage.d.ts +106 -0
  49. package/src/types/workflow.d.ts +149 -18
  50. package/src/utils/agentDoc.ts +109 -0
  51. package/src/utils/aiChat.ts +26 -0
  52. package/src/utils/campaign.ts +741 -0
  53. package/src/utils/campaignFormEditor.ts +871 -0
  54. package/src/utils/dataTable.ts +425 -410
  55. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  56. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  57. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  58. package/src/utils/dataTableRowDocument.ts +508 -0
  59. package/src/utils/dataTableSource.ts +230 -0
  60. package/src/utils/docAccess.ts +383 -0
  61. package/src/utils/documentName.ts +53 -0
  62. package/src/utils/email.ts +112 -53
  63. package/src/utils/fieldDoc.ts +113 -0
  64. package/src/utils/log.ts +169 -176
  65. package/src/utils/logRegistryValue.ts +90 -0
  66. package/src/utils/logStoreRead.ts +266 -0
  67. package/src/utils/logTaxonomy.ts +76 -0
  68. package/src/utils/logTaxonomyDisplay.ts +439 -0
  69. package/src/utils/noteDoc.ts +827 -0
  70. package/src/utils/organization.ts +510 -91
  71. package/src/utils/pageRoute.ts +71 -0
  72. package/src/utils/schedule.ts +799 -0
  73. package/src/utils/scheduleEvaluation.ts +1994 -0
  74. package/src/utils/scheduleICS.ts +246 -0
  75. package/src/utils/scheduleRuleTemplates.ts +188 -0
  76. package/src/utils/sheet.ts +4842 -39
  77. package/src/utils/sheetAxisMove.ts +830 -0
  78. package/src/utils/sheetBootstrap.ts +1769 -0
  79. package/src/utils/sheetCommand.ts +423 -0
  80. package/src/utils/sheetExport.ts +238 -0
  81. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  82. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  83. package/src/utils/sheetJournal.ts +78 -0
  84. package/src/utils/sheetNumberFormat.ts +1211 -0
  85. package/src/utils/sheetRegion.ts +374 -0
  86. package/src/utils/sheetRegionFilter.ts +1259 -0
  87. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  88. package/src/utils/sheetSequence.ts +148 -0
  89. package/src/utils/sheetSnapshot.ts +992 -0
  90. package/src/utils/sheetStructureEdit.ts +1284 -0
  91. package/src/utils/square.ts +22 -12
  92. package/src/utils/storage.ts +152 -0
  93. package/src/utils/table-route.ts +127 -11
  94. package/src/utils/workflow.ts +191 -23
  95. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  96. package/tests/docAccess.test.ts +525 -0
  97. package/tests/editLogEntry.test.ts +112 -0
  98. package/tests/noteDoc.test.ts +669 -0
  99. package/tests/schedule.test.ts +693 -0
  100. package/tests/scheduleEvaluation.test.ts +1970 -0
  101. package/tests/scheduleICS.test.ts +208 -0
  102. package/tests/scheduleRuleTemplates.test.ts +176 -0
  103. package/tests/scheduleSweepInvariants.test.ts +428 -0
  104. package/tests/sheetAxisMove.test.ts +343 -0
  105. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  106. package/tests/sheetRegionFilter.test.ts +1135 -0
  107. package/tests/sheetSequence.test.ts +41 -0
  108. package/tests/sheetStructureEdit.test.ts +82 -0
  109. package/tsconfig.json +10 -10
  110. package/vitest.config.ts +8 -0
  111. package/src/constants/log-activityTexts.ts +0 -235
  112. package/src/constants/product.ts +0 -42
  113. package/src/constants/report.ts +0 -44
  114. package/src/schemas/productsQuery.ts +0 -53
  115. package/src/schemas/reportsQuery.ts +0 -20
  116. package/src/types/product.d.ts +0 -84
  117. package/src/types/report.d.ts +0 -290
  118. package/src/utils/product-calEvent.ts +0 -455
  119. package/src/utils/report.ts +0 -158
@@ -0,0 +1,110 @@
1
+ import { DATA_TABLE_FIELD_TYPE_ENUMS } from '@jsb188/mday/constants/dataTable.ts';
2
+ import {
3
+ isDataTableDateLikeFieldType,
4
+ isDataTableNumberLikeFieldType,
5
+ isDataTableWeekFieldType,
6
+ } from '@jsb188/mday/utils/dataTable.ts';
7
+ import {
8
+ buildDataTableFieldTypeDescriptorsFromConstants,
9
+ collectDataTableFieldTypeDescriptorIssues,
10
+ } from '@jsb188/mday/utils/dataTableFieldTypeRegistry.ts';
11
+ import { describe, expect, it } from 'vitest';
12
+
13
+ // Pre-registry hardcoded helper results (the historical literals the
14
+ // semantics lookups replaced); every key × helper cell is asserted so a
15
+ // registry row change can never silently flip helper behavior.
16
+ const SEMANTICS_TRUTH_TABLE: Record<
17
+ string,
18
+ { numberLike: boolean; dateLike: boolean; week: boolean }
19
+ > = {
20
+ TEXT: { numberLike: false, dateLike: false, week: false },
21
+ ID: { numberLike: false, dateLike: false, week: false },
22
+ ID_OR_TEXT: { numberLike: false, dateLike: false, week: false },
23
+ NUMBER: { numberLike: true, dateLike: false, week: false },
24
+ PRICE: { numberLike: true, dateLike: false, week: false },
25
+ BOOLEAN: { numberLike: false, dateLike: false, week: false },
26
+ DATE: { numberLike: false, dateLike: true, week: false },
27
+ WEEK_OF_MON: { numberLike: false, dateLike: true, week: true },
28
+ WEEK_OF_SUN: { numberLike: false, dateLike: true, week: true },
29
+ DATETIME: { numberLike: false, dateLike: false, week: false },
30
+ SELECT: { numberLike: false, dateLike: false, week: false },
31
+ SELECT_OR_TEXT: { numberLike: false, dateLike: false, week: false },
32
+ MULTI_SELECT: { numberLike: false, dateLike: false, week: false },
33
+ JSON: { numberLike: false, dateLike: false, week: false },
34
+ };
35
+
36
+ describe('dataTable field type semantics lookups', () => {
37
+ it('covers exactly the registry enum keys', () => {
38
+ expect(Object.keys(SEMANTICS_TRUTH_TABLE).sort()).toEqual(
39
+ [...DATA_TABLE_FIELD_TYPE_ENUMS].sort(),
40
+ );
41
+ });
42
+
43
+ it('matches the pre-registry hardcoded results for all 14 keys × 3 helpers', () => {
44
+ for (const fieldType of DATA_TABLE_FIELD_TYPE_ENUMS) {
45
+ const expected = SEMANTICS_TRUTH_TABLE[fieldType];
46
+ expect(
47
+ isDataTableNumberLikeFieldType(fieldType),
48
+ `${fieldType} numberLike`,
49
+ ).toBe(expected.numberLike);
50
+ expect(
51
+ isDataTableDateLikeFieldType(fieldType),
52
+ `${fieldType} dateLike`,
53
+ ).toBe(expected.dateLike);
54
+ expect(isDataTableWeekFieldType(fieldType), `${fieldType} week`).toBe(
55
+ expected.week,
56
+ );
57
+ }
58
+ });
59
+
60
+ it('returns false for missing and unknown field types', () => {
61
+ for (const fieldType of [null, undefined, 'NOT_A_TYPE' as never]) {
62
+ expect(isDataTableNumberLikeFieldType(fieldType)).toBe(false);
63
+ expect(isDataTableDateLikeFieldType(fieldType)).toBe(false);
64
+ expect(isDataTableWeekFieldType(fieldType)).toBe(false);
65
+ }
66
+ });
67
+ });
68
+
69
+ describe('buildDataTableFieldTypeDescriptorsFromConstants', () => {
70
+ it('derives a valid descriptor set in enum order with the seed semantics mapping', () => {
71
+ const descriptors = buildDataTableFieldTypeDescriptorsFromConstants();
72
+
73
+ expect(collectDataTableFieldTypeDescriptorIssues(descriptors)).toEqual([]);
74
+ expect(descriptors.map((descriptor) => descriptor.key)).toEqual([
75
+ ...DATA_TABLE_FIELD_TYPE_ENUMS,
76
+ ]);
77
+ expect(descriptors.map((descriptor) => descriptor.ordinal)).toEqual(
78
+ DATA_TABLE_FIELD_TYPE_ENUMS.map((_, index) => index),
79
+ );
80
+
81
+ const semanticsByKey = Object.fromEntries(
82
+ descriptors.map((descriptor) => [
83
+ descriptor.key,
84
+ descriptor.semanticsKey,
85
+ ]),
86
+ );
87
+ expect(semanticsByKey.PRICE).toBe('NUMBER');
88
+ expect(semanticsByKey.WEEK_OF_MON).toBe('WEEK');
89
+ expect(semanticsByKey.WEEK_OF_SUN).toBe('WEEK');
90
+ expect(semanticsByKey.TEXT).toBe('TEXT');
91
+ });
92
+ });
93
+
94
+ describe('collectDataTableFieldTypeDescriptorIssues', () => {
95
+ it('flags empty sets, duplicates, bad keys, and unknown semantics', () => {
96
+ expect(collectDataTableFieldTypeDescriptorIssues([])).toEqual([
97
+ 'registry has no field type rows',
98
+ ]);
99
+
100
+ const issues = collectDataTableFieldTypeDescriptorIssues([
101
+ { key: 'TEXT', semanticsKey: 'TEXT', ordinal: 0 },
102
+ { key: 'TEXT', semanticsKey: 'TEXT', ordinal: 0 },
103
+ { key: 'bad key', semanticsKey: 'NOPE' as never, ordinal: 1 },
104
+ ]);
105
+ expect(issues).toContain('duplicate field type key TEXT');
106
+ expect(issues).toContain('TEXT: duplicate ordinal 0');
107
+ expect(issues).toContain('bad key: invalid field type key');
108
+ expect(issues).toContain('bad key: unknown semanticsKey NOPE');
109
+ });
110
+ });
@@ -0,0 +1,525 @@
1
+ import type {
2
+ DocAccessGrantObj,
3
+ DocAccessObj,
4
+ DocAccessViewerCtx,
5
+ } from '@jsb188/mday/types/docAccess.d.ts';
6
+ import { getEffectiveDocPermission } from '@jsb188/mday/utils/docAccess.ts';
7
+ import { describe, expect, it } from 'vitest';
8
+
9
+ /*
10
+ * getEffectiveDocPermission is the single decision every access-controlled
11
+ * document type routes through: Sheets, Notes, and schedule calendars all
12
+ * reduce their own authorization question to this one call, and the ICS feed
13
+ * reconstructs a viewer context by hand just to ask it. Nothing downstream
14
+ * second-guesses the answer — "NONE" is the only thing standing between a
15
+ * document and somebody outside the organization that owns it.
16
+ *
17
+ * The function is also unusually easy to break silently. Its inputs are five
18
+ * independent booleans and three permission strings, the grant lookups compare
19
+ * ids that arrive as strings, numbers, or bigints depending on the caller, and
20
+ * the "no grant" / "explicit NONE grant" distinction is carried by null rather
21
+ * than by a value. A refactor that reads naturally can flip an entire class of
22
+ * viewers from denied to allowed without failing a type check.
23
+ *
24
+ * These tests pin the full matrix so that flip cannot happen unnoticed.
25
+ */
26
+
27
+ /*
28
+ * Build one document's stored access settings. The defaults mirror the
29
+ * restrictive end of the policy (private link, no network access) so each test
30
+ * only states the rule it is about.
31
+ */
32
+
33
+ function makeAccess(overrides: Partial<DocAccessObj> = {}): DocAccessObj {
34
+ return {
35
+ team: 'EDIT',
36
+ public: 'NONE',
37
+ network: 'NONE',
38
+ grants: [],
39
+ ...overrides,
40
+ };
41
+ }
42
+
43
+ /*
44
+ * Build one viewer context. The defaults describe a signed-in stranger: no
45
+ * membership anywhere near the owner organization.
46
+ */
47
+
48
+ function makeViewer(
49
+ overrides: Partial<DocAccessViewerCtx> = {},
50
+ ): DocAccessViewerCtx {
51
+ return {
52
+ accountId: '100',
53
+ isOwnerOrgMember: false,
54
+ isAdminOrOwner: false,
55
+ hasWriteACL: false,
56
+ isNetworkOrgMember: false,
57
+ viewerOrganizationIds: [],
58
+ ...overrides,
59
+ };
60
+ }
61
+
62
+ /*
63
+ * Build one grant targeting a single account.
64
+ */
65
+
66
+ function makeAccountGrant(
67
+ accountId: string | number | bigint,
68
+ permission: DocAccessGrantObj['permission'],
69
+ ): DocAccessGrantObj {
70
+ return { granteeType: 'ACCOUNT', granteeId: accountId, permission };
71
+ }
72
+
73
+ /*
74
+ * Build one grant targeting a single organization.
75
+ */
76
+
77
+ function makeOrgGrant(
78
+ organizationId: string | number | bigint,
79
+ permission: DocAccessGrantObj['permission'],
80
+ ): DocAccessGrantObj {
81
+ return { granteeType: 'ORGANIZATION', granteeId: organizationId, permission };
82
+ }
83
+
84
+ describe('getEffectiveDocPermission base policy matrix', () => {
85
+ it('gives owner-org members with the write ACL exactly the team policy', () => {
86
+ const viewer = makeViewer({ isOwnerOrgMember: true, hasWriteACL: true });
87
+
88
+ expect(
89
+ getEffectiveDocPermission(makeAccess({ team: 'EDIT' }), viewer),
90
+ ).toBe('EDIT');
91
+ expect(
92
+ getEffectiveDocPermission(makeAccess({ team: 'VIEW' }), viewer),
93
+ ).toBe('VIEW');
94
+ expect(
95
+ getEffectiveDocPermission(makeAccess({ team: 'NONE' }), viewer),
96
+ ).toBe('NONE');
97
+ });
98
+
99
+ it('defaults a missing policy to team EDIT and a private link', () => {
100
+ const member = makeViewer({ isOwnerOrgMember: true, hasWriteACL: true });
101
+
102
+ expect(getEffectiveDocPermission(null, member)).toBe('EDIT');
103
+ expect(getEffectiveDocPermission(undefined, member)).toBe('EDIT');
104
+ // The same missing policy grants an outsider nothing
105
+ expect(getEffectiveDocPermission(null, makeViewer())).toBe('NONE');
106
+ });
107
+
108
+ it('applies the network policy only to members of a network organization', () => {
109
+ const access = makeAccess({ network: 'EDIT' });
110
+
111
+ expect(
112
+ getEffectiveDocPermission(
113
+ access,
114
+ makeViewer({ isNetworkOrgMember: true }),
115
+ ),
116
+ ).toBe('EDIT');
117
+ expect(
118
+ getEffectiveDocPermission(
119
+ access,
120
+ makeViewer({ isNetworkOrgMember: false }),
121
+ ),
122
+ ).toBe('NONE');
123
+ expect(
124
+ getEffectiveDocPermission(
125
+ makeAccess({ network: 'VIEW' }),
126
+ makeViewer({ isNetworkOrgMember: true }),
127
+ ),
128
+ ).toBe('VIEW');
129
+ });
130
+
131
+ it('never lets the team policy reach anyone outside the owner org', () => {
132
+ // team EDIT is the default policy on every document, so a leak here
133
+ // would expose essentially everything
134
+ expect(
135
+ getEffectiveDocPermission(
136
+ makeAccess({ team: 'EDIT' }),
137
+ makeViewer({ isNetworkOrgMember: true }),
138
+ ),
139
+ ).toBe('NONE');
140
+ });
141
+
142
+ it('gives everyone the public policy once the link is open', () => {
143
+ const access = makeAccess({ public: 'VIEW' });
144
+
145
+ expect(getEffectiveDocPermission(access, makeViewer())).toBe('VIEW');
146
+ expect(
147
+ getEffectiveDocPermission(access, makeViewer({ accountId: null })),
148
+ ).toBe('VIEW');
149
+ expect(
150
+ getEffectiveDocPermission(
151
+ access,
152
+ makeViewer({ isNetworkOrgMember: true }),
153
+ ),
154
+ ).toBe('VIEW');
155
+ });
156
+ });
157
+
158
+ describe('getEffectiveDocPermission public floor', () => {
159
+ it('floors owner-org members at the public permission', () => {
160
+ // A member must never end up with less than an anonymous link visitor:
161
+ // the member can always open the link in a signed-out tab
162
+ const access = makeAccess({ team: 'NONE', public: 'VIEW' });
163
+
164
+ expect(
165
+ getEffectiveDocPermission(
166
+ access,
167
+ makeViewer({ isOwnerOrgMember: true, hasWriteACL: true }),
168
+ ),
169
+ ).toBe('VIEW');
170
+ });
171
+
172
+ it('floors an explicitly blocked grantee at the public permission', () => {
173
+ const access = makeAccess({
174
+ team: 'EDIT',
175
+ public: 'VIEW',
176
+ grants: [makeAccountGrant('100', 'NONE')],
177
+ });
178
+
179
+ expect(
180
+ getEffectiveDocPermission(
181
+ access,
182
+ makeViewer({ isOwnerOrgMember: true, hasWriteACL: true }),
183
+ ),
184
+ ).toBe('VIEW');
185
+ expect(getEffectiveDocPermission(access, makeViewer())).toBe('VIEW');
186
+ });
187
+
188
+ it('never lowers a permission the viewer already earned', () => {
189
+ const access = makeAccess({ team: 'EDIT', public: 'VIEW' });
190
+
191
+ expect(
192
+ getEffectiveDocPermission(
193
+ access,
194
+ makeViewer({ isOwnerOrgMember: true, hasWriteACL: true }),
195
+ ),
196
+ ).toBe('EDIT');
197
+ });
198
+ });
199
+
200
+ describe('getEffectiveDocPermission signed-out downgrade', () => {
201
+ it('caps public EDIT at VIEW for a viewer with no account', () => {
202
+ // Editing has to be attributable to somebody, so an open-to-edit link
203
+ // still only reads for an anonymous visitor
204
+ const access = makeAccess({ public: 'EDIT' });
205
+
206
+ expect(
207
+ getEffectiveDocPermission(access, makeViewer({ accountId: null })),
208
+ ).toBe('VIEW');
209
+ expect(
210
+ getEffectiveDocPermission(access, makeViewer({ accountId: undefined })),
211
+ ).toBe('VIEW');
212
+ });
213
+
214
+ it('keeps public EDIT for a signed-in viewer', () => {
215
+ const access = makeAccess({ public: 'EDIT' });
216
+
217
+ expect(getEffectiveDocPermission(access, makeViewer())).toBe('EDIT');
218
+ });
219
+
220
+ it('caps a signed-out owner-org-flagged viewer at VIEW as well', () => {
221
+ // Defensive: a caller that sets isOwnerOrgMember without an accountId
222
+ // must not route around the downgrade
223
+ const access = makeAccess({ team: 'NONE', public: 'EDIT' });
224
+
225
+ expect(
226
+ getEffectiveDocPermission(
227
+ access,
228
+ makeViewer({
229
+ accountId: null,
230
+ isOwnerOrgMember: true,
231
+ hasWriteACL: true,
232
+ }),
233
+ ),
234
+ ).toBe('VIEW');
235
+ });
236
+ });
237
+
238
+ describe('getEffectiveDocPermission read-only member cap', () => {
239
+ it('caps a member without the org write ACL at VIEW', () => {
240
+ const readOnlyMember = makeViewer({
241
+ isOwnerOrgMember: true,
242
+ hasWriteACL: false,
243
+ });
244
+
245
+ expect(
246
+ getEffectiveDocPermission(makeAccess({ team: 'EDIT' }), readOnlyMember),
247
+ ).toBe('VIEW');
248
+ expect(
249
+ getEffectiveDocPermission(makeAccess({ team: 'VIEW' }), readOnlyMember),
250
+ ).toBe('VIEW');
251
+ });
252
+
253
+ it('does not raise a read-only member above a restrictive team policy', () => {
254
+ const readOnlyMember = makeViewer({
255
+ isOwnerOrgMember: true,
256
+ hasWriteACL: false,
257
+ });
258
+
259
+ expect(
260
+ getEffectiveDocPermission(makeAccess({ team: 'NONE' }), readOnlyMember),
261
+ ).toBe('NONE');
262
+ });
263
+
264
+ it('lets an explicit account grant lift a read-only member to EDIT', () => {
265
+ // A targeted grant is a deliberate elevation, so it outranks the ACL
266
+ // default that produced the cap
267
+ const access = makeAccess({
268
+ team: 'EDIT',
269
+ grants: [makeAccountGrant('100', 'EDIT')],
270
+ });
271
+
272
+ expect(
273
+ getEffectiveDocPermission(
274
+ access,
275
+ makeViewer({ isOwnerOrgMember: true, hasWriteACL: false }),
276
+ ),
277
+ ).toBe('EDIT');
278
+ });
279
+ });
280
+
281
+ describe('getEffectiveDocPermission account grants', () => {
282
+ it('overrides the team policy in both directions', () => {
283
+ const member = makeViewer({ isOwnerOrgMember: true, hasWriteACL: true });
284
+
285
+ expect(
286
+ getEffectiveDocPermission(
287
+ makeAccess({ team: 'VIEW', grants: [makeAccountGrant('100', 'EDIT')] }),
288
+ member,
289
+ ),
290
+ ).toBe('EDIT');
291
+ expect(
292
+ getEffectiveDocPermission(
293
+ makeAccess({ team: 'EDIT', grants: [makeAccountGrant('100', 'VIEW')] }),
294
+ member,
295
+ ),
296
+ ).toBe('VIEW');
297
+ });
298
+
299
+ it('blocks an owner-org member outright with an explicit NONE grant', () => {
300
+ // "no grant" and "NONE grant" are different answers: the second is a
301
+ // deliberate block that has to beat the default team EDIT policy
302
+ const access = makeAccess({
303
+ team: 'EDIT',
304
+ grants: [makeAccountGrant('100', 'NONE')],
305
+ });
306
+
307
+ expect(
308
+ getEffectiveDocPermission(
309
+ access,
310
+ makeViewer({ isOwnerOrgMember: true, hasWriteACL: true }),
311
+ ),
312
+ ).toBe('NONE');
313
+ });
314
+
315
+ it('grants an outsider access with no membership of any kind', () => {
316
+ const access = makeAccess({ grants: [makeAccountGrant('100', 'EDIT')] });
317
+
318
+ expect(getEffectiveDocPermission(access, makeViewer())).toBe('EDIT');
319
+ });
320
+
321
+ it('only matches the grant belonging to this viewer', () => {
322
+ const access = makeAccess({
323
+ team: 'EDIT',
324
+ grants: [makeAccountGrant('999', 'NONE')],
325
+ });
326
+
327
+ expect(
328
+ getEffectiveDocPermission(
329
+ access,
330
+ makeViewer({ isOwnerOrgMember: true, hasWriteACL: true }),
331
+ ),
332
+ ).toBe('EDIT');
333
+ expect(getEffectiveDocPermission(access, makeViewer())).toBe('NONE');
334
+ });
335
+
336
+ it('matches grantee ids across string, number, and bigint spellings', () => {
337
+ // Ids reach this function as strings from GraphQL and as numbers or
338
+ // bigints from Postgres; a strict comparison would silently drop grants
339
+ const viewer = makeViewer({ accountId: 100 });
340
+
341
+ expect(
342
+ getEffectiveDocPermission(
343
+ makeAccess({ grants: [makeAccountGrant('100', 'EDIT')] }),
344
+ viewer,
345
+ ),
346
+ ).toBe('EDIT');
347
+ expect(
348
+ getEffectiveDocPermission(
349
+ makeAccess({ grants: [makeAccountGrant(100n, 'EDIT')] }),
350
+ viewer,
351
+ ),
352
+ ).toBe('EDIT');
353
+ expect(
354
+ getEffectiveDocPermission(
355
+ makeAccess({ grants: [makeAccountGrant(100, 'EDIT')] }),
356
+ makeViewer({ accountId: '100' }),
357
+ ),
358
+ ).toBe('EDIT');
359
+ });
360
+
361
+ it('ignores every grant for a viewer with no account', () => {
362
+ const access = makeAccess({ grants: [makeAccountGrant('100', 'EDIT')] });
363
+
364
+ expect(
365
+ getEffectiveDocPermission(access, makeViewer({ accountId: null })),
366
+ ).toBe('NONE');
367
+ });
368
+
369
+ it('outranks the organization grant for the same viewer', () => {
370
+ const access = makeAccess({
371
+ grants: [makeAccountGrant('100', 'NONE'), makeOrgGrant('7', 'EDIT')],
372
+ });
373
+
374
+ expect(
375
+ getEffectiveDocPermission(
376
+ access,
377
+ makeViewer({ viewerOrganizationIds: ['7'] }),
378
+ ),
379
+ ).toBe('NONE');
380
+ });
381
+ });
382
+
383
+ describe('getEffectiveDocPermission organization grants', () => {
384
+ it('applies only to organizations the viewer actually belongs to', () => {
385
+ const access = makeAccess({ grants: [makeOrgGrant('7', 'EDIT')] });
386
+
387
+ expect(
388
+ getEffectiveDocPermission(
389
+ access,
390
+ makeViewer({ viewerOrganizationIds: ['7'] }),
391
+ ),
392
+ ).toBe('EDIT');
393
+ expect(
394
+ getEffectiveDocPermission(
395
+ access,
396
+ makeViewer({ viewerOrganizationIds: ['8'] }),
397
+ ),
398
+ ).toBe('NONE');
399
+ expect(
400
+ getEffectiveDocPermission(
401
+ access,
402
+ makeViewer({ viewerOrganizationIds: [] }),
403
+ ),
404
+ ).toBe('NONE');
405
+ });
406
+
407
+ it('takes the best permission when the viewer belongs to several granted orgs', () => {
408
+ const access = makeAccess({
409
+ grants: [makeOrgGrant('7', 'VIEW'), makeOrgGrant('8', 'EDIT')],
410
+ });
411
+
412
+ expect(
413
+ getEffectiveDocPermission(
414
+ access,
415
+ makeViewer({ viewerOrganizationIds: ['7', '8'] }),
416
+ ),
417
+ ).toBe('EDIT');
418
+ });
419
+
420
+ it('overrides the network policy in both directions', () => {
421
+ const networkViewer = makeViewer({
422
+ isNetworkOrgMember: true,
423
+ viewerOrganizationIds: ['7'],
424
+ });
425
+
426
+ expect(
427
+ getEffectiveDocPermission(
428
+ makeAccess({ network: 'VIEW', grants: [makeOrgGrant('7', 'EDIT')] }),
429
+ networkViewer,
430
+ ),
431
+ ).toBe('EDIT');
432
+ expect(
433
+ getEffectiveDocPermission(
434
+ makeAccess({ network: 'EDIT', grants: [makeOrgGrant('7', 'NONE')] }),
435
+ networkViewer,
436
+ ),
437
+ ).toBe('NONE');
438
+ });
439
+
440
+ it('matches organization ids across string, number, and bigint spellings', () => {
441
+ expect(
442
+ getEffectiveDocPermission(
443
+ makeAccess({ grants: [makeOrgGrant(7n, 'VIEW')] }),
444
+ makeViewer({ viewerOrganizationIds: ['7'] }),
445
+ ),
446
+ ).toBe('VIEW');
447
+ expect(
448
+ getEffectiveDocPermission(
449
+ makeAccess({ grants: [makeOrgGrant('7', 'VIEW')] }),
450
+ makeViewer({ viewerOrganizationIds: [7] }),
451
+ ),
452
+ ).toBe('VIEW');
453
+ });
454
+
455
+ it('is ignored entirely for owner-org members', () => {
456
+ // Owner-org members are judged by the team policy and their own account
457
+ // grant; an org grant naming the owner org must not re-open a document
458
+ // the team policy closed
459
+ const access = makeAccess({
460
+ team: 'NONE',
461
+ grants: [makeOrgGrant('7', 'EDIT')],
462
+ });
463
+
464
+ expect(
465
+ getEffectiveDocPermission(
466
+ access,
467
+ makeViewer({
468
+ isOwnerOrgMember: true,
469
+ hasWriteACL: true,
470
+ viewerOrganizationIds: ['7'],
471
+ }),
472
+ ),
473
+ ).toBe('NONE');
474
+ });
475
+ });
476
+
477
+ describe('getEffectiveDocPermission owner-org admins and owners', () => {
478
+ const admin = makeViewer({
479
+ isOwnerOrgMember: true,
480
+ isAdminOrOwner: true,
481
+ hasWriteACL: false,
482
+ });
483
+
484
+ it('keeps full access against the most restrictive policy', () => {
485
+ expect(getEffectiveDocPermission(makeAccess({ team: 'NONE' }), admin)).toBe(
486
+ 'EDIT',
487
+ );
488
+ expect(getEffectiveDocPermission(makeAccess({ team: 'VIEW' }), admin)).toBe(
489
+ 'EDIT',
490
+ );
491
+ });
492
+
493
+ it('keeps full access against an explicit NONE grant', () => {
494
+ // Somebody who can read the row in the database must not be lockable
495
+ // out of it through the sharing UI
496
+ const access = makeAccess({
497
+ team: 'NONE',
498
+ grants: [makeAccountGrant('100', 'NONE'), makeOrgGrant('7', 'NONE')],
499
+ });
500
+
501
+ expect(
502
+ getEffectiveDocPermission(access, {
503
+ ...admin,
504
+ viewerOrganizationIds: ['7'],
505
+ }),
506
+ ).toBe('EDIT');
507
+ });
508
+
509
+ it('keeps full access without the org write ACL', () => {
510
+ expect(getEffectiveDocPermission(makeAccess({ team: 'EDIT' }), admin)).toBe(
511
+ 'EDIT',
512
+ );
513
+ });
514
+
515
+ it('does not extend to admins of some other organization', () => {
516
+ // isAdminOrOwner is always resolved against the OWNER org; being an
517
+ // admin somewhere else must grant nothing here
518
+ expect(
519
+ getEffectiveDocPermission(
520
+ makeAccess(),
521
+ makeViewer({ isOwnerOrgMember: false, isAdminOrOwner: true }),
522
+ ),
523
+ ).toBe('NONE');
524
+ });
525
+ });