@jskit-ai/workspaces-core 0.2.15 → 0.2.17

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 (61) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/locales/en.json +0 -5
  3. package/package.json +26 -16
  4. package/patterns/workspace-server/PATTERN.md +9 -9
  5. package/src/server/WorkspacesFeature.js +28 -16
  6. package/src/server/WorkspacesIntegrationsProvider.js +5 -6
  7. package/src/server/common/repositories/workspaceInvitesRepository.js +69 -123
  8. package/src/server/common/repositories/workspaceMembershipsRepository.js +92 -146
  9. package/src/server/common/repositories/workspacesRepository.js +90 -113
  10. package/src/server/common/services/workspaceDirectoryService.js +39 -19
  11. package/src/server/common/support/realtimeServiceEvents.js +19 -17
  12. package/src/server/support/resolveWorkspace.js +1 -1
  13. package/src/server/support/workspaceCrud.js +1 -1
  14. package/src/server/types.js +14 -3
  15. package/src/server/workspaceDirectory/workspaceDirectoryActions.js +7 -7
  16. package/src/server/workspaceDirectory/workspaceDirectoryRoutes.js +6 -6
  17. package/src/server/workspaceMembers/workspaceMembersActions.js +31 -30
  18. package/src/server/workspaceMembers/workspaceMembersRoutes.js +10 -10
  19. package/src/server/workspaceMembers/workspaceMembersService.js +35 -24
  20. package/src/server/workspacePendingInvitations/workspacePendingInvitationsActions.js +4 -4
  21. package/src/server/workspacePendingInvitations/workspacePendingInvitationsRoutes.js +7 -7
  22. package/src/server/workspacePendingInvitations/workspacePendingInvitationsService.js +8 -11
  23. package/src/server/workspaceSettings/workspaceSettingsActions.js +4 -4
  24. package/src/server/workspaceSettings/workspaceSettingsRepository.js +19 -50
  25. package/src/server/workspaceSettings/workspaceSettingsRoutes.js +4 -4
  26. package/src/server/workspacesRuntime.js +40 -47
  27. package/src/shared/resources/workspaceAssistantOutputs.js +1 -1
  28. package/src/shared/resources/workspaceInvitesResource.js +16 -73
  29. package/src/shared/resources/workspaceMembersResource.js +3 -3
  30. package/src/shared/resources/workspaceMembershipsResource.js +14 -48
  31. package/src/shared/resources/workspacePendingInvitationsResource.js +3 -3
  32. package/src/shared/resources/workspaceResource.js +21 -17
  33. package/src/shared/resources/workspaceSettingsResource.js +17 -24
  34. package/src/test/WorkspaceRecordsFeature.js +27 -0
  35. package/test/exportsContract.test.js +2 -0
  36. package/test/featureRuntime.test.js +34 -41
  37. package/test/repositories.integration.test.js +272 -41
  38. package/test/repositoryContracts.test.js +52 -40
  39. package/test/resourcesCanonical.test.js +41 -38
  40. package/test/roleLabels.test.js +10 -10
  41. package/test/support/invitationTransactions.js +49 -47
  42. package/test/support/mysqlWorkspaces.js +129 -0
  43. package/test/usersRouteResources.test.js +17 -17
  44. package/test/workspaceDirectoryService.mysql.test.js +255 -0
  45. package/test/workspaceInvitesRepository.mysql.test.js +355 -0
  46. package/test/workspaceMembersResource.test.js +2 -2
  47. package/test/workspaceMembersService.mysql.test.js +223 -0
  48. package/test/workspaceMembersService.test.js +10 -10
  49. package/test/workspacePendingInvitationsResource.test.js +6 -6
  50. package/test/workspacePendingInvitationsService.mysql.test.js +292 -0
  51. package/test/workspacePendingInvitationsService.test.js +2 -2
  52. package/test/workspaceRecords.test.js +142 -0
  53. package/test/workspaceRecordsFeature.test.js +42 -0
  54. package/test/workspaceService.test.js +2 -2
  55. package/test/workspaceSettingsActions.test.js +26 -21
  56. package/test/workspaceSettingsRepository.test.js +66 -86
  57. package/test/workspaceSettingsResource.test.js +5 -5
  58. package/test/workspaceInternalCrudResources.test.js +0 -98
  59. package/test/workspaceInvitesRepository.test.js +0 -318
  60. package/test/workspaceMembershipsRepository.test.js +0 -329
  61. package/test/workspacesRepository.test.js +0 -410
@@ -8,13 +8,8 @@ function normalizeWorkspaceColor(value) {
8
8
  return typeof value === "string" ? value.toUpperCase() : value;
9
9
  }
10
10
 
11
- function asCollectionDocument(rows = []) {
12
- return {
13
- data: Array.isArray(rows) ? rows : []
14
- };
15
- }
16
-
17
- function createWorkspaceSettingsApiStub(rowOverrides = {}) {
11
+ // The workspace_settings handle, over one stored row.
12
+ function createWorkspaceSettingsHandleStub(rowOverrides = {}) {
18
13
  const DEFAULT_WORKSPACE_THEME = resolveWorkspaceThemePalettes({});
19
14
  const STUB_CREATED_AT = "2026-03-09 00:26:35.710";
20
15
  const STUB_CREATED_AT_ISO = toIsoString(STUB_CREATED_AT);
@@ -39,85 +34,70 @@ function createWorkspaceSettingsApiStub(rowOverrides = {}) {
39
34
  }
40
35
  };
41
36
 
42
- const api = {
43
- resources: {
44
- workspaceSettings: {
45
- async query({ queryParams, format }) {
46
- assert.equal(format, "plain");
47
- const id = String(queryParams?.filters?.id || "");
48
- if (!state.row || (id && String(state.row.id) !== id)) {
49
- return asCollectionDocument([]);
50
- }
51
-
52
- return asCollectionDocument([{
53
- ...state.row,
54
- id: String(state.row.id)
55
- }]);
56
- },
57
- async post(payload) {
58
- const data = payload?.data || {};
59
- assert.equal(payload.format, "plain");
60
- assert.equal(payload.returning, "full");
61
- assert.equal(payload.inputRecord, undefined);
62
- assert.equal(payload.document, undefined);
63
- state.postPayload = payload;
64
- state.row = {
65
- id: String(data.id),
66
- lightPrimaryColor: normalizeWorkspaceColor(data.lightPrimaryColor ?? DEFAULT_WORKSPACE_THEME.light.color),
67
- lightSecondaryColor: normalizeWorkspaceColor(data.lightSecondaryColor ?? DEFAULT_WORKSPACE_THEME.light.secondaryColor),
68
- lightSurfaceColor: normalizeWorkspaceColor(data.lightSurfaceColor ?? DEFAULT_WORKSPACE_THEME.light.surfaceColor),
69
- lightSurfaceVariantColor: normalizeWorkspaceColor(data.lightSurfaceVariantColor ?? DEFAULT_WORKSPACE_THEME.light.surfaceVariantColor),
70
- darkPrimaryColor: normalizeWorkspaceColor(data.darkPrimaryColor ?? DEFAULT_WORKSPACE_THEME.dark.color),
71
- darkSecondaryColor: normalizeWorkspaceColor(data.darkSecondaryColor ?? DEFAULT_WORKSPACE_THEME.dark.secondaryColor),
72
- darkSurfaceColor: normalizeWorkspaceColor(data.darkSurfaceColor ?? DEFAULT_WORKSPACE_THEME.dark.surfaceColor),
73
- darkSurfaceVariantColor: normalizeWorkspaceColor(data.darkSurfaceVariantColor ?? DEFAULT_WORKSPACE_THEME.dark.surfaceVariantColor),
74
- invitesEnabled: data.invitesEnabled ?? true,
75
- createdAt: toIsoString("2026-03-10 00:00:00.000"),
76
- updatedAt: toIsoString("2026-03-10 00:00:00.000")
77
- };
78
- return {
79
- ...state.row,
80
- id: String(state.row.id)
81
- };
82
- },
83
- async patch(payload) {
84
- const data = payload?.data || {};
85
- assert.equal(payload.format, "plain");
86
- assert.equal(payload.returning, "full");
87
- assert.equal(payload.inputRecord, undefined);
88
- assert.equal(payload.document, undefined);
89
- state.patchPayload = payload;
90
- state.row = {
91
- ...state.row,
92
- ...data,
93
- ...(Object.hasOwn(data, "lightPrimaryColor") ? { lightPrimaryColor: normalizeWorkspaceColor(data.lightPrimaryColor) } : {}),
94
- ...(Object.hasOwn(data, "lightSecondaryColor") ? { lightSecondaryColor: normalizeWorkspaceColor(data.lightSecondaryColor) } : {}),
95
- ...(Object.hasOwn(data, "lightSurfaceColor") ? { lightSurfaceColor: normalizeWorkspaceColor(data.lightSurfaceColor) } : {}),
96
- ...(Object.hasOwn(data, "lightSurfaceVariantColor") ? { lightSurfaceVariantColor: normalizeWorkspaceColor(data.lightSurfaceVariantColor) } : {}),
97
- ...(Object.hasOwn(data, "darkPrimaryColor") ? { darkPrimaryColor: normalizeWorkspaceColor(data.darkPrimaryColor) } : {}),
98
- ...(Object.hasOwn(data, "darkSecondaryColor") ? { darkSecondaryColor: normalizeWorkspaceColor(data.darkSecondaryColor) } : {}),
99
- ...(Object.hasOwn(data, "darkSurfaceColor") ? { darkSurfaceColor: normalizeWorkspaceColor(data.darkSurfaceColor) } : {}),
100
- ...(Object.hasOwn(data, "darkSurfaceVariantColor") ? { darkSurfaceVariantColor: normalizeWorkspaceColor(data.darkSurfaceVariantColor) } : {}),
101
- id: String(payload.id || state.row?.id || "")
102
- };
103
- return {
104
- ...state.row,
105
- id: String(state.row.id)
106
- };
107
- }
37
+ const workspaceSettings = {
38
+ async query({ filters, page }) {
39
+ assert.equal(page.size, 1);
40
+ const id = String(filters?.id || "");
41
+ if (!state.row || (id && String(state.row.id) !== id)) {
42
+ return { records: [], page: { size: 1, hasMore: false, next: null } };
108
43
  }
44
+
45
+ return {
46
+ records: [{
47
+ ...state.row,
48
+ id: String(state.row.id)
49
+ }],
50
+ page: { size: 1, hasMore: false, next: null }
51
+ };
52
+ },
53
+ async create(data, options) {
54
+ assert.equal(options.returning, "none");
55
+ state.postPayload = { data };
56
+ state.row = {
57
+ id: String(data.id),
58
+ lightPrimaryColor: normalizeWorkspaceColor(data.lightPrimaryColor ?? DEFAULT_WORKSPACE_THEME.light.color),
59
+ lightSecondaryColor: normalizeWorkspaceColor(data.lightSecondaryColor ?? DEFAULT_WORKSPACE_THEME.light.secondaryColor),
60
+ lightSurfaceColor: normalizeWorkspaceColor(data.lightSurfaceColor ?? DEFAULT_WORKSPACE_THEME.light.surfaceColor),
61
+ lightSurfaceVariantColor: normalizeWorkspaceColor(data.lightSurfaceVariantColor ?? DEFAULT_WORKSPACE_THEME.light.surfaceVariantColor),
62
+ darkPrimaryColor: normalizeWorkspaceColor(data.darkPrimaryColor ?? DEFAULT_WORKSPACE_THEME.dark.color),
63
+ darkSecondaryColor: normalizeWorkspaceColor(data.darkSecondaryColor ?? DEFAULT_WORKSPACE_THEME.dark.secondaryColor),
64
+ darkSurfaceColor: normalizeWorkspaceColor(data.darkSurfaceColor ?? DEFAULT_WORKSPACE_THEME.dark.surfaceColor),
65
+ darkSurfaceVariantColor: normalizeWorkspaceColor(data.darkSurfaceVariantColor ?? DEFAULT_WORKSPACE_THEME.dark.surfaceVariantColor),
66
+ invitesEnabled: data.invitesEnabled ?? true,
67
+ createdAt: toIsoString("2026-03-10 00:00:00.000"),
68
+ updatedAt: toIsoString("2026-03-10 00:00:00.000")
69
+ };
70
+ return null;
71
+ },
72
+ async patch(id, data, options) {
73
+ assert.equal(options.returning, "none");
74
+ state.patchPayload = { id, data };
75
+ state.row = {
76
+ ...state.row,
77
+ ...data,
78
+ ...(Object.hasOwn(data, "lightPrimaryColor") ? { lightPrimaryColor: normalizeWorkspaceColor(data.lightPrimaryColor) } : {}),
79
+ ...(Object.hasOwn(data, "lightSecondaryColor") ? { lightSecondaryColor: normalizeWorkspaceColor(data.lightSecondaryColor) } : {}),
80
+ ...(Object.hasOwn(data, "lightSurfaceColor") ? { lightSurfaceColor: normalizeWorkspaceColor(data.lightSurfaceColor) } : {}),
81
+ ...(Object.hasOwn(data, "lightSurfaceVariantColor") ? { lightSurfaceVariantColor: normalizeWorkspaceColor(data.lightSurfaceVariantColor) } : {}),
82
+ ...(Object.hasOwn(data, "darkPrimaryColor") ? { darkPrimaryColor: normalizeWorkspaceColor(data.darkPrimaryColor) } : {}),
83
+ ...(Object.hasOwn(data, "darkSecondaryColor") ? { darkSecondaryColor: normalizeWorkspaceColor(data.darkSecondaryColor) } : {}),
84
+ ...(Object.hasOwn(data, "darkSurfaceColor") ? { darkSurfaceColor: normalizeWorkspaceColor(data.darkSurfaceColor) } : {}),
85
+ ...(Object.hasOwn(data, "darkSurfaceVariantColor") ? { darkSurfaceVariantColor: normalizeWorkspaceColor(data.darkSurfaceVariantColor) } : {}),
86
+ id: String(id || state.row?.id || "")
87
+ };
88
+ return null;
109
89
  }
110
90
  };
111
91
 
112
- return { api, state, DEFAULT_WORKSPACE_THEME, STUB_CREATED_AT };
92
+ return { workspaceSettings, state, DEFAULT_WORKSPACE_THEME, STUB_CREATED_AT };
113
93
  }
114
94
 
115
95
  test("workspaceSettingsRepository.findByWorkspaceId returns the canonical workspace-settings row", async () => {
116
- const { api, DEFAULT_WORKSPACE_THEME, STUB_CREATED_AT } = createWorkspaceSettingsApiStub();
96
+ const { workspaceSettings, DEFAULT_WORKSPACE_THEME, STUB_CREATED_AT } = createWorkspaceSettingsHandleStub();
117
97
  const repository = createRepository({
118
98
  defaultInvitesEnabled: true,
119
99
  defaultColorSettings: {},
120
- api
100
+ workspaceSettings
121
101
  });
122
102
 
123
103
  const record = await repository.findByWorkspaceId("1");
@@ -139,11 +119,11 @@ test("workspaceSettingsRepository.findByWorkspaceId returns the canonical worksp
139
119
  });
140
120
 
141
121
  test("workspaceSettingsRepository.updateSettingsByWorkspaceId updates invitesEnabled only", async () => {
142
- const { api, state } = createWorkspaceSettingsApiStub();
122
+ const { workspaceSettings, state } = createWorkspaceSettingsHandleStub();
143
123
  const repository = createRepository({
144
124
  defaultInvitesEnabled: true,
145
125
  defaultColorSettings: {},
146
- api
126
+ workspaceSettings
147
127
  });
148
128
 
149
129
  const updated = await repository.updateSettingsByWorkspaceId("1", {
@@ -155,12 +135,12 @@ test("workspaceSettingsRepository.updateSettingsByWorkspaceId updates invitesEna
155
135
  });
156
136
 
157
137
  test("workspaceSettingsRepository.ensureForWorkspaceId delegates defaults to the resource create path", async () => {
158
- const { api, state, DEFAULT_WORKSPACE_THEME } = createWorkspaceSettingsApiStub();
138
+ const { workspaceSettings, state, DEFAULT_WORKSPACE_THEME } = createWorkspaceSettingsHandleStub();
159
139
  state.row = null;
160
140
  const repository = createRepository({
161
141
  defaultInvitesEnabled: true,
162
142
  defaultColorSettings: {},
163
- api
143
+ workspaceSettings
164
144
  });
165
145
 
166
146
  const record = await repository.ensureForWorkspaceId("5");
@@ -188,11 +168,11 @@ test("workspaceSettingsRepository.ensureForWorkspaceId delegates defaults to the
188
168
  });
189
169
 
190
170
  test("workspaceSettingsRepository.ensureForWorkspaceId starts new workspaces from the app's default colors", async () => {
191
- const { api, state, DEFAULT_WORKSPACE_THEME } = createWorkspaceSettingsApiStub();
171
+ const { workspaceSettings, state, DEFAULT_WORKSPACE_THEME } = createWorkspaceSettingsHandleStub();
192
172
  state.row = null;
193
173
  const repository = createRepository({
194
174
  defaultInvitesEnabled: true,
195
- api,
175
+ workspaceSettings,
196
176
  defaultColorSettings: {
197
177
  lightPrimaryColor: "#1C1C1C",
198
178
  darkPrimaryColor: "#F5F5F5"
@@ -208,11 +188,11 @@ test("workspaceSettingsRepository.ensureForWorkspaceId starts new workspaces fro
208
188
  });
209
189
 
210
190
  test("workspaceSettingsRepository.updateSettingsByWorkspaceId updates workspace settings fields", async () => {
211
- const { api, state } = createWorkspaceSettingsApiStub();
191
+ const { workspaceSettings, state } = createWorkspaceSettingsHandleStub();
212
192
  const repository = createRepository({
213
193
  defaultInvitesEnabled: true,
214
194
  defaultColorSettings: {},
215
- api
195
+ workspaceSettings
216
196
  });
217
197
 
218
198
  const updated = await repository.updateSettingsByWorkspaceId("1", {
@@ -224,12 +204,12 @@ test("workspaceSettingsRepository.updateSettingsByWorkspaceId updates workspace
224
204
  });
225
205
 
226
206
  test("workspaceSettingsRepository can be constructed without validating app config shape", () => {
227
- const { api } = createWorkspaceSettingsApiStub();
207
+ const { workspaceSettings } = createWorkspaceSettingsHandleStub();
228
208
 
229
209
  const repository = createRepository({
230
210
  defaultInvitesEnabled: true,
231
211
  defaultColorSettings: {},
232
- api
212
+ workspaceSettings
233
213
  });
234
214
 
235
215
  assert.ok(repository);
@@ -5,7 +5,7 @@ import { resolveSchemaTransportSchemaDefinition } from "@jskit-ai/kernel/shared/
5
5
  import { resolveWorkspaceThemePalettes } from "@jskit-ai/workspaces-core/shared/settings";
6
6
  import {
7
7
  WORKSPACE_SETTINGS_FIELD_KEYS,
8
- workspaceSettingsResource
8
+ workspaceSettingsCrud
9
9
  } from "../src/shared/resources/workspaceSettingsResource.js";
10
10
  import { createWorkspaceRoleCatalog } from "../src/shared/roles.js";
11
11
 
@@ -50,7 +50,7 @@ function parseBody(operation, payload = {}) {
50
50
  }
51
51
 
52
52
  test("workspace settings patch body validates valid payload without reshaping it", async () => {
53
- const parsed = await parseBody(workspaceSettingsResource.operations.patch, {
53
+ const parsed = await parseBody(workspaceSettingsCrud.operations.patch, {
54
54
  lightPrimaryColor: "#0f6b54",
55
55
  lightSecondaryColor: "#0b4d3c",
56
56
  lightSurfaceColor: "#eef5f3",
@@ -78,7 +78,7 @@ test("workspace settings patch body validates valid payload without reshaping it
78
78
  });
79
79
 
80
80
  test("workspace settings patch body rejects unknown fields", async () => {
81
- const parsed = await parseBody(workspaceSettingsResource.operations.patch, {
81
+ const parsed = await parseBody(workspaceSettingsCrud.operations.patch, {
82
82
  avatarUrl: "https://example.com/avatar.png"
83
83
  });
84
84
 
@@ -87,7 +87,7 @@ test("workspace settings patch body rejects unknown fields", async () => {
87
87
  });
88
88
 
89
89
  test("workspace settings create body requires full-write fields", async () => {
90
- const parsed = await parseBody(workspaceSettingsResource.operations.create, {});
90
+ const parsed = await parseBody(workspaceSettingsCrud.operations.create, {});
91
91
 
92
92
  assert.equal(parsed.ok, false);
93
93
  assert.equal(parsed.fieldErrors.lightPrimaryColor, "Light primary color is required.");
@@ -102,7 +102,7 @@ test("workspace settings create body requires full-write fields", async () => {
102
102
  });
103
103
 
104
104
  test("workspace settings output schema accepts already-shaped service payloads", () => {
105
- const outputSchema = resolveSchemaTransportSchemaDefinition(workspaceSettingsResource.operations.view.output, {
105
+ const outputSchema = resolveSchemaTransportSchemaDefinition(workspaceSettingsCrud.operations.view.output, {
106
106
  context: "workspaceSettings.view.output",
107
107
  defaultMode: "replace"
108
108
  });
@@ -1,98 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import { validateOperationSection } from "@jskit-ai/http-runtime/shared/validators/operationValidation";
4
- import { resolveSchemaTransportSchemaDefinition } from "@jskit-ai/kernel/shared/validators";
5
- import { workspaceInvitesResource } from "../src/shared/resources/workspaceInvitesResource.js";
6
- import { workspaceMembershipsResource } from "../src/shared/resources/workspaceMembershipsResource.js";
7
-
8
- function parseBody(operation, payload = {}) {
9
- return validateOperationSection({
10
- operation,
11
- section: "body",
12
- value: payload
13
- });
14
- }
15
-
16
- test("workspace internal CRUD resources expose canonical derived operation sets", () => {
17
- const resources = {
18
- workspaceMemberships: workspaceMembershipsResource,
19
- workspaceInvites: workspaceInvitesResource
20
- };
21
-
22
- for (const [label, resource] of Object.entries(resources)) {
23
- assert.deepEqual(Object.keys(resource.operations), ["list", "view", "create", "patch"]);
24
- assert.equal(typeof resource.operations.create.body?.schema, "object", `${label}.operations.create.body.schema is required.`);
25
- assert.equal(typeof resource.operations.patch.body?.schema, "object", `${label}.operations.patch.body.schema is required.`);
26
- assert.equal(
27
- typeof resolveSchemaTransportSchemaDefinition(resource.operations.view.output, {
28
- context: `${label}.operations.view.output`,
29
- defaultMode: "replace"
30
- }),
31
- "object",
32
- `${label}.operations.view.output transport schema is required.`
33
- );
34
- }
35
- });
36
-
37
- test("workspace memberships derived bodies accept normalized internal writes", async () => {
38
- const create = await parseBody(workspaceMembershipsResource.operations.create, {
39
- workspaceId: "7",
40
- userId: "9",
41
- roleSid: "OWNER",
42
- status: "ACTIVE",
43
- createdAt: "2026-05-02T10:11:12.000Z",
44
- updatedAt: "2026-05-02T10:11:12.000Z"
45
- });
46
- assert.equal(create.ok, true);
47
- assert.equal(String(create.value.workspaceId), "7");
48
- assert.equal(String(create.value.userId), "9");
49
- assert.equal(create.value.roleSid, "OWNER");
50
- assert.equal(create.value.status, "ACTIVE");
51
- assert.equal(create.value.createdAt, "2026-05-02T10:11:12.000Z");
52
- assert.equal(create.value.updatedAt, "2026-05-02T10:11:12.000Z");
53
-
54
- const patch = await parseBody(workspaceMembershipsResource.operations.patch, {
55
- roleSid: "ADMIN",
56
- status: "ACTIVE",
57
- updatedAt: "2026-05-02T10:11:12.000Z"
58
- });
59
- assert.equal(patch.ok, true);
60
- assert.equal(patch.value.roleSid, "ADMIN");
61
- assert.equal(patch.value.status, "ACTIVE");
62
- assert.equal(patch.value.updatedAt, "2026-05-02T10:11:12.000Z");
63
- });
64
-
65
- test("workspace invites derived bodies keep lifecycle fields available for internal writes", async () => {
66
- const create = await parseBody(workspaceInvitesResource.operations.create, {
67
- workspaceId: "7",
68
- email: "Invitee@Example.com",
69
- roleSid: "ADMIN",
70
- status: "PENDING",
71
- tokenHash: " invite-token-hash ",
72
- invitedByUserId: "9",
73
- expiresAt: "2026-05-10T00:00:00.000Z",
74
- acceptedAt: null,
75
- revokedAt: null,
76
- createdAt: "2026-05-02T10:11:12.000Z",
77
- updatedAt: "2026-05-02T10:11:12.000Z"
78
- });
79
- assert.equal(create.ok, true);
80
- assert.equal(String(create.value.workspaceId), "7");
81
- assert.equal(create.value.email, "Invitee@Example.com");
82
- assert.equal(create.value.roleSid, "ADMIN");
83
- assert.equal(create.value.status, "PENDING");
84
- assert.equal(create.value.tokenHash, "invite-token-hash");
85
- assert.equal(String(create.value.invitedByUserId), "9");
86
- assert.equal(create.value.createdAt, "2026-05-02T10:11:12.000Z");
87
- assert.equal(create.value.updatedAt, "2026-05-02T10:11:12.000Z");
88
-
89
- const patch = await parseBody(workspaceInvitesResource.operations.patch, {
90
- status: "ACCEPTED",
91
- acceptedAt: "2026-05-11T00:00:00.000Z",
92
- updatedAt: "2026-05-11T00:00:00.000Z"
93
- });
94
- assert.equal(patch.ok, true);
95
- assert.equal(patch.value.status, "ACCEPTED");
96
- assert.equal(patch.value.acceptedAt, "2026-05-11T00:00:00.000Z");
97
- assert.equal(patch.value.updatedAt, "2026-05-11T00:00:00.000Z");
98
- });
@@ -1,318 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import { toIsoString } from "@jskit-ai/database-runtime/shared";
4
- import { createRepository } from "../src/server/common/repositories/workspaceInvitesRepository.js";
5
-
6
- function asCollectionDocument(rows = []) {
7
- return {
8
- data: Array.isArray(rows) ? rows : []
9
- };
10
- }
11
-
12
- function toWorkspaceInviteRow(row = {}) {
13
- return {
14
- id: String(row.id || ""),
15
- workspaceId: row?.workspace?.id == null ? null : String(row.workspace.id),
16
- workspace: row?.workspace?.id == null ? null : {
17
- ...row.workspace,
18
- id: String(row.workspace.id)
19
- },
20
- email: row.email,
21
- roleSid: row.roleSid,
22
- status: row.status,
23
- tokenHash: row.tokenHash,
24
- invitedByUserId: row?.invitedByUser?.id == null ? null : String(row.invitedByUser.id),
25
- invitedByUser: row?.invitedByUser?.id == null ? null : {
26
- ...row.invitedByUser,
27
- id: String(row.invitedByUser.id)
28
- },
29
- expiresAt: row.expiresAt,
30
- acceptedAt: row.acceptedAt,
31
- revokedAt: row.revokedAt,
32
- createdAt: row.createdAt,
33
- updatedAt: row.updatedAt
34
- };
35
- }
36
-
37
- function createWorkspaceInvitesApiStub({
38
- rows = [],
39
- rowById = new Map()
40
- } = {}) {
41
- const state = {
42
- postPayload: null,
43
- patchPayloads: []
44
- };
45
-
46
- const trx = () => ({
47
- where() { return this; },
48
- select() { return this; },
49
- async first() { return { id: "1" }; },
50
- forUpdate() { return this; },
51
- then(resolve) { return Promise.resolve([...rowById.values()].filter((row) => row.status === "pending")).then(resolve); }
52
- });
53
- trx.isCompleted = () => false;
54
- const api = {
55
- async transaction(work) { return work(trx); },
56
- resources: {
57
- workspaceInvites: {
58
- vars: { queryMaxLimit: 100 },
59
- async query({ queryParams, format }) {
60
- assert.equal(format, "plain");
61
- const filters = queryParams?.filters || {};
62
- const matching = (rows.length ? rows : [...rowById.values()]).filter((row) => {
63
- if (Object.hasOwn(filters, "id") && String(row.id) !== String(filters.id)) {
64
- return false;
65
- }
66
- if (Object.hasOwn(filters, "workspace") && String(row?.workspace?.id || "") !== String(filters.workspace)) {
67
- return false;
68
- }
69
- if (Object.hasOwn(filters, "email") && String(row.email || "") !== String(filters.email)) {
70
- return false;
71
- }
72
- if (Object.hasOwn(filters, "status") && String(row.status || "") !== String(filters.status)) {
73
- return false;
74
- }
75
- if (Object.hasOwn(filters, "tokenHash") && String(row.tokenHash || "") !== String(filters.tokenHash)) {
76
- return false;
77
- }
78
- return true;
79
- });
80
-
81
- return asCollectionDocument(matching.map((row) => toWorkspaceInviteRow(row)));
82
- },
83
- async post(payload) {
84
- const data = payload?.data || {};
85
- assert.equal(payload.format, "plain");
86
- assert.equal(payload.returning, "full");
87
- assert.equal(payload.inputRecord, undefined);
88
- assert.equal(payload.document, undefined);
89
- state.postPayload = payload;
90
- const row = {
91
- id: "1",
92
- workspace: { id: String(data.workspace || "") },
93
- email: data.email,
94
- roleSid: data.roleSid,
95
- status: data.status,
96
- tokenHash: data.tokenHash,
97
- invitedByUser: data.invitedByUser
98
- ? { id: String(data.invitedByUser) }
99
- : null,
100
- expiresAt: data.expiresAt,
101
- acceptedAt: data.acceptedAt,
102
- revokedAt: data.revokedAt,
103
- createdAt: "2026-03-09 00:26:35.710",
104
- updatedAt: "2026-03-09 00:26:35.710"
105
- };
106
- rows.push(row);
107
- rowById.set("1", row);
108
- return toWorkspaceInviteRow(row);
109
- },
110
- async patch(payload) {
111
- const data = payload?.data || {};
112
- assert.equal(payload.format, "plain");
113
- assert.equal(payload.returning, "full");
114
- assert.equal(payload.inputRecord, undefined);
115
- assert.equal(payload.document, undefined);
116
- state.patchPayloads.push(payload);
117
- const existing = rowById.get(String(payload.id));
118
- if (existing) {
119
- const updated = {
120
- ...existing,
121
- ...data
122
- };
123
- rowById.set(String(payload.id), updated);
124
- }
125
- const updatedRow = rowById.get(String(payload.id)) || null;
126
- return updatedRow ? toWorkspaceInviteRow(updatedRow) : null;
127
- }
128
- }
129
- }
130
- };
131
-
132
- return { api, state };
133
- }
134
-
135
- test("workspaceInvitesRepository.insert preserves expiresAt and relationship fields through the resource write path", async () => {
136
- const { api, state } = createWorkspaceInvitesApiStub();
137
- const repository = createRepository({ api });
138
-
139
- await repository.insert({
140
- workspaceId: "1",
141
- email: "invitee@example.com",
142
- roleSid: "member",
143
- status: "pending",
144
- tokenHash: "hash",
145
- invitedByUserId: "1",
146
- expiresAt: "2026-03-16T00:26:35.709Z"
147
- });
148
-
149
- assert.equal(state.postPayload.data.workspace, "1");
150
- assert.equal(state.postPayload.data.email, "invitee@example.com");
151
- assert.equal(state.postPayload.data.invitedByUser, "1");
152
- assert.equal(state.postPayload.data.tokenHash, "hash");
153
- assert.equal(typeof state.postPayload.data.expiresAt, "string");
154
- });
155
-
156
- test("workspaceInvitesRepository.insert recovers a lost duplicate race with the newest pending invite by id, 10 after 9", async () => {
157
- const pendingInvite = (id) => ({
158
- id,
159
- workspace: { id: "7" },
160
- email: "ada@example.com",
161
- roleSid: "member",
162
- status: "pending",
163
- tokenHash: `token-${id}`
164
- });
165
- // In id order, as the resource returns them.
166
- const { api } = createWorkspaceInvitesApiStub({ rows: [pendingInvite("9"), pendingInvite("10")] });
167
- api.resources.workspaceInvites.post = async () => {
168
- throw Object.assign(new Error("Duplicate invite"), {
169
- transactionOutcome: "rolledBack",
170
- cause: { code: "ER_DUP_ENTRY" }
171
- });
172
- };
173
- const repository = createRepository({ api });
174
-
175
- const invite = await repository.insert({ workspaceId: "7", email: "ada@example.com", tokenHash: "token-new" });
176
-
177
- assert.equal(invite.id, "10");
178
- });
179
-
180
- test("workspaceInvitesRepository.findPendingByTokenHash reads from the canonical invite resource without workspace data", async () => {
181
- const { api } = createWorkspaceInvitesApiStub({
182
- rows: [
183
- {
184
- id: "44",
185
- workspace: { id: "9" },
186
- email: "invitee@example.com",
187
- roleSid: "member",
188
- status: "pending",
189
- tokenHash: "hash-token",
190
- invitedByUser: { id: "1" },
191
- expiresAt: "2030-01-01 00:00:00.000",
192
- acceptedAt: null,
193
- revokedAt: null,
194
- createdAt: "2026-03-09 00:26:35.710",
195
- updatedAt: "2026-03-09 00:26:35.710"
196
- }
197
- ]
198
- });
199
- const repository = createRepository({ api });
200
-
201
- const invite = await repository.findPendingByTokenHash("hash-token");
202
-
203
- assert.equal(invite?.workspaceId, "9");
204
- assert.equal(invite?.workspaceSlug, undefined);
205
- });
206
-
207
- test("workspaceInvitesRepository.findByTokenHashWithWorkspace preserves workspace summary fields for public invite resolution", async () => {
208
- const { api } = createWorkspaceInvitesApiStub({
209
- rows: [
210
- {
211
- id: "45",
212
- workspace: {
213
- id: "9",
214
- slug: "tony3",
215
- name: "Tony3",
216
- avatarUrl: "https://example.com/avatar.png"
217
- },
218
- email: "invitee@example.com",
219
- roleSid: "member",
220
- status: "accepted",
221
- tokenHash: "hash-token",
222
- invitedByUser: { id: "1" },
223
- expiresAt: "2030-01-01 00:00:00.000",
224
- acceptedAt: "2026-03-10 00:00:00.000",
225
- revokedAt: null,
226
- createdAt: "2026-03-09 00:26:35.710",
227
- updatedAt: "2026-03-10 00:00:00.000"
228
- }
229
- ]
230
- });
231
- const repository = createRepository({ api });
232
-
233
- const invite = await repository.findByTokenHashWithWorkspace("hash-token");
234
-
235
- assert.equal(invite?.id, "45");
236
- assert.equal(invite?.status, "accepted");
237
- assert.equal(invite?.workspaceSlug, "tony3");
238
- assert.equal(invite?.workspaceName, "Tony3");
239
- assert.equal(invite?.workspaceAvatarUrl, "https://example.com/avatar.png");
240
- });
241
-
242
- test("workspaceInvitesRepository.markAcceptedById uses the internal invite resource for normalized patch writes", async () => {
243
- const { api, state } = createWorkspaceInvitesApiStub({
244
- rowById: new Map([
245
- ["1", {
246
- id: "1",
247
- workspace: { id: "1" },
248
- email: "invitee@example.com",
249
- roleSid: "member",
250
- status: "pending",
251
- tokenHash: "hash",
252
- invitedByUser: { id: "1" },
253
- expiresAt: "2026-03-16 00:26:35.709",
254
- acceptedAt: "2026-03-10 00:26:35.710",
255
- revokedAt: null,
256
- createdAt: "2026-03-09 00:26:35.710",
257
- updatedAt: "2026-03-10 00:26:35.710"
258
- }]
259
- ])
260
- });
261
- const repository = createRepository({ api });
262
-
263
- await repository.markAcceptedById("1");
264
-
265
- const payload = state.patchPayloads[0];
266
- assert.equal(payload.data.status, "accepted");
267
- assert.equal(typeof payload.data.acceptedAt, "string");
268
- assert.equal(payload.data.updatedAt, payload.data.acceptedAt);
269
- });
270
-
271
- test("workspaceInvitesRepository.listPendingByWorkspaceIdWithWorkspace keeps workspace join fields outside the base resource contract", async () => {
272
- const { api } = createWorkspaceInvitesApiStub({
273
- rows: [
274
- {
275
- id: "1",
276
- workspace: {
277
- id: "9",
278
- slug: "tony3",
279
- name: "Tony3",
280
- avatarUrl: "https://example.com/avatar.png"
281
- },
282
- email: "invitee@example.com",
283
- roleSid: "member",
284
- status: "pending",
285
- tokenHash: "hash-token",
286
- invitedByUser: { id: "1" },
287
- expiresAt: "2030-01-01 00:00:00.000",
288
- acceptedAt: null,
289
- revokedAt: null,
290
- createdAt: "2026-03-09 00:26:35.710",
291
- updatedAt: "2026-03-09 00:26:35.710"
292
- }
293
- ]
294
- });
295
- const repository = createRepository({ api });
296
-
297
- const invites = await repository.listPendingByWorkspaceIdWithWorkspace("9");
298
-
299
- assert.deepEqual(invites, [
300
- {
301
- id: "1",
302
- workspaceId: "9",
303
- email: "invitee@example.com",
304
- roleSid: "member",
305
- status: "pending",
306
- tokenHash: "hash-token",
307
- invitedByUserId: "1",
308
- expiresAt: toIsoString("2030-01-01 00:00:00.000"),
309
- acceptedAt: null,
310
- revokedAt: null,
311
- createdAt: toIsoString("2026-03-09 00:26:35.710"),
312
- updatedAt: toIsoString("2026-03-09 00:26:35.710"),
313
- workspaceSlug: "tony3",
314
- workspaceName: "Tony3",
315
- workspaceAvatarUrl: "https://example.com/avatar.png"
316
- }
317
- ]);
318
- });