@ordinatio/entities 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/chunk-ZWID3JLL.mjs +123 -0
- package/dist/companies/index.d.mts +60 -0
- package/dist/companies/index.mjs +24 -0
- package/dist/companies/prisma/index.d.mts +58 -0
- package/dist/companies/prisma/index.mjs +276 -0
- package/dist/companies/server/index.d.mts +167 -0
- package/dist/companies/server/index.mjs +1826 -0
- package/dist/constants-DV-gn6u8.d.mts +22 -0
- package/dist/tags/prisma/index.d.mts +60 -0
- package/dist/tags/prisma/index.mjs +268 -0
- package/dist/tags/server/index.d.mts +3 -117
- package/dist/tags/server/index.mjs +11 -0
- package/dist/types-C81I1Teg.d.mts +117 -0
- package/dist/types-CbfseUAS.d.mts +169 -0
- package/package.json +22 -2
|
@@ -0,0 +1,1826 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_COMPANY_LIMITS,
|
|
3
|
+
checkCompanyName,
|
|
4
|
+
checkCompanyRole,
|
|
5
|
+
checkCompanyWebsite,
|
|
6
|
+
domainOfEmail,
|
|
7
|
+
isFreeMailDomain,
|
|
8
|
+
suggestCompanies
|
|
9
|
+
} from "../../chunk-ZWID3JLL.mjs";
|
|
10
|
+
|
|
11
|
+
// src/companies/server/service.ts
|
|
12
|
+
function byName(a, b) {
|
|
13
|
+
return a.name.localeCompare(b.name) || a.id.localeCompare(b.id);
|
|
14
|
+
}
|
|
15
|
+
function unknownKind(method, result) {
|
|
16
|
+
throw new Error(`company store ${method} returned an unknown result kind "${result.kind}": the store and the service are out of step`);
|
|
17
|
+
}
|
|
18
|
+
function createCompanyService(store, suppliedLimits = DEFAULT_COMPANY_LIMITS) {
|
|
19
|
+
const limits = { ...suppliedLimits };
|
|
20
|
+
function fail(failure) {
|
|
21
|
+
return { ok: false, ...failure };
|
|
22
|
+
}
|
|
23
|
+
function checkName(name) {
|
|
24
|
+
const checked = checkCompanyName(name, limits.maxNameLength);
|
|
25
|
+
if (checked.ok) return checked;
|
|
26
|
+
return { ok: false, failure: { reason: "invalid-name", problem: checked.reason, length: checked.length } };
|
|
27
|
+
}
|
|
28
|
+
function checkWebsite(website) {
|
|
29
|
+
const checked = checkCompanyWebsite(website ?? "", limits.maxWebsiteLength);
|
|
30
|
+
if (!checked.ok) return { ok: false, failure: { reason: "invalid-website", problem: checked.reason } };
|
|
31
|
+
return { ok: true, value: checked.website === null ? { website: null, domain: null } : { website: checked.website, domain: checked.domain } };
|
|
32
|
+
}
|
|
33
|
+
function checkRole(role) {
|
|
34
|
+
const checked = checkCompanyRole(role, limits.maxRoleLength);
|
|
35
|
+
if (checked.ok) return checked;
|
|
36
|
+
return { ok: false, failure: { reason: "invalid-role", problem: checked.reason, length: checked.length } };
|
|
37
|
+
}
|
|
38
|
+
async function create(input) {
|
|
39
|
+
const name = checkName(input.name);
|
|
40
|
+
if (!name.ok) return fail(name.failure);
|
|
41
|
+
const website = checkWebsite(input.website);
|
|
42
|
+
if (!website.ok) return fail(website.failure);
|
|
43
|
+
const result = await store.create({ name: name.name, nameKey: name.key, ...website.value }, { maxCompanies: limits.maxPerWorkspace });
|
|
44
|
+
if (result.kind === "created") return { ok: true, company: result.company };
|
|
45
|
+
if (result.kind === "duplicate") return fail({ reason: "duplicate" });
|
|
46
|
+
if (result.kind === "limit") return fail({ reason: "limit" });
|
|
47
|
+
return unknownKind("create", result);
|
|
48
|
+
}
|
|
49
|
+
async function findOrCreate(name) {
|
|
50
|
+
const checked = checkName(name);
|
|
51
|
+
if (!checked.ok) return fail(checked.failure);
|
|
52
|
+
const existing = await store.findByKey(checked.key);
|
|
53
|
+
if (existing) return { ok: true, company: existing, created: false };
|
|
54
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
55
|
+
const made = await create({ name: checked.name });
|
|
56
|
+
if (made.ok) return { ok: true, company: made.company, created: true };
|
|
57
|
+
if (made.reason !== "duplicate") return made;
|
|
58
|
+
const raced = await store.findByKey(checked.key);
|
|
59
|
+
if (raced) return { ok: true, company: raced, created: false };
|
|
60
|
+
}
|
|
61
|
+
return fail({ reason: "duplicate" });
|
|
62
|
+
}
|
|
63
|
+
async function update(id, input) {
|
|
64
|
+
if (!await store.get(id)) return fail({ reason: "not-found" });
|
|
65
|
+
const patch = {};
|
|
66
|
+
if (input.name !== void 0) {
|
|
67
|
+
const name = checkName(input.name);
|
|
68
|
+
if (!name.ok) return fail(name.failure);
|
|
69
|
+
patch.rename = { name: name.name, nameKey: name.key };
|
|
70
|
+
}
|
|
71
|
+
if (input.website !== void 0) {
|
|
72
|
+
const website = checkWebsite(input.website);
|
|
73
|
+
if (!website.ok) return fail(website.failure);
|
|
74
|
+
patch.website = website.value;
|
|
75
|
+
}
|
|
76
|
+
const result = await store.update(id, patch);
|
|
77
|
+
if (result.kind === "updated") return { ok: true, before: result.before, after: result.after };
|
|
78
|
+
if (result.kind === "notFound") return fail({ reason: "not-found" });
|
|
79
|
+
if (result.kind === "duplicate") return fail({ reason: "duplicate" });
|
|
80
|
+
return unknownKind("update", result);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
async list() {
|
|
84
|
+
return [...await store.list()].sort(byName);
|
|
85
|
+
},
|
|
86
|
+
async options() {
|
|
87
|
+
return [...await store.list()].sort(byName).map((company) => ({ id: company.id, name: company.name }));
|
|
88
|
+
},
|
|
89
|
+
async get(id) {
|
|
90
|
+
const company = await store.get(id);
|
|
91
|
+
return company ? { ok: true, company } : fail({ reason: "not-found" });
|
|
92
|
+
},
|
|
93
|
+
create,
|
|
94
|
+
findOrCreate,
|
|
95
|
+
update,
|
|
96
|
+
async remove(id) {
|
|
97
|
+
const result = await store.remove(id);
|
|
98
|
+
if (result.kind === "removed") return { ok: true, company: result.company, memberCount: result.memberCount };
|
|
99
|
+
if (result.kind === "notFound") return fail({ reason: "not-found" });
|
|
100
|
+
return unknownKind("remove", result);
|
|
101
|
+
},
|
|
102
|
+
async addMember(companyId, personId, input, actorId) {
|
|
103
|
+
if (!await store.get(companyId)) return fail({ reason: "not-found" });
|
|
104
|
+
if (!await store.personExists(personId)) return fail({ reason: "person-gone" });
|
|
105
|
+
const role = checkRole(input.role);
|
|
106
|
+
if (!role.ok) return fail(role.failure);
|
|
107
|
+
const result = await store.addMember(companyId, personId, role.role, actorId, { maxMembers: limits.maxMembersPerCompany });
|
|
108
|
+
if (result.kind === "added") return { ok: true, member: result.member, added: true };
|
|
109
|
+
if (result.kind === "already") return { ok: true, member: result.member, added: false };
|
|
110
|
+
if (result.kind === "companyGone") return fail({ reason: "not-found" });
|
|
111
|
+
if (result.kind === "personGone") return fail({ reason: "person-gone" });
|
|
112
|
+
if (result.kind === "memberLimit") return fail({ reason: "member-limit" });
|
|
113
|
+
return unknownKind("addMember", result);
|
|
114
|
+
},
|
|
115
|
+
async setRole(companyId, personId, role) {
|
|
116
|
+
if (!await store.get(companyId)) return fail({ reason: "not-found" });
|
|
117
|
+
const checked = checkRole(role);
|
|
118
|
+
if (!checked.ok) return fail(checked.failure);
|
|
119
|
+
const result = await store.setRole(companyId, personId, checked.role);
|
|
120
|
+
if (result.kind === "updated") return { ok: true, before: result.before, after: result.after };
|
|
121
|
+
if (result.kind === "companyGone") return fail({ reason: "not-found" });
|
|
122
|
+
if (result.kind === "notMember") return fail({ reason: "not-member" });
|
|
123
|
+
return unknownKind("setRole", result);
|
|
124
|
+
},
|
|
125
|
+
async removeMember(companyId, personId) {
|
|
126
|
+
const result = await store.removeMember(companyId, personId);
|
|
127
|
+
if (result.kind === "removed") return { ok: true, member: result.member };
|
|
128
|
+
if (result.kind === "companyGone") return fail({ reason: "not-found" });
|
|
129
|
+
if (result.kind === "notMember") return fail({ reason: "not-member" });
|
|
130
|
+
return unknownKind("removeMember", result);
|
|
131
|
+
},
|
|
132
|
+
async members(companyId) {
|
|
133
|
+
if (!await store.get(companyId)) return fail({ reason: "not-found" });
|
|
134
|
+
return { ok: true, members: [...await store.members(companyId)] };
|
|
135
|
+
},
|
|
136
|
+
memberCounts(ids2) {
|
|
137
|
+
return store.memberCounts(ids2);
|
|
138
|
+
},
|
|
139
|
+
async suggest(email, personId) {
|
|
140
|
+
const domain = domainOfEmail(email);
|
|
141
|
+
if (domain === null || isFreeMailDomain(domain)) return [];
|
|
142
|
+
const candidates = await store.findByDomain(domain);
|
|
143
|
+
if (candidates.length === 0) return [];
|
|
144
|
+
const already = personId === void 0 ? [] : ((await store.companiesForPeople([personId])).get(personId) ?? []).map((entry) => entry.company.id);
|
|
145
|
+
const suggested = new Set(suggestCompanies(email, candidates, already));
|
|
146
|
+
return candidates.filter((company) => suggested.has(company.id)).sort(byName);
|
|
147
|
+
},
|
|
148
|
+
async forPeople(personIds) {
|
|
149
|
+
const found = await store.companiesForPeople(personIds);
|
|
150
|
+
return new Map([...found].map(([id, list]) => [id, [...list].sort((a, b) => byName(a.company, b.company))]));
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/companies/server/memory-store.ts
|
|
156
|
+
var tick = () => Promise.resolve();
|
|
157
|
+
var memberKey = (companyId, personId) => `${companyId}\0${personId}`;
|
|
158
|
+
var personKey = (workspace, id) => `${workspace}\0${id}`;
|
|
159
|
+
function createMemoryCompanyWorld(options = {}) {
|
|
160
|
+
const serialise = options.serialise ?? true;
|
|
161
|
+
const companies = /* @__PURE__ */ new Map();
|
|
162
|
+
const people = /* @__PURE__ */ new Set();
|
|
163
|
+
const memberships = /* @__PURE__ */ new Map();
|
|
164
|
+
let sequence = 0;
|
|
165
|
+
let personSequence = 0;
|
|
166
|
+
let pendingHook = null;
|
|
167
|
+
let tail = Promise.resolve();
|
|
168
|
+
function exclusive(fn) {
|
|
169
|
+
if (!serialise) return fn();
|
|
170
|
+
const run = tail.then(fn, fn);
|
|
171
|
+
tail = run.then(() => void 0, () => void 0);
|
|
172
|
+
return run;
|
|
173
|
+
}
|
|
174
|
+
const copy = (record) => ({ ...record });
|
|
175
|
+
const copyMember = (record) => ({ ...record });
|
|
176
|
+
function store(workspace) {
|
|
177
|
+
const mine = () => [...companies.values()].filter((row) => row.workspace === workspace).map((row) => row.record);
|
|
178
|
+
const own = (id) => {
|
|
179
|
+
const row = companies.get(id);
|
|
180
|
+
return row && row.workspace === workspace ? row.record : void 0;
|
|
181
|
+
};
|
|
182
|
+
const personIsMine = (id) => people.has(personKey(workspace, id));
|
|
183
|
+
const membersOfCompany = (companyId) => [...memberships.values()].filter((m) => m.workspace === workspace && m.record.companyId === companyId);
|
|
184
|
+
function addBlocker(companyId, personId, limits) {
|
|
185
|
+
if (!own(companyId)) return { kind: "companyGone" };
|
|
186
|
+
if (!personIsMine(personId)) return { kind: "personGone" };
|
|
187
|
+
const existing = memberships.get(memberKey(companyId, personId));
|
|
188
|
+
if (existing) return { kind: "already", member: copyMember(existing.record) };
|
|
189
|
+
if (membersOfCompany(companyId).length >= limits.maxMembers) return { kind: "memberLimit" };
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
async list() {
|
|
194
|
+
return mine().map(copy);
|
|
195
|
+
},
|
|
196
|
+
async getMany(ids2) {
|
|
197
|
+
const wanted = new Set(ids2);
|
|
198
|
+
return mine().filter((company) => wanted.has(company.id)).map(copy);
|
|
199
|
+
},
|
|
200
|
+
async findByKey(nameKey) {
|
|
201
|
+
const found = mine().find((company) => company.nameKey === nameKey);
|
|
202
|
+
return found ? copy(found) : null;
|
|
203
|
+
},
|
|
204
|
+
async get(id) {
|
|
205
|
+
const found = own(id);
|
|
206
|
+
return found ? copy(found) : null;
|
|
207
|
+
},
|
|
208
|
+
create(input, limits) {
|
|
209
|
+
return exclusive(async () => {
|
|
210
|
+
if (mine().length >= limits.maxCompanies) return { kind: "limit" };
|
|
211
|
+
if (mine().some((company) => company.nameKey === input.nameKey)) return { kind: "duplicate" };
|
|
212
|
+
await tick();
|
|
213
|
+
const record = { id: `company_${++sequence}`, name: input.name, nameKey: input.nameKey, website: input.website, domain: input.domain };
|
|
214
|
+
companies.set(record.id, { workspace, record });
|
|
215
|
+
return { kind: "created", company: copy(record) };
|
|
216
|
+
});
|
|
217
|
+
},
|
|
218
|
+
update(id, patch) {
|
|
219
|
+
return exclusive(async () => {
|
|
220
|
+
const current = own(id);
|
|
221
|
+
if (!current) return { kind: "notFound" };
|
|
222
|
+
const before = copy(current);
|
|
223
|
+
if (patch.rename && mine().some((company) => company.id !== id && company.nameKey === patch.rename?.nameKey)) return { kind: "duplicate" };
|
|
224
|
+
await tick();
|
|
225
|
+
if (patch.rename) {
|
|
226
|
+
current.name = patch.rename.name;
|
|
227
|
+
current.nameKey = patch.rename.nameKey;
|
|
228
|
+
}
|
|
229
|
+
if (patch.website) {
|
|
230
|
+
current.website = patch.website.website;
|
|
231
|
+
current.domain = patch.website.domain;
|
|
232
|
+
}
|
|
233
|
+
return { kind: "updated", before, after: copy(current) };
|
|
234
|
+
});
|
|
235
|
+
},
|
|
236
|
+
remove(id) {
|
|
237
|
+
return exclusive(async () => {
|
|
238
|
+
const current = own(id);
|
|
239
|
+
if (!current) return { kind: "notFound" };
|
|
240
|
+
const memberCount = membersOfCompany(id).length;
|
|
241
|
+
await tick();
|
|
242
|
+
companies.delete(id);
|
|
243
|
+
for (const [key2, member] of memberships) if (member.record.companyId === id) memberships.delete(key2);
|
|
244
|
+
return { kind: "removed", company: copy(current), memberCount };
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
async findByDomain(domain) {
|
|
248
|
+
return mine().filter((company) => company.domain === domain).map(copy);
|
|
249
|
+
},
|
|
250
|
+
async memberCounts(ids2) {
|
|
251
|
+
if (ids2 !== void 0 && ids2.length === 0) return /* @__PURE__ */ new Map();
|
|
252
|
+
const wanted = ids2 === void 0 ? null : new Set(ids2);
|
|
253
|
+
const counts = /* @__PURE__ */ new Map();
|
|
254
|
+
for (const company of mine()) if (wanted === null || wanted.has(company.id)) counts.set(company.id, membersOfCompany(company.id).length);
|
|
255
|
+
return counts;
|
|
256
|
+
},
|
|
257
|
+
async personExists(personId) {
|
|
258
|
+
return personIsMine(personId);
|
|
259
|
+
},
|
|
260
|
+
async addMember(companyId, personId, role, actorId, limits) {
|
|
261
|
+
const early = await exclusive(async () => addBlocker(companyId, personId, limits));
|
|
262
|
+
if (early) return early;
|
|
263
|
+
const hook = pendingHook;
|
|
264
|
+
pendingHook = null;
|
|
265
|
+
if (hook) await hook();
|
|
266
|
+
return exclusive(async () => {
|
|
267
|
+
const blocked = addBlocker(companyId, personId, limits);
|
|
268
|
+
if (blocked) return blocked;
|
|
269
|
+
await tick();
|
|
270
|
+
const record = { companyId, personId, role };
|
|
271
|
+
memberships.set(memberKey(companyId, personId), { workspace, record, actorId });
|
|
272
|
+
return { kind: "added", member: copyMember(record) };
|
|
273
|
+
});
|
|
274
|
+
},
|
|
275
|
+
setRole(companyId, personId, role) {
|
|
276
|
+
return exclusive(async () => {
|
|
277
|
+
if (!own(companyId)) return { kind: "companyGone" };
|
|
278
|
+
const current = memberships.get(memberKey(companyId, personId));
|
|
279
|
+
if (!current || current.workspace !== workspace) return { kind: "notMember" };
|
|
280
|
+
const before = copyMember(current.record);
|
|
281
|
+
await tick();
|
|
282
|
+
current.record.role = role;
|
|
283
|
+
return { kind: "updated", before, after: copyMember(current.record) };
|
|
284
|
+
});
|
|
285
|
+
},
|
|
286
|
+
removeMember(companyId, personId) {
|
|
287
|
+
return exclusive(async () => {
|
|
288
|
+
if (!own(companyId)) return { kind: "companyGone" };
|
|
289
|
+
const current = memberships.get(memberKey(companyId, personId));
|
|
290
|
+
if (!current || current.workspace !== workspace) return { kind: "notMember" };
|
|
291
|
+
await tick();
|
|
292
|
+
memberships.delete(memberKey(companyId, personId));
|
|
293
|
+
return { kind: "removed", member: copyMember(current.record) };
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
async members(companyId) {
|
|
297
|
+
if (!own(companyId)) return [];
|
|
298
|
+
return membersOfCompany(companyId).map((m) => copyMember(m.record));
|
|
299
|
+
},
|
|
300
|
+
async companiesForPeople(personIds) {
|
|
301
|
+
const wanted = new Set(personIds);
|
|
302
|
+
const found = /* @__PURE__ */ new Map();
|
|
303
|
+
for (const m of memberships.values()) {
|
|
304
|
+
if (m.workspace !== workspace || !wanted.has(m.record.personId)) continue;
|
|
305
|
+
const company = own(m.record.companyId);
|
|
306
|
+
if (!company) continue;
|
|
307
|
+
const list = found.get(m.record.personId) ?? [];
|
|
308
|
+
list.push({ company: copy(company), role: m.record.role });
|
|
309
|
+
found.set(m.record.personId, list);
|
|
310
|
+
}
|
|
311
|
+
return found;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
store,
|
|
317
|
+
addPerson(workspace, id) {
|
|
318
|
+
const personId = id ?? `person_${++personSequence}`;
|
|
319
|
+
people.add(personKey(workspace, personId));
|
|
320
|
+
return personId;
|
|
321
|
+
},
|
|
322
|
+
removePerson(workspace, id) {
|
|
323
|
+
return exclusive(async () => {
|
|
324
|
+
people.delete(personKey(workspace, id));
|
|
325
|
+
for (const [key2, member] of memberships) if (member.workspace === workspace && member.record.personId === id) memberships.delete(key2);
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
armBeforeMemberWrite(hook) {
|
|
329
|
+
pendingHook = hook;
|
|
330
|
+
},
|
|
331
|
+
actorOf(workspace, companyId, personId) {
|
|
332
|
+
const found = memberships.get(memberKey(companyId, personId));
|
|
333
|
+
return found && found.workspace === workspace ? found.actorId : void 0;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function createMemoryCompanyHarness(options = {}) {
|
|
338
|
+
const world = createMemoryCompanyWorld(options);
|
|
339
|
+
let workspaces = 0;
|
|
340
|
+
return {
|
|
341
|
+
async newWorkspace() {
|
|
342
|
+
return `workspace_${++workspaces}`;
|
|
343
|
+
},
|
|
344
|
+
storeFor: (workspace) => world.store(workspace),
|
|
345
|
+
async addPerson(workspace) {
|
|
346
|
+
return world.addPerson(workspace);
|
|
347
|
+
},
|
|
348
|
+
removePerson: (workspace, id) => world.removePerson(workspace, id),
|
|
349
|
+
armBeforeMemberWrite: (hook) => world.armBeforeMemberWrite(hook),
|
|
350
|
+
async cleanup() {
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/companies/server/contract-kit.ts
|
|
356
|
+
var LIMITS = { maxCompanies: 100 };
|
|
357
|
+
var MEMBER_LIMITS = { maxMembers: 1e3 };
|
|
358
|
+
var ACTOR = "actor-1";
|
|
359
|
+
var NO_SITE = { website: null, domain: null };
|
|
360
|
+
var site = (domain) => ({ website: `https://${domain}`, domain });
|
|
361
|
+
var key = (name) => name.toLowerCase();
|
|
362
|
+
var ids = (list) => list.map((company) => company.id).sort();
|
|
363
|
+
var byPerson = (list) => [...list].sort((x, y) => x.personId.localeCompare(y.personId) || x.companyId.localeCompare(y.companyId));
|
|
364
|
+
var core = (company) => ({ id: company.id, name: company.name, nameKey: company.nameKey, website: company.website, domain: company.domain });
|
|
365
|
+
var cores = (list) => list.map(core).sort((x, y) => x.id.localeCompare(y.id));
|
|
366
|
+
function makeKit(api, harness) {
|
|
367
|
+
async function setup() {
|
|
368
|
+
const workspace = await harness.newWorkspace();
|
|
369
|
+
const store = harness.storeFor(workspace);
|
|
370
|
+
const make = async (s, name, web = NO_SITE) => {
|
|
371
|
+
const result = await s.create({ name, nameKey: key(name), ...web }, LIMITS);
|
|
372
|
+
if (result.kind !== "created") throw new Error(`setup: could not create ${name}: ${result.kind}`);
|
|
373
|
+
return result.company;
|
|
374
|
+
};
|
|
375
|
+
const person = () => harness.addPerson(workspace);
|
|
376
|
+
const join = async (companyId, personId, role = null) => {
|
|
377
|
+
const result = await store.addMember(companyId, personId, role, ACTOR, MEMBER_LIMITS);
|
|
378
|
+
if (result.kind !== "added") throw new Error(`setup: could not add a member: ${result.kind}`);
|
|
379
|
+
return result.member;
|
|
380
|
+
};
|
|
381
|
+
return { workspace, store, make, person, join };
|
|
382
|
+
}
|
|
383
|
+
return { api, harness, setup };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/companies/server/contract-companies.ts
|
|
387
|
+
function defineCompanyStoreCompaniesContract(kit) {
|
|
388
|
+
const { describe, it, expect } = kit.api;
|
|
389
|
+
const { setup } = kit;
|
|
390
|
+
describe("CompanyStore contract: companies", () => {
|
|
391
|
+
it("creates a company and finds it by id, by key and in the list; getMany returns only the asked-for ids; no read (list, getMany, findByDomain, memberCounts) caps the number of rows, and get, findByKey, duplicate, limit, update and remove still work with 120 companies; and an id is never handed out twice, even after the highest one is removed", async () => {
|
|
392
|
+
const { store, make } = await setup();
|
|
393
|
+
const taft = await make(store, "Taft Law", site("taft.com"));
|
|
394
|
+
expect([taft.name, taft.nameKey, taft.website, taft.domain]).toEqual(["Taft Law", "taft law", "https://taft.com", "taft.com"]);
|
|
395
|
+
expect(typeof taft.id === "string" && taft.id.length > 0).toBe(true);
|
|
396
|
+
expect((await store.get(taft.id))?.id).toBe(taft.id);
|
|
397
|
+
expect((await store.findByKey("taft law"))?.id).toBe(taft.id);
|
|
398
|
+
expect(await store.findByKey("nope")).toBeNull();
|
|
399
|
+
expect(await store.get("missing")).toBeNull();
|
|
400
|
+
expect(cores(await store.list())).toEqual(cores([taft]));
|
|
401
|
+
expect(cores([await store.get(taft.id)])).toEqual(cores([taft]));
|
|
402
|
+
const other = await make(store, "Other");
|
|
403
|
+
const unasked = await make(store, "Unasked");
|
|
404
|
+
expect(ids(await store.getMany([taft.id, other.id, "missing"]))).toEqual(ids([taft, other]));
|
|
405
|
+
expect(await store.getMany([])).toEqual([]);
|
|
406
|
+
expect(await store.getMany(["missing", "ghost-1"])).toEqual([]);
|
|
407
|
+
expect(ids(await store.getMany([taft.id, taft.id]))).toEqual([taft.id]);
|
|
408
|
+
expect(ids(await store.getMany([unasked.id]))).toEqual([unasked.id]);
|
|
409
|
+
expect(new Set(ids(await store.list())).size).toBe(3);
|
|
410
|
+
const many3 = [taft, other, unasked];
|
|
411
|
+
const bigLimits = { maxCompanies: 1e3 };
|
|
412
|
+
for (let i = 0; i < 117; i++) {
|
|
413
|
+
const result = await store.create({ name: `Company ${i}`, nameKey: `company ${i}`, ...site("shared.com") }, bigLimits);
|
|
414
|
+
if (result.kind !== "created") throw new Error(`setup: could not create Company ${i}: ${result.kind}`);
|
|
415
|
+
many3.push(result.company);
|
|
416
|
+
}
|
|
417
|
+
expect((await store.list()).length).toBe(120);
|
|
418
|
+
expect(ids(await store.list())).toEqual(ids(many3));
|
|
419
|
+
expect(ids(await store.getMany(many3.map((company) => company.id)))).toEqual(ids(many3));
|
|
420
|
+
expect(ids(await store.findByDomain("shared.com"))).toEqual(ids(many3.slice(3)));
|
|
421
|
+
const everyCount = await store.memberCounts();
|
|
422
|
+
expect([...everyCount.keys()].sort()).toEqual(ids(many3));
|
|
423
|
+
expect([...everyCount.values()].every((count3) => count3 === 0)).toBe(true);
|
|
424
|
+
const askedCount = await store.memberCounts(many3.map((company) => company.id));
|
|
425
|
+
expect([...askedCount.keys()].sort()).toEqual(ids(many3));
|
|
426
|
+
const last = many3[119];
|
|
427
|
+
expect(core(await store.get(last.id))).toEqual(core(last));
|
|
428
|
+
expect((await store.findByKey(last.nameKey))?.id).toBe(last.id);
|
|
429
|
+
expect(await store.create({ name: "Copy", nameKey: last.nameKey, ...NO_SITE }, bigLimits)).toEqual({ kind: "duplicate" });
|
|
430
|
+
expect(await store.create({ name: "One too many", nameKey: "one too many", ...NO_SITE }, { maxCompanies: 120 })).toEqual({ kind: "limit" });
|
|
431
|
+
expect((await store.update(last.id, { rename: { name: "Last Renamed", nameKey: "last renamed" } })).kind).toBe("updated");
|
|
432
|
+
const removedLast = await store.remove(last.id);
|
|
433
|
+
expect(removedLast.kind === "removed" && removedLast.company.nameKey).toBe("last renamed");
|
|
434
|
+
expect((await store.list()).length).toBe(119);
|
|
435
|
+
const fresh = await store.create({ name: "Fresh", nameKey: "fresh", ...NO_SITE }, bigLimits);
|
|
436
|
+
expect(fresh.kind === "created" && fresh.company.id !== last.id && !ids(many3).includes(fresh.company.id)).toBe(true);
|
|
437
|
+
expect(await store.get(last.id)).toBeNull();
|
|
438
|
+
});
|
|
439
|
+
it("text comes back exactly as stored (accents, quotes, backslashes, percent and underscore signs, emoji, a name of the maximum length; given to create and to update), and a domain search is an exact value, never a pattern", async () => {
|
|
440
|
+
const { store, make } = await setup();
|
|
441
|
+
const texts = [
|
|
442
|
+
{ name: "M\xFCller & S\xF6hne, Inc.", web: site("mueller.com") },
|
|
443
|
+
{ name: `O'Brien "Law"; DROP TABLE "Company"; --`, web: site("obrien.com") },
|
|
444
|
+
{ name: "C:\\temp\\100%_sure", web: site("100%.com") },
|
|
445
|
+
{ name: "\xDCn\u0131\u0301c\xF6d\xE9 \u65E5\u672C\u8A9E \u{1F680}", web: site("a_b.com") },
|
|
446
|
+
{ name: " Spaced Out ", web: site("spaced.com") },
|
|
447
|
+
// a store keeps what it is given: cleaning names is the service's job
|
|
448
|
+
{ name: "Case Kept", web: { website: "https://Example.COM/Path?x=1", domain: "Example.COM" } },
|
|
449
|
+
// opaque to the store: not lower-cased, not cleaned
|
|
450
|
+
{ name: "Padded Site", web: { website: " https://padded.com/ ", domain: " padded.com " } },
|
|
451
|
+
// website and domain are kept as given too, spaces included
|
|
452
|
+
{ name: "x".repeat(120), web: site("long.com") }
|
|
453
|
+
];
|
|
454
|
+
const made = [];
|
|
455
|
+
for (const { name, web } of texts) made.push(await make(store, name, web));
|
|
456
|
+
for (const [index, company] of made.entries()) {
|
|
457
|
+
const { name, web } = texts[index];
|
|
458
|
+
const expected = { id: company.id, name, nameKey: name.toLowerCase(), website: web.website, domain: web.domain };
|
|
459
|
+
expect(core(company)).toEqual(expected);
|
|
460
|
+
expect(core(await store.get(company.id))).toEqual(expected);
|
|
461
|
+
expect((await store.findByKey(name.toLowerCase()))?.id).toBe(company.id);
|
|
462
|
+
expect(ids(await store.findByDomain(web.domain))).toEqual([company.id]);
|
|
463
|
+
expect(core((await store.getMany([company.id]))[0])).toEqual(expected);
|
|
464
|
+
expect(core(await store.findByKey(name.toLowerCase()))).toEqual(expected);
|
|
465
|
+
expect(cores(await store.findByDomain(web.domain))).toEqual([expected]);
|
|
466
|
+
}
|
|
467
|
+
expect(cores(await store.list())).toEqual(cores(made));
|
|
468
|
+
const later = await make(store, "Updated Later");
|
|
469
|
+
const written = { rename: { name: " Padded Rename ", nameKey: " Padded Rename " }, website: { website: " https://Upd.COM/Path?y=2 ", domain: " Upd.COM " } };
|
|
470
|
+
const after = await store.update(later.id, written);
|
|
471
|
+
const stored = { id: later.id, name: " Padded Rename ", nameKey: " Padded Rename ", website: " https://Upd.COM/Path?y=2 ", domain: " Upd.COM " };
|
|
472
|
+
expect(after.kind === "updated" && core(after.after)).toEqual(stored);
|
|
473
|
+
expect(core(await store.get(later.id))).toEqual(stored);
|
|
474
|
+
expect((await store.findByKey(" Padded Rename "))?.id).toBe(later.id);
|
|
475
|
+
expect(ids(await store.findByDomain(" Upd.COM "))).toEqual([later.id]);
|
|
476
|
+
expect(await store.findByDomain("upd.com")).toEqual([]);
|
|
477
|
+
await make(store, "No website at all");
|
|
478
|
+
for (const pattern of ["%", "_", "a%", "a_b.co_", "_ong.com", "100%", "1__.com", ".*", "obrien.com' OR '1'='1", "mueller.com\\", "example.com", "EXAMPLE.COM", "upd.com", "Upd.COM", "padded.com", "https://padded.com/", "null", "NULL", "undefined"]) expect(await store.findByDomain(pattern)).toEqual([]);
|
|
479
|
+
});
|
|
480
|
+
it("every write touches only its own company: create, rename, website set and clear, a rename and a website together, a refused update, a refused create and remove leave the other companies exactly as they were", async () => {
|
|
481
|
+
const { store, make } = await setup();
|
|
482
|
+
const withSite = await make(store, "Bystander One", site("one.com"));
|
|
483
|
+
const bare = await make(store, "Bystander Two");
|
|
484
|
+
const lookalike = await make(store, "Mine Again Extra");
|
|
485
|
+
const bystanders = [withSite, bare, lookalike];
|
|
486
|
+
const untouched = async () => {
|
|
487
|
+
expect(cores((await store.list()).filter((company) => bystanders.some((b) => b.id === company.id)))).toEqual(cores(bystanders));
|
|
488
|
+
for (const b of bystanders) expect(core(await store.get(b.id))).toEqual(core(b));
|
|
489
|
+
expect(cores(await store.getMany(bystanders.map((b) => b.id)))).toEqual(cores(bystanders));
|
|
490
|
+
expect(ids(await store.findByDomain("one.com"))).toEqual([withSite.id]);
|
|
491
|
+
expect((await store.findByKey(bare.nameKey))?.id).toBe(bare.id);
|
|
492
|
+
};
|
|
493
|
+
await untouched();
|
|
494
|
+
const mine = await make(store, "Mine");
|
|
495
|
+
await untouched();
|
|
496
|
+
await store.update(mine.id, { rename: { name: "Mine Renamed", nameKey: "mine renamed" } });
|
|
497
|
+
await untouched();
|
|
498
|
+
await store.update(mine.id, { website: site("mine.com") });
|
|
499
|
+
await untouched();
|
|
500
|
+
await store.update(mine.id, { website: NO_SITE });
|
|
501
|
+
await untouched();
|
|
502
|
+
await store.update(mine.id, { rename: { name: "Mine Again", nameKey: "mine again" }, website: site("again.com") });
|
|
503
|
+
await untouched();
|
|
504
|
+
await store.update(mine.id, { rename: { name: "Refused", nameKey: withSite.nameKey }, website: site("refused.com") });
|
|
505
|
+
await untouched();
|
|
506
|
+
await store.create({ name: "Refused too", nameKey: withSite.nameKey, ...site("refused2.com") }, LIMITS);
|
|
507
|
+
await untouched();
|
|
508
|
+
await store.update(mine.id, { website: site("one.com") });
|
|
509
|
+
expect(ids(await store.findByDomain("one.com"))).toEqual(ids([withSite, mine]));
|
|
510
|
+
expect((await store.update(mine.id, { rename: { name: mine.name, nameKey: mine.nameKey }, website: site("one.com") })).kind).toBe("updated");
|
|
511
|
+
expect((await store.remove(mine.id)).kind).toBe("removed");
|
|
512
|
+
await untouched();
|
|
513
|
+
expect(await store.get(mine.id)).toBeNull();
|
|
514
|
+
});
|
|
515
|
+
it("reads are exact: a key or a domain is matched whole, never by prefix, case or whitespace, and an id never by prefix; a company with no website reads back as null through every read", async () => {
|
|
516
|
+
const { store, make } = await setup();
|
|
517
|
+
const taft = await make(store, "Taft", site("taft.com"));
|
|
518
|
+
const bare = await make(store, "Bare");
|
|
519
|
+
for (const wrong of ["taf", "TAFT", " taft", "taft ", "taft\n", "t%", "taft%", "taft_"]) expect(await store.findByKey(wrong)).toBeNull();
|
|
520
|
+
for (const wrong of ["taft.co", "taft.com.", "Taft.com", " taft.com", "taft.com ", "aft.com", "taft.c%", "taft.co_"]) expect(await store.findByDomain(wrong)).toEqual([]);
|
|
521
|
+
expect(ids(await store.findByDomain("taft.com"))).toEqual([taft.id]);
|
|
522
|
+
expect(await store.get(taft.id.slice(0, -1))).toBeNull();
|
|
523
|
+
expect(await store.get(`${taft.id}x`)).toBeNull();
|
|
524
|
+
for (const odd of ["", taft.id.slice(0, -1), `${taft.id}x`, `${taft.id},${taft.id}`, `x,${taft.id}`, `${taft.id}\\`, "' OR '1'='1", `${taft.id}' --`]) {
|
|
525
|
+
expect(await store.get(odd)).toBeNull();
|
|
526
|
+
expect(await store.update(odd, { rename: { name: "Hijacked", nameKey: "hijacked" } })).toEqual({ kind: "notFound" });
|
|
527
|
+
expect(await store.remove(odd)).toEqual({ kind: "notFound" });
|
|
528
|
+
expect(await store.getMany([odd])).toEqual([]);
|
|
529
|
+
}
|
|
530
|
+
expect(cores(await store.list())).toEqual(cores([taft, bare]));
|
|
531
|
+
for (const read of [await store.get(bare.id), (await store.list()).find((c) => c.id === bare.id), (await store.getMany([bare.id]))[0], await store.findByKey("bare")]) {
|
|
532
|
+
expect([read?.website, read?.domain]).toEqual([null, null]);
|
|
533
|
+
}
|
|
534
|
+
expect(cores(await store.list())).toEqual(cores([taft, bare]));
|
|
535
|
+
expect(cores(await store.getMany([taft.id, bare.id]))).toEqual(cores([taft, bare]));
|
|
536
|
+
});
|
|
537
|
+
it("a company without a website has no domain either: both come back null, both survive a read, and an empty domain search does not find it", async () => {
|
|
538
|
+
const { store, make } = await setup();
|
|
539
|
+
const bare = await make(store, "Bare");
|
|
540
|
+
expect([bare.website, bare.domain]).toEqual([null, null]);
|
|
541
|
+
const read = await store.get(bare.id);
|
|
542
|
+
expect([read.website, read.domain]).toEqual([null, null]);
|
|
543
|
+
expect(await store.findByDomain("")).toEqual([]);
|
|
544
|
+
});
|
|
545
|
+
it("the key is whatever the caller supplied, kept apart from the name through create, rename and remove, and a renamed-away key is free again", async () => {
|
|
546
|
+
const { store } = await setup();
|
|
547
|
+
const created = await store.create({ name: "Foo Bar", nameKey: "foo-bar", ...NO_SITE }, LIMITS);
|
|
548
|
+
if (created.kind !== "created") throw new Error("setup");
|
|
549
|
+
expect(created.company.nameKey).toBe("foo-bar");
|
|
550
|
+
expect((await store.findByKey("foo-bar"))?.id).toBe(created.company.id);
|
|
551
|
+
expect(await store.findByKey("foo bar")).toBeNull();
|
|
552
|
+
const renamed = await store.update(created.company.id, { rename: { name: "Baz Qux", nameKey: "baz-qux" } });
|
|
553
|
+
expect(renamed.kind === "updated" && [renamed.before.nameKey, renamed.after.nameKey]).toEqual(["foo-bar", "baz-qux"]);
|
|
554
|
+
expect(await store.findByKey("foo-bar")).toBeNull();
|
|
555
|
+
expect((await store.create({ name: "Foo Bar", nameKey: "foo-bar", ...NO_SITE }, LIMITS)).kind).toBe("created");
|
|
556
|
+
const removed = await store.remove(created.company.id);
|
|
557
|
+
expect(removed.kind === "removed" && [removed.company.name, removed.company.nameKey]).toEqual(["Baz Qux", "baz-qux"]);
|
|
558
|
+
});
|
|
559
|
+
it("a second company with the same key is a duplicate and changes nothing: not the list, not the existing company, not the domains", async () => {
|
|
560
|
+
const { store, make } = await setup();
|
|
561
|
+
const taft = await make(store, "Taft", site("taft.com"));
|
|
562
|
+
expect(await store.create({ name: "TAFT", nameKey: "taft", ...site("other.com") }, LIMITS)).toEqual({ kind: "duplicate" });
|
|
563
|
+
expect((await store.list()).length).toBe(1);
|
|
564
|
+
expect(cores(await store.list())).toEqual(cores([taft]));
|
|
565
|
+
expect(await store.findByDomain("other.com")).toEqual([]);
|
|
566
|
+
});
|
|
567
|
+
it("keys are compared exactly: two keys that differ only by case are two keys, in create and in rename, and the same name under another key is another company (the service lower-cases before it calls the store)", async () => {
|
|
568
|
+
const { store } = await setup();
|
|
569
|
+
expect((await store.create({ name: "Taft", nameKey: "taft", ...NO_SITE }, LIMITS)).kind).toBe("created");
|
|
570
|
+
expect((await store.create({ name: "TAFT", nameKey: "TAFT", ...NO_SITE }, LIMITS)).kind).toBe("created");
|
|
571
|
+
expect((await store.findByKey("TAFT"))?.name).toBe("TAFT");
|
|
572
|
+
expect((await store.findByKey("taft"))?.name).toBe("Taft");
|
|
573
|
+
expect(await store.create({ name: "Again", nameKey: "TAFT", ...NO_SITE }, LIMITS)).toEqual({ kind: "duplicate" });
|
|
574
|
+
expect((await store.create({ name: "Taft", nameKey: "taft two", ...NO_SITE }, LIMITS)).kind).toBe("created");
|
|
575
|
+
const other = await store.create({ name: "Other", nameKey: "other", ...NO_SITE }, LIMITS);
|
|
576
|
+
const otherId = other.kind === "created" ? other.company.id : "";
|
|
577
|
+
expect((await store.update(otherId, { rename: { name: "Other Cased", nameKey: "Other" } })).kind).toBe("updated");
|
|
578
|
+
expect((await store.update(otherId, { rename: { name: "Taft Cased", nameKey: "tAFT" } })).kind).toBe("updated");
|
|
579
|
+
expect((await store.update(otherId, { rename: { name: "Taft", nameKey: "taft x" } })).kind).toBe("updated");
|
|
580
|
+
expect(await store.update(otherId, { rename: { name: "Taft Again", nameKey: "taft" } })).toEqual({ kind: "duplicate" });
|
|
581
|
+
});
|
|
582
|
+
it("the create limit wins over duplicate, and a removed company frees its place", async () => {
|
|
583
|
+
const { store, make } = await setup();
|
|
584
|
+
await make(store, "One");
|
|
585
|
+
await make(store, "Two");
|
|
586
|
+
expect(await store.create({ name: "Three", nameKey: "three", ...NO_SITE }, { maxCompanies: 2 })).toEqual({ kind: "limit" });
|
|
587
|
+
expect(await store.create({ name: "One", nameKey: "one", ...NO_SITE }, { maxCompanies: 2 })).toEqual({ kind: "limit" });
|
|
588
|
+
expect(await store.create({ name: "Zero", nameKey: "zero", ...NO_SITE }, { maxCompanies: 0 })).toEqual({ kind: "limit" });
|
|
589
|
+
expect(await store.create({ name: "One", nameKey: "one", ...NO_SITE }, { maxCompanies: 0 })).toEqual({ kind: "limit" });
|
|
590
|
+
expect((await store.create({ name: "Three", nameKey: "three", ...NO_SITE }, { maxCompanies: 3 })).kind).toBe("created");
|
|
591
|
+
expect((await store.list()).length).toBe(3);
|
|
592
|
+
const mixed = await setup();
|
|
593
|
+
await mixed.make(mixed.store, "With site", site("w.com"));
|
|
594
|
+
await mixed.make(mixed.store, "Without site");
|
|
595
|
+
expect(await mixed.store.create({ name: "Third", nameKey: "third", ...NO_SITE }, { maxCompanies: 2 })).toEqual({ kind: "limit" });
|
|
596
|
+
const [first] = await store.list();
|
|
597
|
+
expect((await store.remove(first.id)).kind).toBe("removed");
|
|
598
|
+
expect((await store.create({ name: "Four", nameKey: "four", ...NO_SITE }, { maxCompanies: 3 })).kind).toBe("created");
|
|
599
|
+
expect(await store.create({ name: "Five", nameKey: "five", ...NO_SITE }, { maxCompanies: 3 })).toEqual({ kind: "limit" });
|
|
600
|
+
});
|
|
601
|
+
it("update gives before and after; name and key travel together, website and domain travel together, and a field not in the patch does not change", async () => {
|
|
602
|
+
const { store, make } = await setup();
|
|
603
|
+
const taft = await make(store, "Taft", site("taft.com"));
|
|
604
|
+
const renamed = await store.update(taft.id, { rename: { name: "Taft Law", nameKey: "taft law" } });
|
|
605
|
+
expect(renamed.kind === "updated" && [core(renamed.before), core(renamed.after)]).toEqual([
|
|
606
|
+
{ id: taft.id, name: "Taft", nameKey: "taft", website: "https://taft.com", domain: "taft.com" },
|
|
607
|
+
{ id: taft.id, name: "Taft Law", nameKey: "taft law", website: "https://taft.com", domain: "taft.com" }
|
|
608
|
+
]);
|
|
609
|
+
const moved = await store.update(taft.id, { website: site("taftlaw.com") });
|
|
610
|
+
expect(moved.kind === "updated" && [core(moved.before), core(moved.after)]).toEqual([
|
|
611
|
+
{ id: taft.id, name: "Taft Law", nameKey: "taft law", website: "https://taft.com", domain: "taft.com" },
|
|
612
|
+
{ id: taft.id, name: "Taft Law", nameKey: "taft law", website: "https://taftlaw.com", domain: "taftlaw.com" }
|
|
613
|
+
]);
|
|
614
|
+
const cleared = await store.update(taft.id, { website: NO_SITE });
|
|
615
|
+
expect(cleared.kind === "updated" && [cleared.after.website, cleared.after.domain]).toEqual([null, null]);
|
|
616
|
+
const both = await store.update(taft.id, { rename: { name: "Both", nameKey: "both" }, website: site("both.com") });
|
|
617
|
+
expect(both.kind === "updated" && core(both.after)).toEqual({ id: taft.id, name: "Both", nameKey: "both", website: "https://both.com", domain: "both.com" });
|
|
618
|
+
expect(core(await store.get(taft.id))).toEqual({ id: taft.id, name: "Both", nameKey: "both", website: "https://both.com", domain: "both.com" });
|
|
619
|
+
const renamedAndCleared = await store.update(taft.id, { rename: { name: "Last", nameKey: "last" }, website: NO_SITE });
|
|
620
|
+
expect(renamedAndCleared.kind === "updated" && core(renamedAndCleared.after)).toEqual({ id: taft.id, name: "Last", nameKey: "last", website: null, domain: null });
|
|
621
|
+
expect(core(await store.get(taft.id))).toEqual({ id: taft.id, name: "Last", nameKey: "last", website: null, domain: null });
|
|
622
|
+
expect(await store.findByDomain("both.com")).toEqual([]);
|
|
623
|
+
});
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// src/companies/server/contract-companies-more.ts
|
|
628
|
+
function defineCompanyStoreCompaniesMoreContract(kit) {
|
|
629
|
+
const { describe, it, expect } = kit.api;
|
|
630
|
+
const { setup } = kit;
|
|
631
|
+
describe("CompanyStore contract: companies (more)", () => {
|
|
632
|
+
it("a company with no website can be given one, and a rename leaves a missing website missing (null, not an empty string)", async () => {
|
|
633
|
+
const { store, make } = await setup();
|
|
634
|
+
const bare = await make(store, "Bare");
|
|
635
|
+
const renamed = await store.update(bare.id, { rename: { name: "Bare Two", nameKey: "bare two" } });
|
|
636
|
+
expect(renamed.kind === "updated" && [renamed.before.website, renamed.before.domain, renamed.after.website, renamed.after.domain]).toEqual([null, null, null, null]);
|
|
637
|
+
expect([(await store.get(bare.id))?.website, (await store.get(bare.id))?.domain]).toEqual([null, null]);
|
|
638
|
+
const given = await store.update(bare.id, { website: site("bare.com") });
|
|
639
|
+
expect(given.kind === "updated" && [core(given.before), core(given.after)]).toEqual([
|
|
640
|
+
{ id: bare.id, name: "Bare Two", nameKey: "bare two", website: null, domain: null },
|
|
641
|
+
{ id: bare.id, name: "Bare Two", nameKey: "bare two", website: "https://bare.com", domain: "bare.com" }
|
|
642
|
+
]);
|
|
643
|
+
expect(core(await store.get(bare.id))).toEqual({ id: bare.id, name: "Bare Two", nameKey: "bare two", website: "https://bare.com", domain: "bare.com" });
|
|
644
|
+
expect(ids(await store.findByDomain("bare.com"))).toEqual([bare.id]);
|
|
645
|
+
});
|
|
646
|
+
it("writes that change nothing still succeed (the same patch twice, a website equal to the current one, an identical rename, clearing a bare company, a patch naming fields as undefined, another website text with the very same domain), and two updates in a row chain their before and after", async () => {
|
|
647
|
+
const { store, make } = await setup();
|
|
648
|
+
const acme = await make(store, "Acme", site("acme.com"));
|
|
649
|
+
const patch = { rename: { name: "Acme Two", nameKey: "acme two" }, website: site("two.com") };
|
|
650
|
+
const first = await store.update(acme.id, patch);
|
|
651
|
+
const second = await store.update(acme.id, patch);
|
|
652
|
+
const twoCore = { id: acme.id, name: "Acme Two", nameKey: "acme two", website: "https://two.com", domain: "two.com" };
|
|
653
|
+
expect(first.kind === "updated" && [core(first.before).name, core(first.after)]).toEqual(["Acme", twoCore]);
|
|
654
|
+
expect(second.kind === "updated" && [core(second.before), core(second.after)]).toEqual([twoCore, twoCore]);
|
|
655
|
+
expect((await store.update(acme.id, { website: site("two.com") })).kind).toBe("updated");
|
|
656
|
+
expect((await store.update(acme.id, { rename: { name: "Acme Two", nameKey: "acme two" } })).kind).toBe("updated");
|
|
657
|
+
const untouchedByUndefined = await store.update(acme.id, { rename: void 0, website: void 0 });
|
|
658
|
+
expect(untouchedByUndefined.kind === "updated" && [core(untouchedByUndefined.before), core(untouchedByUndefined.after)]).toEqual([twoCore, twoCore]);
|
|
659
|
+
const bare = await make(store, "Bare");
|
|
660
|
+
const cleared = await store.update(bare.id, { website: NO_SITE });
|
|
661
|
+
expect(cleared.kind === "updated" && [core(cleared.before), core(cleared.after)]).toEqual([core(bare), core(bare)]);
|
|
662
|
+
const sameDomain = await store.update(acme.id, { website: { website: "https://www.two.com/about", domain: "two.com" } });
|
|
663
|
+
expect(sameDomain.kind === "updated" && sameDomain.after.website).toBe("https://www.two.com/about");
|
|
664
|
+
expect((await store.get(acme.id))?.website).toBe("https://www.two.com/about");
|
|
665
|
+
const third = await store.update(acme.id, { rename: { name: "Acme Three", nameKey: "acme three" } });
|
|
666
|
+
expect(third.kind === "updated" && [core(third.before).name, core(third.after).name]).toEqual(["Acme Two", "Acme Three"]);
|
|
667
|
+
});
|
|
668
|
+
it("after every kind of update, get, list, getMany, findByKey, findByDomain and memberCounts all show the latest data", async () => {
|
|
669
|
+
const { store, make } = await setup();
|
|
670
|
+
const acme = await make(store, "Acme", site("acme.com"));
|
|
671
|
+
await make(store, "Other", site("other.com"));
|
|
672
|
+
const everywhere = async (expected) => {
|
|
673
|
+
expect(core(await store.get(expected.id))).toEqual(expected);
|
|
674
|
+
expect(core((await store.list()).find((company) => company.id === expected.id))).toEqual(expected);
|
|
675
|
+
expect(core((await store.getMany([expected.id]))[0])).toEqual(expected);
|
|
676
|
+
expect(core(await store.findByKey(expected.nameKey))).toEqual(expected);
|
|
677
|
+
if (expected.domain !== null) expect(cores(await store.findByDomain(expected.domain))).toEqual([expected]);
|
|
678
|
+
expect([...await store.memberCounts([expected.id])]).toEqual([[expected.id, 0]]);
|
|
679
|
+
};
|
|
680
|
+
await everywhere({ id: acme.id, name: "Acme", nameKey: "acme", website: "https://acme.com", domain: "acme.com" });
|
|
681
|
+
await store.update(acme.id, { rename: { name: "Acme Two", nameKey: "acme two" } });
|
|
682
|
+
await everywhere({ id: acme.id, name: "Acme Two", nameKey: "acme two", website: "https://acme.com", domain: "acme.com" });
|
|
683
|
+
expect(await store.findByKey("acme")).toBeNull();
|
|
684
|
+
await store.update(acme.id, { website: site("two.com") });
|
|
685
|
+
await everywhere({ id: acme.id, name: "Acme Two", nameKey: "acme two", website: "https://two.com", domain: "two.com" });
|
|
686
|
+
expect(await store.findByDomain("acme.com")).toEqual([]);
|
|
687
|
+
await store.update(acme.id, { rename: { name: "Acme Three", nameKey: "acme three" }, website: site("three.com") });
|
|
688
|
+
await everywhere({ id: acme.id, name: "Acme Three", nameKey: "acme three", website: "https://three.com", domain: "three.com" });
|
|
689
|
+
await store.update(acme.id, { website: NO_SITE });
|
|
690
|
+
await everywhere({ id: acme.id, name: "Acme Three", nameKey: "acme three", website: null, domain: null });
|
|
691
|
+
expect(await store.findByDomain("three.com")).toEqual([]);
|
|
692
|
+
});
|
|
693
|
+
it("refused and repeated operations do not change how much room is left: a duplicate create, a refused rename, a second remove and an update of nothing use or free no place", async () => {
|
|
694
|
+
const { store, make } = await setup();
|
|
695
|
+
const limitThree = { maxCompanies: 3 };
|
|
696
|
+
const a = await make(store, "A");
|
|
697
|
+
const b = await make(store, "B");
|
|
698
|
+
const c1 = await make(store, "C");
|
|
699
|
+
expect(await store.create({ name: "D", nameKey: "d", ...NO_SITE }, limitThree)).toEqual({ kind: "limit" });
|
|
700
|
+
expect((await store.remove(c1.id)).kind).toBe("removed");
|
|
701
|
+
expect((await store.create({ name: "A again", nameKey: "a", ...NO_SITE }, limitThree)).kind).toBe("duplicate");
|
|
702
|
+
expect(await store.update(b.id, { rename: { name: "A", nameKey: "a" } })).toEqual({ kind: "duplicate" });
|
|
703
|
+
expect(await store.remove(c1.id)).toEqual({ kind: "notFound" });
|
|
704
|
+
expect(await store.update("missing", { rename: { name: "X", nameKey: "x" } })).toEqual({ kind: "notFound" });
|
|
705
|
+
expect((await store.create({ name: "E", nameKey: "e", ...NO_SITE }, limitThree)).kind).toBe("created");
|
|
706
|
+
expect(await store.create({ name: "F", nameKey: "f", ...NO_SITE }, limitThree)).toEqual({ kind: "limit" });
|
|
707
|
+
expect(ids(await store.list()).length).toBe(3);
|
|
708
|
+
expect((await store.get(a.id))?.id).toBe(a.id);
|
|
709
|
+
});
|
|
710
|
+
it("a key that was renamed away is free for ANOTHER company, and a company can go back to a key it left", async () => {
|
|
711
|
+
const { store, make } = await setup();
|
|
712
|
+
const a = await make(store, "Alpha");
|
|
713
|
+
const b = await make(store, "Beta");
|
|
714
|
+
expect((await store.update(a.id, { rename: { name: "Gamma", nameKey: "gamma" } })).kind).toBe("updated");
|
|
715
|
+
expect((await store.update(b.id, { rename: { name: "Alpha", nameKey: "alpha" } })).kind).toBe("updated");
|
|
716
|
+
expect((await store.update(a.id, { rename: { name: "Beta", nameKey: "beta" } })).kind).toBe("updated");
|
|
717
|
+
expect((await store.update(a.id, { rename: { name: "Gamma", nameKey: "gamma" } })).kind).toBe("updated");
|
|
718
|
+
expect((await store.findByKey("gamma"))?.id).toBe(a.id);
|
|
719
|
+
expect((await store.findByKey("alpha"))?.id).toBe(b.id);
|
|
720
|
+
expect(await store.findByKey("beta")).toBeNull();
|
|
721
|
+
});
|
|
722
|
+
it("update precedence: not found first, then duplicate against ANOTHER company only; a case-only rename of itself is fine; an empty patch changes nothing", async () => {
|
|
723
|
+
const { store, make } = await setup();
|
|
724
|
+
const acme = await make(store, "Acme", site("acme.com"));
|
|
725
|
+
const bolt = await make(store, "Bolt");
|
|
726
|
+
expect(await store.update("missing", { rename: { name: "Acme", nameKey: "acme" } })).toEqual({ kind: "notFound" });
|
|
727
|
+
expect(await store.update(bolt.id, { rename: { name: "Acme", nameKey: "acme" } })).toEqual({ kind: "duplicate" });
|
|
728
|
+
expect((await store.get(bolt.id))?.name).toBe("Bolt");
|
|
729
|
+
expect(await store.update(bolt.id, { rename: { name: "Acme", nameKey: "acme" }, website: site("bolt.com") })).toEqual({ kind: "duplicate" });
|
|
730
|
+
expect(core(await store.get(bolt.id))).toEqual({ id: bolt.id, name: "Bolt", nameKey: "bolt", website: null, domain: null });
|
|
731
|
+
expect(await store.findByDomain("bolt.com")).toEqual([]);
|
|
732
|
+
expect((await store.update(acme.id, { rename: { name: "ACME", nameKey: "acme" } })).kind).toBe("updated");
|
|
733
|
+
expect((await store.get(acme.id))?.name).toBe("ACME");
|
|
734
|
+
const current = core(await store.get(acme.id));
|
|
735
|
+
expect(current).toEqual({ id: acme.id, name: "ACME", nameKey: "acme", website: "https://acme.com", domain: "acme.com" });
|
|
736
|
+
const empty = await store.update(acme.id, {});
|
|
737
|
+
expect(empty.kind === "updated" && [core(empty.before), core(empty.after)]).toEqual([current, current]);
|
|
738
|
+
expect(core(await store.get(acme.id))).toEqual(current);
|
|
739
|
+
const recased = await store.update(acme.id, { rename: { name: "Acme Two", nameKey: "acme" }, website: site("x.com") });
|
|
740
|
+
const recasedAs = { id: acme.id, name: "Acme Two", nameKey: "acme", website: "https://x.com", domain: "x.com" };
|
|
741
|
+
expect(recased.kind === "updated" && core(recased.after)).toEqual(recasedAs);
|
|
742
|
+
expect(core(await store.get(acme.id))).toEqual(recasedAs);
|
|
743
|
+
expect(ids(await store.findByDomain("x.com"))).toEqual([acme.id]);
|
|
744
|
+
expect(await store.findByDomain("acme.com")).toEqual([]);
|
|
745
|
+
});
|
|
746
|
+
it("remove returns the company and the number of people detached (none here), once; then it is not found and its key is free again", async () => {
|
|
747
|
+
const { store, make } = await setup();
|
|
748
|
+
const gone = await make(store, "Gone", site("gone.com"));
|
|
749
|
+
const stays = await make(store, "Stays", site("stays.com"));
|
|
750
|
+
const changed = await store.update(gone.id, { website: site("gone-now.com") });
|
|
751
|
+
if (changed.kind !== "updated") throw new Error(`setup: could not change the website: ${changed.kind}`);
|
|
752
|
+
const goneNow = changed.after;
|
|
753
|
+
const removed = await store.remove(gone.id);
|
|
754
|
+
expect(removed.kind === "removed" && [core(removed.company), removed.memberCount]).toEqual([core(goneNow), 0]);
|
|
755
|
+
expect(await store.remove(gone.id)).toEqual({ kind: "notFound" });
|
|
756
|
+
expect(await store.get(gone.id)).toBeNull();
|
|
757
|
+
expect(await store.update(gone.id, { rename: { name: "Back", nameKey: "back" } })).toEqual({ kind: "notFound" });
|
|
758
|
+
expect(await store.update(gone.id, { website: site("back.com") })).toEqual({ kind: "notFound" });
|
|
759
|
+
expect(await store.get(gone.id)).toBeNull();
|
|
760
|
+
expect(await store.findByKey("back")).toBeNull();
|
|
761
|
+
expect(ids(await store.list())).toEqual([stays.id]);
|
|
762
|
+
expect(ids(await store.getMany([gone.id, stays.id]))).toEqual([stays.id]);
|
|
763
|
+
expect(await store.findByKey("gone")).toBeNull();
|
|
764
|
+
expect(await store.findByDomain("gone-now.com")).toEqual([]);
|
|
765
|
+
const again = await store.create({ name: "Gone", nameKey: "gone", ...NO_SITE }, LIMITS);
|
|
766
|
+
expect(again.kind).toBe("created");
|
|
767
|
+
if (again.kind !== "created") throw new Error("setup");
|
|
768
|
+
expect([again.company.website, again.company.domain]).toEqual([null, null]);
|
|
769
|
+
expect(again.company.id === gone.id).toBe(false);
|
|
770
|
+
expect(await store.get(gone.id)).toBeNull();
|
|
771
|
+
expect(await store.findByDomain("gone-now.com")).toEqual([]);
|
|
772
|
+
expect([...await store.memberCounts([again.company.id, gone.id])]).toEqual([[again.company.id, 0]]);
|
|
773
|
+
const temp = await make(store, "Temp");
|
|
774
|
+
const keeper = await make(store, "Keeper");
|
|
775
|
+
await store.remove(temp.id);
|
|
776
|
+
expect((await store.update(keeper.id, { rename: { name: "Temp", nameKey: "temp" } })).kind).toBe("updated");
|
|
777
|
+
});
|
|
778
|
+
it('ids that do not exist ("missing", "ghost-1", "nothing") are answered as not found, never as a failure', async () => {
|
|
779
|
+
const { store, make } = await setup();
|
|
780
|
+
const real = await make(store, "Real");
|
|
781
|
+
for (const id of ["missing", "ghost-1", "nothing"]) {
|
|
782
|
+
expect(await store.get(id)).toBeNull();
|
|
783
|
+
expect(await store.update(id, { rename: { name: "X", nameKey: "x" } })).toEqual({ kind: "notFound" });
|
|
784
|
+
expect(await store.update(id, { website: site("x.com") })).toEqual({ kind: "notFound" });
|
|
785
|
+
expect(await store.update(id, {})).toEqual({ kind: "notFound" });
|
|
786
|
+
expect(await store.remove(id)).toEqual({ kind: "notFound" });
|
|
787
|
+
}
|
|
788
|
+
expect(ids(await store.getMany(["missing", "ghost-1", real.id, "nothing"]))).toEqual([real.id]);
|
|
789
|
+
});
|
|
790
|
+
it("findByDomain returns every company with exactly that domain: none for a company without one, a sub-domain, a longer name or another case; it follows a change of website", async () => {
|
|
791
|
+
const { store, make } = await setup();
|
|
792
|
+
const a = await make(store, "Taft", site("taft.com"));
|
|
793
|
+
const b = await make(store, "Taft Partners", site("taft.com"));
|
|
794
|
+
await make(store, "No site");
|
|
795
|
+
await make(store, "Mail", site("mail.taft.com"));
|
|
796
|
+
await make(store, "Taft UK", site("taft.co.uk"));
|
|
797
|
+
expect(ids(await store.findByDomain("taft.com"))).toEqual(ids([a, b]));
|
|
798
|
+
expect(await store.findByDomain("taft")).toEqual([]);
|
|
799
|
+
expect(await store.findByDomain("TAFT.COM")).toEqual([]);
|
|
800
|
+
expect(await store.findByDomain("aft.com")).toEqual([]);
|
|
801
|
+
expect(cores(await store.findByDomain("mail.taft.com")).map((c) => c.name)).toEqual(["Mail"]);
|
|
802
|
+
await store.update(a.id, { website: site("taftlaw.com") });
|
|
803
|
+
expect(ids(await store.findByDomain("taft.com"))).toEqual(ids([b]));
|
|
804
|
+
expect(ids(await store.findByDomain("taftlaw.com"))).toEqual(ids([a]));
|
|
805
|
+
await store.update(a.id, { website: NO_SITE });
|
|
806
|
+
expect(await store.findByDomain("taftlaw.com")).toEqual([]);
|
|
807
|
+
});
|
|
808
|
+
it("very long id lists work: tens of thousands of ids (most of them unknown) in one getMany and one memberCounts", async () => {
|
|
809
|
+
const { store, make } = await setup();
|
|
810
|
+
const real = await make(store, "Real");
|
|
811
|
+
const crowd = Array.from({ length: 4e4 }, (_, i) => `ghost-${i}`);
|
|
812
|
+
crowd.push(real.id);
|
|
813
|
+
expect(ids(await store.getMany(crowd))).toEqual([real.id]);
|
|
814
|
+
expect([...await store.memberCounts(crowd)]).toEqual([[real.id, 0]]);
|
|
815
|
+
});
|
|
816
|
+
it("memberCounts gives every company a zero when nobody belongs, only the asked-for ones, an empty map for an empty ask, and nothing for an unknown id", async () => {
|
|
817
|
+
const { store, make } = await setup();
|
|
818
|
+
const taft = await make(store, "Taft");
|
|
819
|
+
const bolt = await make(store, "Bolt");
|
|
820
|
+
expect([...await store.memberCounts()].sort()).toEqual([[bolt.id, 0], [taft.id, 0]].sort());
|
|
821
|
+
expect([...await store.memberCounts([taft.id, "missing"])]).toEqual([[taft.id, 0]]);
|
|
822
|
+
expect((await store.memberCounts([])).size).toBe(0);
|
|
823
|
+
await store.remove(bolt.id);
|
|
824
|
+
expect([...await store.memberCounts()]).toEqual([[taft.id, 0]]);
|
|
825
|
+
expect([...await store.memberCounts([bolt.id, taft.id])]).toEqual([[taft.id, 0]]);
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// src/companies/server/contract-members.ts
|
|
831
|
+
function defineCompanyStoreMembersContract(kit) {
|
|
832
|
+
const { describe, it, expect } = kit.api;
|
|
833
|
+
const { setup, harness } = kit;
|
|
834
|
+
describe("CompanyStore contract: members (adding)", () => {
|
|
835
|
+
it("personExists is true for a person of this workspace and false for an unknown id, an empty text, a prefix of a real id and a removed person", async () => {
|
|
836
|
+
const { store, person, workspace } = await setup();
|
|
837
|
+
const alive = await person();
|
|
838
|
+
const doomed = await person();
|
|
839
|
+
expect(await store.personExists(alive)).toBe(true);
|
|
840
|
+
expect(await store.personExists(doomed)).toBe(true);
|
|
841
|
+
for (const odd of ["missing", "", `${alive}x`, alive.slice(0, -1), `${alive},${doomed}`]) expect(await store.personExists(odd)).toBe(false);
|
|
842
|
+
await harness.removePerson(workspace, doomed);
|
|
843
|
+
expect(await store.personExists(doomed)).toBe(false);
|
|
844
|
+
expect(await store.personExists(alive)).toBe(true);
|
|
845
|
+
});
|
|
846
|
+
it('addMember answers with the membership: company, person and role exactly as given (null stays null; spaces, case, quotes, emoji and the text "null" are kept), and it shows in members, companiesForPeople and memberCounts', async () => {
|
|
847
|
+
const { store, make, person } = await setup();
|
|
848
|
+
const company = await make(store, "Acme");
|
|
849
|
+
const roles = [null, "Partner", " Spaced Out ", "Case KEPT", `O'Brien "Chair"; DROP TABLE "Member"; --`, "\xDCn\u0131\u0301c\xF6d\xE9 \u65E5\u672C\u8A9E \u{1F680}", "100%_sure\\", "null", "NULL", "x".repeat(200)];
|
|
850
|
+
const expected = [];
|
|
851
|
+
for (const role of roles) {
|
|
852
|
+
const who = await person();
|
|
853
|
+
const added = await store.addMember(company.id, who, role, ACTOR, MEMBER_LIMITS);
|
|
854
|
+
expect(added).toEqual({ kind: "added", member: { companyId: company.id, personId: who, role } });
|
|
855
|
+
expected.push({ companyId: company.id, personId: who, role });
|
|
856
|
+
}
|
|
857
|
+
expect(byPerson(await store.members(company.id))).toEqual(byPerson(expected));
|
|
858
|
+
const chips = await store.companiesForPeople(expected.map((member) => member.personId));
|
|
859
|
+
expect(chips.size).toBe(roles.length);
|
|
860
|
+
for (const member of expected) expect(chips.get(member.personId)?.map((chip) => [core(chip.company), chip.role])).toEqual([[core(company), member.role]]);
|
|
861
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, roles.length]]);
|
|
862
|
+
});
|
|
863
|
+
it("addMember refuses in a fixed order: companyGone, then personGone, then already, then memberLimit", async () => {
|
|
864
|
+
const { store, make, person, join, workspace } = await setup();
|
|
865
|
+
const company = await make(store, "Acme");
|
|
866
|
+
const goneCompany = await make(store, "Gone Co");
|
|
867
|
+
await store.remove(goneCompany.id);
|
|
868
|
+
const member = await person();
|
|
869
|
+
await join(company.id, member, "Partner");
|
|
870
|
+
const gonePerson = await person();
|
|
871
|
+
await harness.removePerson(workspace, gonePerson);
|
|
872
|
+
const full = { maxMembers: 1 };
|
|
873
|
+
expect(await store.addMember(goneCompany.id, gonePerson, null, ACTOR, full)).toEqual({ kind: "companyGone" });
|
|
874
|
+
expect(await store.addMember("missing", member, null, ACTOR, full)).toEqual({ kind: "companyGone" });
|
|
875
|
+
expect(await store.addMember(company.id, gonePerson, null, ACTOR, full)).toEqual({ kind: "personGone" });
|
|
876
|
+
expect(await store.addMember(company.id, "missing", null, ACTOR, full)).toEqual({ kind: "personGone" });
|
|
877
|
+
expect((await store.addMember(company.id, member, "Chair", ACTOR, full)).kind).toBe("already");
|
|
878
|
+
const zero = { maxMembers: 0 };
|
|
879
|
+
expect(await store.addMember(goneCompany.id, member, null, ACTOR, zero)).toEqual({ kind: "companyGone" });
|
|
880
|
+
expect(await store.addMember(company.id, gonePerson, null, ACTOR, zero)).toEqual({ kind: "personGone" });
|
|
881
|
+
const newcomer = await person();
|
|
882
|
+
expect(await store.addMember(company.id, newcomer, null, ACTOR, full)).toEqual({ kind: "memberLimit" });
|
|
883
|
+
expect(byPerson(await store.members(company.id))).toEqual([{ companyId: company.id, personId: member, role: "Partner" }]);
|
|
884
|
+
});
|
|
885
|
+
it("an existing member is reported as already, with the membership as it is (the role is not touched, even by another role or by null) and without counting against the limit", async () => {
|
|
886
|
+
const { store, make, person, join } = await setup();
|
|
887
|
+
const company = await make(store, "Acme");
|
|
888
|
+
const who = await person();
|
|
889
|
+
await join(company.id, who, "Partner");
|
|
890
|
+
const one = { maxMembers: 1 };
|
|
891
|
+
const mine = { companyId: company.id, personId: who, role: "Partner" };
|
|
892
|
+
expect(await store.addMember(company.id, who, "Chair", ACTOR, one)).toEqual({ kind: "already", member: mine });
|
|
893
|
+
expect(await store.addMember(company.id, who, null, ACTOR, one)).toEqual({ kind: "already", member: mine });
|
|
894
|
+
expect(await store.addMember(company.id, who, "Partner", ACTOR, { maxMembers: 0 })).toEqual({ kind: "already", member: mine });
|
|
895
|
+
expect(await store.members(company.id)).toEqual([mine]);
|
|
896
|
+
await store.setRole(company.id, who, null);
|
|
897
|
+
expect(await store.addMember(company.id, who, "Chair", ACTOR, one)).toEqual({ kind: "already", member: { ...mine, role: null } });
|
|
898
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 1]]);
|
|
899
|
+
});
|
|
900
|
+
it("the member limit is per company: 0 admits nobody, N admits exactly N, and only a removed member or a removed person frees a place (a refused add, an already, a not-member removal or a missing company use or free none)", async () => {
|
|
901
|
+
const { store, make, person, workspace } = await setup();
|
|
902
|
+
const a = await make(store, "A");
|
|
903
|
+
const b = await make(store, "B");
|
|
904
|
+
const [p1, p2, p3, p4, p5] = [await person(), await person(), await person(), await person(), await person()];
|
|
905
|
+
expect(await store.addMember(a.id, p1, null, ACTOR, { maxMembers: 0 })).toEqual({ kind: "memberLimit" });
|
|
906
|
+
expect(await store.members(a.id)).toEqual([]);
|
|
907
|
+
const two = { maxMembers: 2 };
|
|
908
|
+
expect((await store.addMember(a.id, p1, null, ACTOR, two)).kind).toBe("added");
|
|
909
|
+
expect((await store.addMember(a.id, p2, null, ACTOR, two)).kind).toBe("added");
|
|
910
|
+
expect((await store.addMember(b.id, p1, null, ACTOR, two)).kind).toBe("added");
|
|
911
|
+
expect((await store.addMember(b.id, p2, null, ACTOR, two)).kind).toBe("added");
|
|
912
|
+
expect(await store.addMember(a.id, p3, null, ACTOR, two)).toEqual({ kind: "memberLimit" });
|
|
913
|
+
expect((await store.addMember(a.id, p1, "Again", ACTOR, two)).kind).toBe("already");
|
|
914
|
+
expect(await store.removeMember(a.id, p3)).toEqual({ kind: "notMember" });
|
|
915
|
+
expect(await store.setRole(a.id, p3, "Chair")).toEqual({ kind: "notMember" });
|
|
916
|
+
expect(await store.addMember("missing", p3, null, ACTOR, two)).toEqual({ kind: "companyGone" });
|
|
917
|
+
expect(await store.addMember(a.id, "missing", null, ACTOR, two)).toEqual({ kind: "personGone" });
|
|
918
|
+
expect(await store.addMember(a.id, p3, null, ACTOR, two)).toEqual({ kind: "memberLimit" });
|
|
919
|
+
expect((await store.removeMember(a.id, p1)).kind).toBe("removed");
|
|
920
|
+
expect((await store.addMember(a.id, p3, null, ACTOR, two)).kind).toBe("added");
|
|
921
|
+
expect(await store.addMember(a.id, p4, null, ACTOR, two)).toEqual({ kind: "memberLimit" });
|
|
922
|
+
await harness.removePerson(workspace, p2);
|
|
923
|
+
expect((await store.addMember(a.id, p4, null, ACTOR, two)).kind).toBe("added");
|
|
924
|
+
expect(await store.addMember(a.id, p5, null, ACTOR, two)).toEqual({ kind: "memberLimit" });
|
|
925
|
+
expect(byPerson(await store.members(a.id)).map((member) => member.personId)).toEqual([p3, p4].sort());
|
|
926
|
+
});
|
|
927
|
+
it("nothing is capped: 120 people in one company and one person in 111 companies, seen the same way by members, memberCounts (also for long lists of companies and of ids) and companiesForPeople", async () => {
|
|
928
|
+
const { store, make, person, join } = await setup();
|
|
929
|
+
const hub = await make(store, "Hub");
|
|
930
|
+
const star = await person();
|
|
931
|
+
const everyone = [star];
|
|
932
|
+
await join(hub.id, star);
|
|
933
|
+
for (let i = 0; i < 119; i++) {
|
|
934
|
+
const who = await person();
|
|
935
|
+
await join(hub.id, who, `Role ${i}`);
|
|
936
|
+
everyone.push(who);
|
|
937
|
+
}
|
|
938
|
+
const others = [];
|
|
939
|
+
for (let i = 0; i < 110; i++) {
|
|
940
|
+
const result = await store.create({ name: `Other ${i}`, nameKey: `other ${i}`, website: null, domain: null }, { maxCompanies: 1e3 });
|
|
941
|
+
if (result.kind !== "created") throw new Error(`setup: could not create Other ${i}: ${result.kind}`);
|
|
942
|
+
await join(result.company.id, star, "Star");
|
|
943
|
+
others.push(result.company);
|
|
944
|
+
}
|
|
945
|
+
expect((await store.members(hub.id)).length).toBe(120);
|
|
946
|
+
expect(byPerson(await store.members(hub.id)).map((member) => member.personId)).toEqual([...everyone].sort());
|
|
947
|
+
expect([...await store.memberCounts([hub.id])]).toEqual([[hub.id, 120]]);
|
|
948
|
+
const counts = await store.memberCounts();
|
|
949
|
+
expect(counts.size).toBe(111);
|
|
950
|
+
expect(counts.get(hub.id)).toBe(120);
|
|
951
|
+
expect(others.every((other) => counts.get(other.id) === 1)).toBe(true);
|
|
952
|
+
const asked = await store.memberCounts([hub, ...others].map((company) => company.id));
|
|
953
|
+
expect(asked.size).toBe(111);
|
|
954
|
+
expect(asked.get(hub.id)).toBe(120);
|
|
955
|
+
expect(others.every((other) => asked.get(other.id) === 1)).toBe(true);
|
|
956
|
+
const crowd = Array.from({ length: 4e4 }, (_, i) => `ghost-${i}`);
|
|
957
|
+
crowd.push(hub.id);
|
|
958
|
+
expect([...await store.memberCounts(crowd)]).toEqual([[hub.id, 120]]);
|
|
959
|
+
const chips = await store.companiesForPeople(everyone);
|
|
960
|
+
expect(chips.size).toBe(120);
|
|
961
|
+
expect(ids((chips.get(star) ?? []).map((chip) => chip.company))).toEqual(ids([hub, ...others]));
|
|
962
|
+
expect(everyone.slice(1).every((who) => chips.get(who)?.length === 1)).toBe(true);
|
|
963
|
+
});
|
|
964
|
+
it("removing a company takes its memberships with it and reports how many; the people stay, other companies keep theirs, and a company made again under the old key starts with nobody", async () => {
|
|
965
|
+
const { store, make, person, join } = await setup();
|
|
966
|
+
const a = await make(store, "A");
|
|
967
|
+
const b = await make(store, "B");
|
|
968
|
+
const [p1, p2, p3] = [await person(), await person(), await person()];
|
|
969
|
+
await join(a.id, p1, "Partner");
|
|
970
|
+
await join(a.id, p2);
|
|
971
|
+
await join(a.id, p3);
|
|
972
|
+
await join(b.id, p3, "Chair");
|
|
973
|
+
await join(b.id, p2);
|
|
974
|
+
const removed = await store.remove(a.id);
|
|
975
|
+
expect(removed.kind === "removed" && removed.memberCount).toBe(3);
|
|
976
|
+
expect(await store.members(a.id)).toEqual([]);
|
|
977
|
+
expect(await store.memberCounts([a.id])).toEqual(/* @__PURE__ */ new Map());
|
|
978
|
+
const chips = await store.companiesForPeople([p1, p2, p3]);
|
|
979
|
+
expect([...chips.keys()].sort()).toEqual([p2, p3].sort());
|
|
980
|
+
expect(chips.get(p3)?.map((chip) => [chip.company.id, chip.role])).toEqual([[b.id, "Chair"]]);
|
|
981
|
+
expect(byPerson(await store.members(b.id)).map((member) => member.personId)).toEqual([p2, p3].sort());
|
|
982
|
+
for (const who of [p1, p2, p3]) expect(await store.personExists(who)).toBe(true);
|
|
983
|
+
expect(await store.addMember(a.id, p1, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
984
|
+
expect(await store.setRole(a.id, p1, "x")).toEqual({ kind: "companyGone" });
|
|
985
|
+
expect(await store.removeMember(a.id, p1)).toEqual({ kind: "companyGone" });
|
|
986
|
+
const again = await store.create({ name: "A", nameKey: "a", website: null, domain: null }, { maxCompanies: 100 });
|
|
987
|
+
const newId = again.kind === "created" ? again.company.id : "";
|
|
988
|
+
expect(newId === a.id).toBe(false);
|
|
989
|
+
expect(await store.members(newId)).toEqual([]);
|
|
990
|
+
expect(await store.members(a.id)).toEqual([]);
|
|
991
|
+
expect((await store.companiesForPeople([p1])).size).toBe(0);
|
|
992
|
+
expect([...await store.memberCounts([newId])]).toEqual([[newId, 0]]);
|
|
993
|
+
});
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
// src/companies/server/contract-members-reads.ts
|
|
998
|
+
function defineCompanyStoreReadsContract(kit) {
|
|
999
|
+
const { describe, it, expect } = kit.api;
|
|
1000
|
+
const { setup, harness } = kit;
|
|
1001
|
+
describe("CompanyStore contract: members (reading)", () => {
|
|
1002
|
+
it("members lists the memberships of exactly one company, each with company, person and role; an unknown or removed company has none; every change shows at once", async () => {
|
|
1003
|
+
const { store, make, person, join, workspace } = await setup();
|
|
1004
|
+
const a = await make(store, "A");
|
|
1005
|
+
const b = await make(store, "B");
|
|
1006
|
+
const empty = await make(store, "Empty");
|
|
1007
|
+
const gone = await make(store, "Gone Co");
|
|
1008
|
+
const [p, q, r] = [await person(), await person(), await person()];
|
|
1009
|
+
await join(a.id, p, "Partner");
|
|
1010
|
+
await join(a.id, q);
|
|
1011
|
+
await join(a.id, r, "Clerk");
|
|
1012
|
+
await join(b.id, p, "B-p");
|
|
1013
|
+
await join(b.id, q);
|
|
1014
|
+
await join(gone.id, r);
|
|
1015
|
+
await store.remove(gone.id);
|
|
1016
|
+
expect((await store.addMember(a.id, q, "Again", ACTOR, MEMBER_LIMITS)).kind).toBe("already");
|
|
1017
|
+
expect(byPerson(await store.members(a.id))).toEqual(byPerson([{ companyId: a.id, personId: p, role: "Partner" }, { companyId: a.id, personId: q, role: null }, { companyId: a.id, personId: r, role: "Clerk" }]));
|
|
1018
|
+
expect(byPerson(await store.members(b.id))).toEqual(byPerson([{ companyId: b.id, personId: p, role: "B-p" }, { companyId: b.id, personId: q, role: null }]));
|
|
1019
|
+
expect(await store.members(empty.id)).toEqual([]);
|
|
1020
|
+
expect(await store.members("missing")).toEqual([]);
|
|
1021
|
+
expect(await store.members(gone.id)).toEqual([]);
|
|
1022
|
+
await store.setRole(a.id, q, "Intern");
|
|
1023
|
+
expect(byPerson(await store.members(a.id)).map((member) => member.role)).toEqual(byPerson([{ companyId: a.id, personId: p, role: "Partner" }, { companyId: a.id, personId: q, role: "Intern" }, { companyId: a.id, personId: r, role: "Clerk" }]).map((member) => member.role));
|
|
1024
|
+
await store.removeMember(a.id, p);
|
|
1025
|
+
expect(byPerson(await store.members(a.id)).map((member) => member.personId)).toEqual([q, r].sort());
|
|
1026
|
+
await harness.removePerson(workspace, r);
|
|
1027
|
+
expect(await store.members(a.id)).toEqual([{ companyId: a.id, personId: q, role: "Intern" }]);
|
|
1028
|
+
expect(byPerson(await store.members(b.id)).length).toBe(2);
|
|
1029
|
+
});
|
|
1030
|
+
it("companiesForPeople gives each person who has a company the CURRENT company and the CURRENT role; people without one, unknown or removed people and removed companies are absent; an empty ask is an empty map; a repeated id is one entry; 40,000 ids work", async () => {
|
|
1031
|
+
const { store, make, person, join, workspace } = await setup();
|
|
1032
|
+
const a = await make(store, "A", site("a.com"));
|
|
1033
|
+
const b = await make(store, "B");
|
|
1034
|
+
const gone = await make(store, "Gone Co");
|
|
1035
|
+
const [p, q, nobody, doomed] = [await person(), await person(), await person(), await person()];
|
|
1036
|
+
await join(a.id, p, "Partner");
|
|
1037
|
+
await join(b.id, p, "Chair");
|
|
1038
|
+
await join(a.id, q);
|
|
1039
|
+
await join(gone.id, nobody);
|
|
1040
|
+
await join(a.id, doomed);
|
|
1041
|
+
await store.remove(gone.id);
|
|
1042
|
+
await harness.removePerson(workspace, doomed);
|
|
1043
|
+
const chips = await store.companiesForPeople([p, q, nobody, doomed, "missing", ""]);
|
|
1044
|
+
expect([...chips.keys()].sort()).toEqual([p, q].sort());
|
|
1045
|
+
const leaver = await person();
|
|
1046
|
+
await join(a.id, leaver, "Temp");
|
|
1047
|
+
await join(b.id, leaver);
|
|
1048
|
+
await store.removeMember(a.id, leaver);
|
|
1049
|
+
const view = (list) => [...list ?? []].sort((x, y) => x.company.id.localeCompare(y.company.id)).map((chip) => [chip.company.id, chip.company.name, chip.company.domain, chip.role]);
|
|
1050
|
+
const byId = (rows) => rows.sort((x, y) => x[0].localeCompare(y[0]));
|
|
1051
|
+
expect(view(chips.get(p))).toEqual(byId([[a.id, "A", "a.com", "Partner"], [b.id, "B", null, "Chair"]]));
|
|
1052
|
+
expect(chips.get(q)?.map((chip) => [core(chip.company), chip.role])).toEqual([[core(a), null]]);
|
|
1053
|
+
expect((await store.companiesForPeople([])).size).toBe(0);
|
|
1054
|
+
const twice = await store.companiesForPeople([p, p, p]);
|
|
1055
|
+
expect(twice.size).toBe(1);
|
|
1056
|
+
expect(twice.get(p)?.length).toBe(2);
|
|
1057
|
+
await store.update(a.id, { rename: { name: "A Renamed", nameKey: "a renamed" }, website: site("renamed.com") });
|
|
1058
|
+
await store.setRole(a.id, p, "Founder");
|
|
1059
|
+
const fresh = await store.companiesForPeople([p, q]);
|
|
1060
|
+
expect(view(fresh.get(p))).toEqual(byId([[a.id, "A Renamed", "renamed.com", "Founder"], [b.id, "B", null, "Chair"]]));
|
|
1061
|
+
expect(view(fresh.get(q))).toEqual([[a.id, "A Renamed", "renamed.com", null]]);
|
|
1062
|
+
expect(view((await store.companiesForPeople([leaver])).get(leaver))).toEqual([[b.id, "B", null, null]]);
|
|
1063
|
+
await store.remove(b.id);
|
|
1064
|
+
expect((await store.companiesForPeople([leaver])).has(leaver)).toBe(false);
|
|
1065
|
+
expect(view((await store.companiesForPeople([p])).get(p))).toEqual([[a.id, "A Renamed", "renamed.com", "Founder"]]);
|
|
1066
|
+
const crowd = Array.from({ length: 4e4 }, (_, i) => `ghost-${i}`);
|
|
1067
|
+
crowd.push(p);
|
|
1068
|
+
const big = await store.companiesForPeople(crowd);
|
|
1069
|
+
expect([...big.keys()]).toEqual([p]);
|
|
1070
|
+
expect(big.get(p)?.map((chip) => chip.role)).toEqual(["Founder"]);
|
|
1071
|
+
});
|
|
1072
|
+
it("memberCounts follows every change as plain numbers: adds, an already, removals, a removed person, a removed company (absent), and only the asked-for ids", async () => {
|
|
1073
|
+
const { store, make, person, join, workspace } = await setup();
|
|
1074
|
+
const a = await make(store, "A");
|
|
1075
|
+
const b = await make(store, "B");
|
|
1076
|
+
const [p, q, r] = [await person(), await person(), await person()];
|
|
1077
|
+
const counts = async (...wanted) => [...await store.memberCounts(wanted.length ? wanted : void 0)].sort();
|
|
1078
|
+
expect(await counts()).toEqual([[a.id, 0], [b.id, 0]].sort());
|
|
1079
|
+
await join(a.id, p);
|
|
1080
|
+
await join(a.id, q);
|
|
1081
|
+
await join(b.id, p);
|
|
1082
|
+
await join(b.id, q);
|
|
1083
|
+
await join(b.id, r);
|
|
1084
|
+
expect(await counts()).toEqual([[a.id, 2], [b.id, 3]].sort());
|
|
1085
|
+
for (const [, count3] of await counts()) expect(typeof count3).toBe("number");
|
|
1086
|
+
await store.addMember(a.id, p, "Again", ACTOR, MEMBER_LIMITS);
|
|
1087
|
+
expect(await counts(a.id)).toEqual([[a.id, 2]]);
|
|
1088
|
+
expect(await counts(a.id, "missing", "")).toEqual([[a.id, 2]]);
|
|
1089
|
+
await store.removeMember(a.id, p);
|
|
1090
|
+
await store.removeMember(a.id, p);
|
|
1091
|
+
expect(await counts()).toEqual([[a.id, 1], [b.id, 3]].sort());
|
|
1092
|
+
await harness.removePerson(workspace, q);
|
|
1093
|
+
expect(await counts()).toEqual([[a.id, 0], [b.id, 2]].sort());
|
|
1094
|
+
await store.remove(b.id);
|
|
1095
|
+
expect(await counts()).toEqual([[a.id, 0]]);
|
|
1096
|
+
expect(await counts(b.id)).toEqual([]);
|
|
1097
|
+
});
|
|
1098
|
+
it("removing a person takes their memberships and nobody else's: the companies stay, the others keep their roles, and the person cannot be added again", async () => {
|
|
1099
|
+
const { store, make, person, join, workspace } = await setup();
|
|
1100
|
+
const a = await make(store, "A");
|
|
1101
|
+
const b = await make(store, "B");
|
|
1102
|
+
const [p, q] = [await person(), await person()];
|
|
1103
|
+
await join(a.id, p, "Partner");
|
|
1104
|
+
await join(b.id, p);
|
|
1105
|
+
await join(a.id, q, "Q");
|
|
1106
|
+
await harness.removePerson(workspace, p);
|
|
1107
|
+
expect(await store.members(a.id)).toEqual([{ companyId: a.id, personId: q, role: "Q" }]);
|
|
1108
|
+
expect(await store.members(b.id)).toEqual([]);
|
|
1109
|
+
expect([...await store.memberCounts()].sort()).toEqual([[a.id, 1], [b.id, 0]].sort());
|
|
1110
|
+
expect((await store.companiesForPeople([p, q])).has(p)).toBe(false);
|
|
1111
|
+
expect((await store.companiesForPeople([q])).get(q)?.map((chip) => chip.role)).toEqual(["Q"]);
|
|
1112
|
+
expect(await store.personExists(p)).toBe(false);
|
|
1113
|
+
expect(cores(await store.list())).toEqual(cores([a, b]));
|
|
1114
|
+
expect(await store.addMember(a.id, p, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "personGone" });
|
|
1115
|
+
});
|
|
1116
|
+
it("memberships and company records do not disturb each other: adding, changing and removing members leaves every company read as it was, and renaming or re-siting a company keeps its members", async () => {
|
|
1117
|
+
const { store, make, person, join } = await setup();
|
|
1118
|
+
const a = await make(store, "A", site("a.com"));
|
|
1119
|
+
const bare = await make(store, "Bare");
|
|
1120
|
+
const [p, q] = [await person(), await person()];
|
|
1121
|
+
const snapshot = async () => ({ list: cores(await store.list()), byKey: core(await store.findByKey("a")), byDomain: cores(await store.findByDomain("a.com")), one: core(await store.get(a.id)) });
|
|
1122
|
+
const start = await snapshot();
|
|
1123
|
+
await join(a.id, p, "Partner");
|
|
1124
|
+
await join(bare.id, q);
|
|
1125
|
+
await store.setRole(a.id, p, "Chair");
|
|
1126
|
+
await store.addMember(a.id, q, null, ACTOR, MEMBER_LIMITS);
|
|
1127
|
+
await store.removeMember(a.id, q);
|
|
1128
|
+
expect(await snapshot()).toEqual(start);
|
|
1129
|
+
const members = byPerson(await store.members(a.id));
|
|
1130
|
+
expect((await store.update(a.id, { rename: { name: "A Two", nameKey: "a two" } })).kind).toBe("updated");
|
|
1131
|
+
expect((await store.update(a.id, { website: site("two.com") })).kind).toBe("updated");
|
|
1132
|
+
expect((await store.update(a.id, {})).kind).toBe("updated");
|
|
1133
|
+
expect(byPerson(await store.members(a.id))).toEqual(members);
|
|
1134
|
+
expect([...await store.memberCounts([a.id])]).toEqual([[a.id, 1]]);
|
|
1135
|
+
expect(await store.members(bare.id)).toEqual([{ companyId: bare.id, personId: q, role: null }]);
|
|
1136
|
+
expect((await store.removeMember(bare.id, q)).kind).toBe("removed");
|
|
1137
|
+
expect(core(await store.get(bare.id))).toEqual(core(bare));
|
|
1138
|
+
expect([...await store.memberCounts([bare.id])]).toEqual([[bare.id, 0]]);
|
|
1139
|
+
});
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
// src/companies/server/contract-members-roles.ts
|
|
1144
|
+
function defineCompanyStoreRolesContract(kit) {
|
|
1145
|
+
const { describe, it, expect } = kit.api;
|
|
1146
|
+
const { setup, harness } = kit;
|
|
1147
|
+
describe("CompanyStore contract: members (roles and leaving)", () => {
|
|
1148
|
+
it("setRole answers with the membership before and after, each change starting from the last one: null clears, the same role is updated with before equal to after, and role text is kept exactly (read back through members and companiesForPeople)", async () => {
|
|
1149
|
+
const { store, make, person, join } = await setup();
|
|
1150
|
+
const company = await make(store, "Acme");
|
|
1151
|
+
const who = await person();
|
|
1152
|
+
await join(company.id, who, "Partner");
|
|
1153
|
+
const mine = (role) => ({ companyId: company.id, personId: who, role });
|
|
1154
|
+
expect(await store.setRole(company.id, who, "Chair")).toEqual({ kind: "updated", before: mine("Partner"), after: mine("Chair") });
|
|
1155
|
+
expect(await store.setRole(company.id, who, "Chair")).toEqual({ kind: "updated", before: mine("Chair"), after: mine("Chair") });
|
|
1156
|
+
expect(await store.setRole(company.id, who, null)).toEqual({ kind: "updated", before: mine("Chair"), after: mine(null) });
|
|
1157
|
+
expect((await store.companiesForPeople([who])).get(who)?.map((chip) => chip.role)).toEqual([null]);
|
|
1158
|
+
expect(await store.setRole(company.id, who, null)).toEqual({ kind: "updated", before: mine(null), after: mine(null) });
|
|
1159
|
+
let previous = null;
|
|
1160
|
+
for (const role of [" Spaced Out ", "Case KEPT", `O'Brien "Chair"; DROP TABLE "Member"; --`, "\xDCn\u0131\u0301c\xF6d\xE9 \u65E5\u672C\u8A9E \u{1F680}", "100%_sure\\", "null", "NULL", "x".repeat(200)]) {
|
|
1161
|
+
expect(await store.setRole(company.id, who, role)).toEqual({ kind: "updated", before: mine(previous), after: mine(role) });
|
|
1162
|
+
expect(await store.members(company.id)).toEqual([mine(role)]);
|
|
1163
|
+
expect((await store.companiesForPeople([who])).get(who)?.map((chip) => chip.role)).toEqual([role]);
|
|
1164
|
+
previous = role;
|
|
1165
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
it("setRole refuses in a fixed order, companyGone then notMember (a person who is not in THIS company, a removed person and an unknown person included), and a real change touches only that one membership", async () => {
|
|
1168
|
+
const { store, make, person, join, workspace } = await setup();
|
|
1169
|
+
const a = await make(store, "A");
|
|
1170
|
+
const b = await make(store, "B");
|
|
1171
|
+
const goneCompany = await make(store, "Gone Co");
|
|
1172
|
+
await store.remove(goneCompany.id);
|
|
1173
|
+
const [p, q, r] = [await person(), await person(), await person()];
|
|
1174
|
+
const gonePerson = await person();
|
|
1175
|
+
await join(a.id, p, "A-p");
|
|
1176
|
+
await join(a.id, q, "A-q");
|
|
1177
|
+
await join(b.id, p, "B-p");
|
|
1178
|
+
await harness.removePerson(workspace, gonePerson);
|
|
1179
|
+
const before = async () => [byPerson(await store.members(a.id)), byPerson(await store.members(b.id))];
|
|
1180
|
+
const start = await before();
|
|
1181
|
+
expect(await store.setRole(goneCompany.id, gonePerson, "x")).toEqual({ kind: "companyGone" });
|
|
1182
|
+
expect(await store.setRole("missing", p, "x")).toEqual({ kind: "companyGone" });
|
|
1183
|
+
expect(await store.setRole(a.id, r, "x")).toEqual({ kind: "notMember" });
|
|
1184
|
+
expect(await store.setRole(a.id, gonePerson, "x")).toEqual({ kind: "notMember" });
|
|
1185
|
+
expect(await store.setRole(a.id, "missing", "x")).toEqual({ kind: "notMember" });
|
|
1186
|
+
expect(await store.setRole(b.id, q, "x")).toEqual({ kind: "notMember" });
|
|
1187
|
+
expect(await before()).toEqual(start);
|
|
1188
|
+
const companyBefore = core(await store.get(a.id));
|
|
1189
|
+
expect((await store.setRole(a.id, p, "New")).kind).toBe("updated");
|
|
1190
|
+
expect(await store.members(a.id).then(byPerson)).toEqual(byPerson([{ companyId: a.id, personId: p, role: "New" }, { companyId: a.id, personId: q, role: "A-q" }]));
|
|
1191
|
+
expect(await store.members(b.id)).toEqual([{ companyId: b.id, personId: p, role: "B-p" }]);
|
|
1192
|
+
expect(core(await store.get(a.id))).toEqual(companyBefore);
|
|
1193
|
+
});
|
|
1194
|
+
it("removeMember answers with the membership as it is now, once only; it touches only that membership, and adding the person again starts clean with the role given then", async () => {
|
|
1195
|
+
const { store, make, person, join, workspace } = await setup();
|
|
1196
|
+
const a = await make(store, "A");
|
|
1197
|
+
const b = await make(store, "B");
|
|
1198
|
+
const goneCompany = await make(store, "Gone Co");
|
|
1199
|
+
await store.remove(goneCompany.id);
|
|
1200
|
+
const [p, q] = [await person(), await person()];
|
|
1201
|
+
await join(a.id, p, "Partner");
|
|
1202
|
+
await join(a.id, q, "Q");
|
|
1203
|
+
await join(b.id, p, "B-p");
|
|
1204
|
+
await store.setRole(a.id, p, "Chair");
|
|
1205
|
+
expect(await store.removeMember(goneCompany.id, p)).toEqual({ kind: "companyGone" });
|
|
1206
|
+
expect(await store.removeMember("missing", p)).toEqual({ kind: "companyGone" });
|
|
1207
|
+
expect(await store.removeMember(a.id, "missing")).toEqual({ kind: "notMember" });
|
|
1208
|
+
expect(await store.removeMember(b.id, q)).toEqual({ kind: "notMember" });
|
|
1209
|
+
expect(await store.members(a.id).then((list) => list.length)).toBe(2);
|
|
1210
|
+
expect(await store.removeMember(a.id, p)).toEqual({ kind: "removed", member: { companyId: a.id, personId: p, role: "Chair" } });
|
|
1211
|
+
expect(await store.removeMember(a.id, p)).toEqual({ kind: "notMember" });
|
|
1212
|
+
expect(await store.members(a.id)).toEqual([{ companyId: a.id, personId: q, role: "Q" }]);
|
|
1213
|
+
expect(await store.members(b.id)).toEqual([{ companyId: b.id, personId: p, role: "B-p" }]);
|
|
1214
|
+
expect([...await store.memberCounts([a.id, b.id])].sort()).toEqual([[a.id, 1], [b.id, 1]].sort());
|
|
1215
|
+
expect(await store.personExists(p)).toBe(true);
|
|
1216
|
+
expect(core(await store.get(a.id)).id).toBe(a.id);
|
|
1217
|
+
expect(await store.addMember(a.id, p, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "added", member: { companyId: a.id, personId: p, role: null } });
|
|
1218
|
+
await harness.removePerson(workspace, q);
|
|
1219
|
+
expect(await store.removeMember(a.id, q)).toEqual({ kind: "notMember" });
|
|
1220
|
+
});
|
|
1221
|
+
it('ids that do not exist (an empty text, a prefix, a list in one text, quotes) are "not found" for every membership method and write nothing', async () => {
|
|
1222
|
+
const { store, make, person, join } = await setup();
|
|
1223
|
+
const a = await make(store, "A");
|
|
1224
|
+
const p = await person();
|
|
1225
|
+
await join(a.id, p, "Partner");
|
|
1226
|
+
const mine = [{ companyId: a.id, personId: p, role: "Partner" }];
|
|
1227
|
+
for (const odd of ["", "missing", `${a.id}x`, a.id.slice(0, -1), `${a.id},${a.id}`, `x,${a.id}`, "' OR '1'='1"]) {
|
|
1228
|
+
expect(await store.addMember(odd, p, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1229
|
+
expect(await store.setRole(odd, p, "x")).toEqual({ kind: "companyGone" });
|
|
1230
|
+
expect(await store.removeMember(odd, p)).toEqual({ kind: "companyGone" });
|
|
1231
|
+
expect(await store.members(odd)).toEqual([]);
|
|
1232
|
+
expect((await store.memberCounts([odd])).size).toBe(0);
|
|
1233
|
+
}
|
|
1234
|
+
for (const odd of ["", "missing", `${p}x`, p.slice(0, -1), `${p},${p}`, `x,${p}`, "' OR '1'='1"]) {
|
|
1235
|
+
expect(await store.addMember(a.id, odd, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "personGone" });
|
|
1236
|
+
expect(await store.setRole(a.id, odd, "x")).toEqual({ kind: "notMember" });
|
|
1237
|
+
expect(await store.removeMember(a.id, odd)).toEqual({ kind: "notMember" });
|
|
1238
|
+
expect((await store.companiesForPeople([odd])).size).toBe(0);
|
|
1239
|
+
}
|
|
1240
|
+
expect(await store.members(a.id)).toEqual(mine);
|
|
1241
|
+
expect((await store.companiesForPeople([p])).get(p)?.length).toBe(1);
|
|
1242
|
+
});
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
// src/companies/server/contract-concurrency.ts
|
|
1247
|
+
var BIG = { maxCompanies: 1e3 };
|
|
1248
|
+
var keyOf = (name) => name.toLowerCase();
|
|
1249
|
+
function count(results, kind) {
|
|
1250
|
+
return results.filter((result) => result.kind === kind).length;
|
|
1251
|
+
}
|
|
1252
|
+
function many(n, make) {
|
|
1253
|
+
return Promise.all(Array.from({ length: n }, (_, i) => make(i)));
|
|
1254
|
+
}
|
|
1255
|
+
function defineCompanyStoreConcurrencyContract(kit) {
|
|
1256
|
+
const { describe, it, expect } = kit.api;
|
|
1257
|
+
const { setup, harness } = kit;
|
|
1258
|
+
describe("CompanyStore contract: simultaneous callers (companies)", () => {
|
|
1259
|
+
it("ten creates with different names at a limit of 3 give exactly 3 created and 7 limit, and the list holds exactly 3", async () => {
|
|
1260
|
+
const { store } = await setup();
|
|
1261
|
+
const results = await many(10, (i) => store.create({ name: `C${i}`, nameKey: `c${i}`, ...NO_SITE }, { maxCompanies: 3 }));
|
|
1262
|
+
expect([count(results, "created"), count(results, "limit")]).toEqual([3, 7]);
|
|
1263
|
+
expect((await store.list()).length).toBe(3);
|
|
1264
|
+
});
|
|
1265
|
+
it("ten creates of the same name give exactly one created and nine duplicates, and the list holds exactly one", async () => {
|
|
1266
|
+
const { store } = await setup();
|
|
1267
|
+
const results = await many(10, () => store.create({ name: "Same", nameKey: "same", ...NO_SITE }, BIG));
|
|
1268
|
+
expect([count(results, "created"), count(results, "duplicate")]).toEqual([1, 9]);
|
|
1269
|
+
expect((await store.list()).length).toBe(1);
|
|
1270
|
+
});
|
|
1271
|
+
it("two renames onto the same key give one updated and one duplicate; the winner holds the key and the loser keeps its own", async () => {
|
|
1272
|
+
const { store, make } = await setup();
|
|
1273
|
+
const a = await make(store, "A");
|
|
1274
|
+
const b = await make(store, "B");
|
|
1275
|
+
const results = await Promise.all([store.update(a.id, { rename: { name: "Target", nameKey: "target" } }), store.update(b.id, { rename: { name: "Target", nameKey: "target" } })]);
|
|
1276
|
+
expect([count(results, "updated"), count(results, "duplicate")]).toEqual([1, 1]);
|
|
1277
|
+
const winner = await store.findByKey("target");
|
|
1278
|
+
expect([a.id, b.id].includes(winner.id)).toBe(true);
|
|
1279
|
+
const loser = winner.id === a.id ? b : a;
|
|
1280
|
+
expect((await store.get(loser.id))?.nameKey).toBe(loser.nameKey);
|
|
1281
|
+
expect((await store.list()).length).toBe(2);
|
|
1282
|
+
});
|
|
1283
|
+
it("a create and a rename onto the same key at once give exactly one winner", async () => {
|
|
1284
|
+
const { store, make } = await setup();
|
|
1285
|
+
const old = await make(store, "Old");
|
|
1286
|
+
const [created, renamed] = await Promise.all([store.create({ name: "Target", nameKey: "target", ...NO_SITE }, BIG), store.update(old.id, { rename: { name: "Target", nameKey: "target" } })]);
|
|
1287
|
+
expect([created.kind, renamed.kind].filter((kind) => kind === "duplicate").length).toBe(1);
|
|
1288
|
+
expect([created.kind, renamed.kind].filter((kind) => kind === "created" || kind === "updated").length).toBe(1);
|
|
1289
|
+
const holders = (await store.list()).filter((company) => company.nameKey === "target");
|
|
1290
|
+
expect(holders.length).toBe(1);
|
|
1291
|
+
expect(created.kind === "created" ? holders[0]?.id === created.company.id : holders[0]?.id === old.id).toBe(true);
|
|
1292
|
+
});
|
|
1293
|
+
it("two updates of one company at once give one before/after chain: exactly one starts from the original, the other starts from what the first left, and the last one is what stays", async () => {
|
|
1294
|
+
const { store, make } = await setup();
|
|
1295
|
+
const acme = await make(store, "Acme", site("acme.com"));
|
|
1296
|
+
const [x, y] = await Promise.all([store.update(acme.id, { website: site("x.com") }), store.update(acme.id, { website: site("y.com") })]);
|
|
1297
|
+
if (x.kind !== "updated" || y.kind !== "updated") throw new Error("both updates must succeed");
|
|
1298
|
+
const fromOriginal = [x, y].filter((result) => result.before.website === acme.website);
|
|
1299
|
+
expect(fromOriginal.length).toBe(1);
|
|
1300
|
+
const first = fromOriginal[0];
|
|
1301
|
+
const second = first === x ? y : x;
|
|
1302
|
+
expect(core(second.before)).toEqual(core(first.after));
|
|
1303
|
+
expect(core(await store.get(acme.id))).toEqual(core(second.after));
|
|
1304
|
+
});
|
|
1305
|
+
it("ten removes of one company give exactly one removed and nine notFound, and the members detached are reported once", async () => {
|
|
1306
|
+
const { store, make, person, join } = await setup();
|
|
1307
|
+
const acme = await make(store, "Acme");
|
|
1308
|
+
await join(acme.id, await person());
|
|
1309
|
+
await join(acme.id, await person());
|
|
1310
|
+
const results = await many(10, () => store.remove(acme.id));
|
|
1311
|
+
expect([count(results, "removed"), count(results, "notFound")]).toEqual([1, 9]);
|
|
1312
|
+
const removed = results.find((result) => result.kind === "removed");
|
|
1313
|
+
expect(removed?.kind === "removed" && removed.memberCount).toBe(2);
|
|
1314
|
+
expect(await store.get(acme.id)).toBeNull();
|
|
1315
|
+
expect(await store.members(acme.id)).toEqual([]);
|
|
1316
|
+
});
|
|
1317
|
+
it("a rename and a website-only patch of one company at once both land: the company ends with the new name and the new website", async () => {
|
|
1318
|
+
const { store, make } = await setup();
|
|
1319
|
+
const acme = await make(store, "Acme", site("acme.com"));
|
|
1320
|
+
const [renamed, resited] = await Promise.all([store.update(acme.id, { rename: { name: "Beta", nameKey: "beta" } }), store.update(acme.id, { website: site("beta.com") })]);
|
|
1321
|
+
expect([renamed.kind, resited.kind]).toEqual(["updated", "updated"]);
|
|
1322
|
+
expect(core(await store.get(acme.id))).toEqual({ id: acme.id, name: "Beta", nameKey: "beta", website: "https://beta.com", domain: "beta.com" });
|
|
1323
|
+
expect((await store.findByKey("beta"))?.id).toBe(acme.id);
|
|
1324
|
+
expect((await store.findByDomain("beta.com")).length).toBe(1);
|
|
1325
|
+
expect(await store.findByDomain("acme.com")).toEqual([]);
|
|
1326
|
+
});
|
|
1327
|
+
it("two workspaces racing at the same limit each get their own places: 10 creates in each at a limit of 3 give 3 created in each", async () => {
|
|
1328
|
+
const { store } = await setup();
|
|
1329
|
+
const other = harness.storeFor(await harness.newWorkspace());
|
|
1330
|
+
const make = (target, tag) => (i) => target.create({ name: `${tag}${i}`, nameKey: keyOf(`${tag}${i}`), ...NO_SITE }, { maxCompanies: 3 });
|
|
1331
|
+
const [mine, theirs] = await Promise.all([many(10, make(store, "A")), many(10, make(other, "B"))]);
|
|
1332
|
+
expect([count(mine, "created"), count(mine, "limit")]).toEqual([3, 7]);
|
|
1333
|
+
expect([count(theirs, "created"), count(theirs, "limit")]).toEqual([3, 7]);
|
|
1334
|
+
expect([(await store.list()).length, (await other.list()).length]).toEqual([3, 3]);
|
|
1335
|
+
});
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
// src/companies/server/contract-concurrency-members.ts
|
|
1340
|
+
function count2(results, kind) {
|
|
1341
|
+
return results.filter((result) => result.kind === kind).length;
|
|
1342
|
+
}
|
|
1343
|
+
function many2(n, make) {
|
|
1344
|
+
return Promise.all(Array.from({ length: n }, (_, i) => make(i)));
|
|
1345
|
+
}
|
|
1346
|
+
function defineCompanyStoreMembersConcurrencyContract(kit) {
|
|
1347
|
+
const { describe, it, expect } = kit.api;
|
|
1348
|
+
const { setup, harness } = kit;
|
|
1349
|
+
describe("CompanyStore contract: simultaneous callers (memberships)", () => {
|
|
1350
|
+
it("ten adds of ten different people at a member limit of 3 give exactly 3 added and 7 memberLimit, and the company holds exactly 3", async () => {
|
|
1351
|
+
const { store, make, person } = await setup();
|
|
1352
|
+
const company = await make(store, "Acme");
|
|
1353
|
+
const people = await many2(10, () => person());
|
|
1354
|
+
const results = await many2(10, (i) => store.addMember(company.id, people[i], null, ACTOR, { maxMembers: 3 }));
|
|
1355
|
+
expect([count2(results, "added"), count2(results, "memberLimit")]).toEqual([3, 7]);
|
|
1356
|
+
expect((await store.members(company.id)).length).toBe(3);
|
|
1357
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 3]]);
|
|
1358
|
+
});
|
|
1359
|
+
it("ten adds of the same person give exactly one added and nine already, all reporting the role that stayed", async () => {
|
|
1360
|
+
const { store, make, person } = await setup();
|
|
1361
|
+
const company = await make(store, "Acme");
|
|
1362
|
+
const who = await person();
|
|
1363
|
+
const results = await many2(10, (i) => store.addMember(company.id, who, `Role ${i}`, ACTOR, MEMBER_LIMITS));
|
|
1364
|
+
expect([count2(results, "added"), count2(results, "already")]).toEqual([1, 9]);
|
|
1365
|
+
const stored = await store.members(company.id);
|
|
1366
|
+
expect(stored.length).toBe(1);
|
|
1367
|
+
for (const result of results) expect(result.kind === "added" || result.kind === "already" ? result.member.role : "wrong").toBe(stored[0]?.role);
|
|
1368
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 1]]);
|
|
1369
|
+
});
|
|
1370
|
+
it("with one place left, simultaneous adds of the existing member are all already and exactly one newcomer gets the place", async () => {
|
|
1371
|
+
const { store, make, person, join } = await setup();
|
|
1372
|
+
const company = await make(store, "Acme");
|
|
1373
|
+
const member = await person();
|
|
1374
|
+
await join(company.id, member, "Partner");
|
|
1375
|
+
const newcomers = await many2(5, () => person());
|
|
1376
|
+
const two = { maxMembers: 2 };
|
|
1377
|
+
const results = await Promise.all([...Array.from({ length: 5 }, () => store.addMember(company.id, member, null, ACTOR, two)), ...newcomers.map((who) => store.addMember(company.id, who, null, ACTOR, two))]);
|
|
1378
|
+
expect([count2(results, "already"), count2(results, "added"), count2(results, "memberLimit")]).toEqual([5, 1, 4]);
|
|
1379
|
+
expect((await store.members(company.id)).length).toBe(2);
|
|
1380
|
+
expect((await store.members(company.id)).some((m) => m.personId === member && m.role === "Partner")).toBe(true);
|
|
1381
|
+
});
|
|
1382
|
+
it("two simultaneous role changes give one before/after chain: exactly one starts from the original role, the other from what the first left, and the last one stays", async () => {
|
|
1383
|
+
const { store, make, person, join } = await setup();
|
|
1384
|
+
const company = await make(store, "Acme");
|
|
1385
|
+
const who = await person();
|
|
1386
|
+
await join(company.id, who, "Original");
|
|
1387
|
+
const [x, y] = await Promise.all([store.setRole(company.id, who, "X"), store.setRole(company.id, who, "Y")]);
|
|
1388
|
+
if (x.kind !== "updated" || y.kind !== "updated") throw new Error("both role changes must succeed");
|
|
1389
|
+
const fromOriginal = [x, y].filter((result) => result.before.role === "Original");
|
|
1390
|
+
expect(fromOriginal.length).toBe(1);
|
|
1391
|
+
const first = fromOriginal[0];
|
|
1392
|
+
const second = first === x ? y : x;
|
|
1393
|
+
expect(second.before).toEqual(first.after);
|
|
1394
|
+
expect(await store.members(company.id)).toEqual([second.after]);
|
|
1395
|
+
});
|
|
1396
|
+
it("ten removals of one membership give exactly one removed and nine notMember, and nobody is left", async () => {
|
|
1397
|
+
const { store, make, person, join } = await setup();
|
|
1398
|
+
const company = await make(store, "Acme");
|
|
1399
|
+
const who = await person();
|
|
1400
|
+
await join(company.id, who, "Partner");
|
|
1401
|
+
const results = await many2(10, () => store.removeMember(company.id, who));
|
|
1402
|
+
expect([count2(results, "removed"), count2(results, "notMember")]).toEqual([1, 9]);
|
|
1403
|
+
expect(await store.members(company.id)).toEqual([]);
|
|
1404
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 0]]);
|
|
1405
|
+
});
|
|
1406
|
+
it("a role change and a removal of the same membership at once leave nobody behind, and the removal reports the role the change did or did not reach", async () => {
|
|
1407
|
+
const { store, make, person, join } = await setup();
|
|
1408
|
+
const company = await make(store, "Acme");
|
|
1409
|
+
const who = await person();
|
|
1410
|
+
await join(company.id, who, "Original");
|
|
1411
|
+
const [changed, left] = await Promise.all([store.setRole(company.id, who, "New"), store.removeMember(company.id, who)]);
|
|
1412
|
+
expect(left.kind).toBe("removed");
|
|
1413
|
+
expect(["updated", "notMember"].includes(changed.kind)).toBe(true);
|
|
1414
|
+
expect(left.kind === "removed" && left.member.role).toBe(changed.kind === "updated" ? "New" : "Original");
|
|
1415
|
+
expect(await store.members(company.id)).toEqual([]);
|
|
1416
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 0]]);
|
|
1417
|
+
});
|
|
1418
|
+
it("a company removed while people are added, changed and removed leaves no membership behind, and every answer is one that can happen", async () => {
|
|
1419
|
+
const { store, make, person, join } = await setup();
|
|
1420
|
+
const company = await make(store, "Acme");
|
|
1421
|
+
const [p1, p2, p3, newcomer] = [await person(), await person(), await person(), await person()];
|
|
1422
|
+
for (const who of [p1, p2, p3]) await join(company.id, who, "Start");
|
|
1423
|
+
const [removed, added, changed, left] = await Promise.all([
|
|
1424
|
+
store.remove(company.id),
|
|
1425
|
+
store.addMember(company.id, newcomer, null, ACTOR, MEMBER_LIMITS),
|
|
1426
|
+
store.setRole(company.id, p1, "Changed"),
|
|
1427
|
+
store.removeMember(company.id, p2)
|
|
1428
|
+
]);
|
|
1429
|
+
expect(removed.kind === "removed" && removed.memberCount >= 2 && removed.memberCount <= 4).toBe(true);
|
|
1430
|
+
expect(["added", "companyGone"].includes(added.kind)).toBe(true);
|
|
1431
|
+
expect(["updated", "companyGone"].includes(changed.kind)).toBe(true);
|
|
1432
|
+
expect(["removed", "companyGone"].includes(left.kind)).toBe(true);
|
|
1433
|
+
expect(await store.members(company.id)).toEqual([]);
|
|
1434
|
+
expect((await store.memberCounts([company.id])).size).toBe(0);
|
|
1435
|
+
expect((await store.companiesForPeople([p1, p2, p3, newcomer])).size).toBe(0);
|
|
1436
|
+
});
|
|
1437
|
+
it("a person removed while being added to companies leaves no membership behind", async () => {
|
|
1438
|
+
const { store, make, person, workspace } = await setup();
|
|
1439
|
+
const a = await make(store, "A");
|
|
1440
|
+
const b = await make(store, "B");
|
|
1441
|
+
const who = await person();
|
|
1442
|
+
const [one, two] = await Promise.all([store.addMember(a.id, who, null, ACTOR, MEMBER_LIMITS), store.addMember(b.id, who, null, ACTOR, MEMBER_LIMITS), harness.removePerson(workspace, who)]).then(([x, y]) => [x, y]);
|
|
1443
|
+
expect(["added", "personGone"].includes(one.kind)).toBe(true);
|
|
1444
|
+
expect(["added", "personGone"].includes(two.kind)).toBe(true);
|
|
1445
|
+
expect(await store.personExists(who)).toBe(false);
|
|
1446
|
+
expect(byPerson([...await store.members(a.id), ...await store.members(b.id)])).toEqual([]);
|
|
1447
|
+
expect((await store.companiesForPeople([who])).size).toBe(0);
|
|
1448
|
+
expect([...await store.memberCounts()].sort()).toEqual([[a.id, 0], [b.id, 0]].sort());
|
|
1449
|
+
});
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
// src/companies/server/contract-hooks.ts
|
|
1454
|
+
function defineCompanyStoreHooksContract(kit) {
|
|
1455
|
+
const { describe, it, expect } = kit.api;
|
|
1456
|
+
const { setup, harness } = kit;
|
|
1457
|
+
const run = harness.armBeforeMemberWrite ? it : it.skip ?? it;
|
|
1458
|
+
const arm = (hook) => {
|
|
1459
|
+
if (!harness.armBeforeMemberWrite) throw new Error("This check needs harness.armBeforeMemberWrite (see contract-types.ts): supply the hook, or run with a runner that has it.skip");
|
|
1460
|
+
harness.armBeforeMemberWrite(hook);
|
|
1461
|
+
};
|
|
1462
|
+
describe("CompanyStore contract: deleted while being added", () => {
|
|
1463
|
+
run("a company removed between addMember's check and its write: the add answers companyGone, writes nothing, and the person is untouched", async () => {
|
|
1464
|
+
const { store, make, person } = await setup();
|
|
1465
|
+
const company = await make(store, "Acme");
|
|
1466
|
+
const who = await person();
|
|
1467
|
+
let ran = 0;
|
|
1468
|
+
arm(async () => {
|
|
1469
|
+
ran += 1;
|
|
1470
|
+
await store.remove(company.id);
|
|
1471
|
+
});
|
|
1472
|
+
expect(await store.addMember(company.id, who, "Partner", ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1473
|
+
expect(ran).toBe(1);
|
|
1474
|
+
expect(await store.members(company.id)).toEqual([]);
|
|
1475
|
+
expect((await store.memberCounts([company.id])).size).toBe(0);
|
|
1476
|
+
expect((await store.companiesForPeople([who])).size).toBe(0);
|
|
1477
|
+
expect(await store.personExists(who)).toBe(true);
|
|
1478
|
+
});
|
|
1479
|
+
run("a person removed between addMember's check and its write: the add answers personGone, writes nothing, and the company is untouched", async () => {
|
|
1480
|
+
const { store, make, person, workspace } = await setup();
|
|
1481
|
+
const company = await make(store, "Acme");
|
|
1482
|
+
const stays = await person();
|
|
1483
|
+
await store.addMember(company.id, stays, "Chair", ACTOR, MEMBER_LIMITS);
|
|
1484
|
+
const doomed = await person();
|
|
1485
|
+
let ran = 0;
|
|
1486
|
+
arm(async () => {
|
|
1487
|
+
ran += 1;
|
|
1488
|
+
await harness.removePerson(workspace, doomed);
|
|
1489
|
+
});
|
|
1490
|
+
expect(await store.addMember(company.id, doomed, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "personGone" });
|
|
1491
|
+
expect(ran).toBe(1);
|
|
1492
|
+
expect(byPerson(await store.members(company.id))).toEqual([{ companyId: company.id, personId: stays, role: "Chair" }]);
|
|
1493
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 1]]);
|
|
1494
|
+
expect((await store.companiesForPeople([doomed])).size).toBe(0);
|
|
1495
|
+
expect(await store.personExists(doomed)).toBe(false);
|
|
1496
|
+
expect((await store.get(company.id))?.id).toBe(company.id);
|
|
1497
|
+
});
|
|
1498
|
+
run("the company AND the person removed between addMember's check and its write: the add answers companyGone (the company is looked at first), and nothing is written", async () => {
|
|
1499
|
+
const { store, make, person, workspace } = await setup();
|
|
1500
|
+
const company = await make(store, "Acme");
|
|
1501
|
+
const doomed = await person();
|
|
1502
|
+
arm(async () => {
|
|
1503
|
+
await store.remove(company.id);
|
|
1504
|
+
await harness.removePerson(workspace, doomed);
|
|
1505
|
+
});
|
|
1506
|
+
expect(await store.addMember(company.id, doomed, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1507
|
+
expect(await store.members(company.id)).toEqual([]);
|
|
1508
|
+
expect((await store.companiesForPeople([doomed])).size).toBe(0);
|
|
1509
|
+
expect(await store.personExists(doomed)).toBe(false);
|
|
1510
|
+
});
|
|
1511
|
+
run("the hook runs once, for the next add that gets past the existence checks: an add stopped by companyGone or personGone leaves it armed, and an add after it runs no hook", async () => {
|
|
1512
|
+
const { store, make, person } = await setup();
|
|
1513
|
+
const company = await make(store, "Acme");
|
|
1514
|
+
const [newcomer, second] = [await person(), await person()];
|
|
1515
|
+
const gone = await make(store, "Gone Co");
|
|
1516
|
+
await store.remove(gone.id);
|
|
1517
|
+
let ran = 0;
|
|
1518
|
+
arm(async () => {
|
|
1519
|
+
ran += 1;
|
|
1520
|
+
});
|
|
1521
|
+
expect(await store.addMember(gone.id, newcomer, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1522
|
+
expect(await store.addMember(company.id, "missing", null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "personGone" });
|
|
1523
|
+
expect(ran).toBe(0);
|
|
1524
|
+
expect((await store.addMember(company.id, newcomer, null, ACTOR, MEMBER_LIMITS)).kind).toBe("added");
|
|
1525
|
+
expect(ran).toBe(1);
|
|
1526
|
+
expect((await store.addMember(company.id, second, null, ACTOR, MEMBER_LIMITS)).kind).toBe("added");
|
|
1527
|
+
expect(ran).toBe(1);
|
|
1528
|
+
});
|
|
1529
|
+
run("a person added by someone else between addMember's check and its write: the add answers already with that membership (its role kept, also when the company is then full), and nothing more is written", async () => {
|
|
1530
|
+
const { store, make, person } = await setup();
|
|
1531
|
+
const company = await make(store, "Acme");
|
|
1532
|
+
const who = await person();
|
|
1533
|
+
arm(async () => {
|
|
1534
|
+
expect((await store.addMember(company.id, who, "First", ACTOR, MEMBER_LIMITS)).kind).toBe("added");
|
|
1535
|
+
});
|
|
1536
|
+
expect(await store.addMember(company.id, who, "Second", ACTOR, { maxMembers: 1 })).toEqual({ kind: "already", member: { companyId: company.id, personId: who, role: "First" } });
|
|
1537
|
+
expect(await store.members(company.id)).toEqual([{ companyId: company.id, personId: who, role: "First" }]);
|
|
1538
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 1]]);
|
|
1539
|
+
});
|
|
1540
|
+
run("the last place taken by someone else between addMember's check and its write: the add answers memberLimit and the company holds exactly the limit", async () => {
|
|
1541
|
+
const { store, make, person } = await setup();
|
|
1542
|
+
const company = await make(store, "Acme");
|
|
1543
|
+
const [other, who] = [await person(), await person()];
|
|
1544
|
+
const one = { maxMembers: 1 };
|
|
1545
|
+
arm(async () => {
|
|
1546
|
+
expect((await store.addMember(company.id, other, null, ACTOR, one)).kind).toBe("added");
|
|
1547
|
+
});
|
|
1548
|
+
expect(await store.addMember(company.id, who, null, ACTOR, one)).toEqual({ kind: "memberLimit" });
|
|
1549
|
+
expect(byPerson(await store.members(company.id))).toEqual([{ companyId: company.id, personId: other, role: null }]);
|
|
1550
|
+
expect([...await store.memberCounts([company.id])]).toEqual([[company.id, 1]]);
|
|
1551
|
+
expect((await store.companiesForPeople([who])).size).toBe(0);
|
|
1552
|
+
});
|
|
1553
|
+
});
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
// src/companies/server/contract-tenancy.ts
|
|
1557
|
+
function defineCompanyStoreTenancyContract(kit) {
|
|
1558
|
+
const { describe, it, expect } = kit.api;
|
|
1559
|
+
const { setup, harness } = kit;
|
|
1560
|
+
async function twoWorkspaces() {
|
|
1561
|
+
const a = await setup();
|
|
1562
|
+
const workspaceB = await harness.newWorkspace();
|
|
1563
|
+
const storeB = harness.storeFor(workspaceB);
|
|
1564
|
+
const personB = () => harness.addPerson(workspaceB);
|
|
1565
|
+
const joinB = async (companyId, personId, role = null) => {
|
|
1566
|
+
const result = await storeB.addMember(companyId, personId, role, ACTOR, MEMBER_LIMITS);
|
|
1567
|
+
if (result.kind !== "added") throw new Error(`setup: could not add a member in the second workspace: ${result.kind}`);
|
|
1568
|
+
return result.member;
|
|
1569
|
+
};
|
|
1570
|
+
const crowdB = async (count3) => {
|
|
1571
|
+
const pb = await personB();
|
|
1572
|
+
const companies = [];
|
|
1573
|
+
for (let i = 0; i < count3; i++) {
|
|
1574
|
+
const result = await storeB.create({ name: `B ${i}`, nameKey: `b ${i}`, ...site("b.com") }, { maxCompanies: 1e3 });
|
|
1575
|
+
if (result.kind !== "created") throw new Error(`setup: could not create B ${i}: ${result.kind}`);
|
|
1576
|
+
await joinB(result.company.id, pb, "B");
|
|
1577
|
+
companies.push(result.company);
|
|
1578
|
+
}
|
|
1579
|
+
return { companies, pb };
|
|
1580
|
+
};
|
|
1581
|
+
return { ...a, workspaceA: a.workspace, storeB, personB, joinB, crowdB };
|
|
1582
|
+
}
|
|
1583
|
+
const ghosts = () => Array.from({ length: 4e4 }, (_, i) => `ghost-${i}`);
|
|
1584
|
+
describe("CompanyStore contract: tenancy", () => {
|
|
1585
|
+
it("every read sees only its own workspace, also when the other workspace is large: list, get, getMany, findByKey and findByDomain", async () => {
|
|
1586
|
+
const t = await twoWorkspaces();
|
|
1587
|
+
const { store, storeB, make } = t;
|
|
1588
|
+
const a1 = await make(store, "A1", site("a.com"));
|
|
1589
|
+
const a2 = await make(store, "A2");
|
|
1590
|
+
const { companies: bs } = await t.crowdB(105);
|
|
1591
|
+
const b0 = bs[0];
|
|
1592
|
+
expect(cores(await store.list())).toEqual(cores([a1, a2]));
|
|
1593
|
+
expect((await storeB.list()).length).toBe(105);
|
|
1594
|
+
expect(await store.get(b0.id)).toBeNull();
|
|
1595
|
+
expect(await storeB.get(a1.id)).toBeNull();
|
|
1596
|
+
expect(ids(await store.getMany([a1.id, ...bs.map((company) => company.id)]))).toEqual([a1.id]);
|
|
1597
|
+
expect(await store.getMany(bs.map((company) => company.id))).toEqual([]);
|
|
1598
|
+
expect(await store.findByKey(b0.nameKey)).toBeNull();
|
|
1599
|
+
expect(await storeB.findByKey(a1.nameKey)).toBeNull();
|
|
1600
|
+
expect(await store.findByDomain("b.com")).toEqual([]);
|
|
1601
|
+
expect(ids(await storeB.findByDomain("b.com")).length).toBe(105);
|
|
1602
|
+
expect(ids(await store.findByDomain("a.com"))).toEqual([a1.id]);
|
|
1603
|
+
expect(await storeB.findByDomain("a.com")).toEqual([]);
|
|
1604
|
+
const crowd = ghosts();
|
|
1605
|
+
crowd.push(a1.id, ...bs.map((company) => company.id));
|
|
1606
|
+
expect(ids(await store.getMany(crowd))).toEqual([a1.id]);
|
|
1607
|
+
});
|
|
1608
|
+
it("the same name, key, website and domain can exist in two workspaces: a duplicate is per workspace, and a rename may take a key only the other workspace has", async () => {
|
|
1609
|
+
const { store, storeB, make } = await twoWorkspaces();
|
|
1610
|
+
const a = await make(store, "Acme", site("acme.com"));
|
|
1611
|
+
const b = await make(storeB, "Acme", site("acme.com"));
|
|
1612
|
+
expect(a.id === b.id).toBe(false);
|
|
1613
|
+
expect((await store.findByKey("acme"))?.id).toBe(a.id);
|
|
1614
|
+
expect((await storeB.findByKey("acme"))?.id).toBe(b.id);
|
|
1615
|
+
expect(ids(await store.findByDomain("acme.com"))).toEqual([a.id]);
|
|
1616
|
+
expect(ids(await storeB.findByDomain("acme.com"))).toEqual([b.id]);
|
|
1617
|
+
expect(await storeB.create({ name: "Acme", nameKey: "acme", ...NO_SITE }, LIMITS)).toEqual({ kind: "duplicate" });
|
|
1618
|
+
const zeta = await make(store, "Zeta");
|
|
1619
|
+
const bee = await make(storeB, "Bee");
|
|
1620
|
+
const onlyB = await make(storeB, "Only B");
|
|
1621
|
+
expect((await storeB.update(bee.id, { rename: { name: "Zeta", nameKey: "zeta" } })).kind).toBe("updated");
|
|
1622
|
+
expect((await store.update(zeta.id, { rename: { name: "Only B", nameKey: "only b" } })).kind).toBe("updated");
|
|
1623
|
+
expect(core(await storeB.get(onlyB.id))).toEqual(core(onlyB));
|
|
1624
|
+
expect(await store.findByKey("zeta")).toBeNull();
|
|
1625
|
+
expect((await storeB.findByKey("zeta"))?.id).toBe(bee.id);
|
|
1626
|
+
});
|
|
1627
|
+
it("limits count per workspace: a full workspace does not block the other, and the other's companies and members use none of its places", async () => {
|
|
1628
|
+
const t = await twoWorkspaces();
|
|
1629
|
+
const { store, storeB } = t;
|
|
1630
|
+
const two = { maxCompanies: 2 };
|
|
1631
|
+
const create = (s, n, limits = two) => s.create({ name: n, nameKey: n.toLowerCase(), ...NO_SITE }, limits);
|
|
1632
|
+
expect((await create(store, "a1")).kind).toBe("created");
|
|
1633
|
+
expect((await create(store, "a2")).kind).toBe("created");
|
|
1634
|
+
expect(await create(store, "a3")).toEqual({ kind: "limit" });
|
|
1635
|
+
expect((await create(storeB, "b1")).kind).toBe("created");
|
|
1636
|
+
expect((await create(storeB, "b2")).kind).toBe("created");
|
|
1637
|
+
expect(await create(storeB, "b3")).toEqual({ kind: "limit" });
|
|
1638
|
+
for (const n of ["b4", "b5"]) expect((await create(storeB, n, { maxCompanies: 100 })).kind).toBe("created");
|
|
1639
|
+
expect((await create(store, "c1", { maxCompanies: 2 })).kind).toBe("limit");
|
|
1640
|
+
const three = { maxCompanies: 3 };
|
|
1641
|
+
const t2 = await twoWorkspaces();
|
|
1642
|
+
for (const n of ["x1", "x2", "x3", "x4"]) expect((await create(t2.storeB, n, { maxCompanies: 100 })).kind).toBe("created");
|
|
1643
|
+
for (const n of ["y1", "y2", "y3"]) expect((await create(t2.store, n, three)).kind).toBe("created");
|
|
1644
|
+
expect(await create(t2.store, "y4", three)).toEqual({ kind: "limit" });
|
|
1645
|
+
const aCompany = await create(store, "a-members", { maxCompanies: 100 });
|
|
1646
|
+
const bCompany = await create(storeB, "b-members", { maxCompanies: 100 });
|
|
1647
|
+
if (aCompany.kind !== "created" || bCompany.kind !== "created") throw new Error("setup");
|
|
1648
|
+
for (let i = 0; i < 5; i++) await t.joinB(bCompany.company.id, await t.personB());
|
|
1649
|
+
const limit2 = { maxMembers: 2 };
|
|
1650
|
+
const [p1, p2, p3] = [await t.person(), await t.person(), await t.person()];
|
|
1651
|
+
expect((await store.addMember(aCompany.company.id, p1, null, ACTOR, limit2)).kind).toBe("added");
|
|
1652
|
+
expect((await store.addMember(aCompany.company.id, p2, null, ACTOR, limit2)).kind).toBe("added");
|
|
1653
|
+
expect(await store.addMember(aCompany.company.id, p3, null, ACTOR, limit2)).toEqual({ kind: "memberLimit" });
|
|
1654
|
+
expect((await storeB.addMember(bCompany.company.id, await t.personB(), null, ACTOR, { maxMembers: 6 })).kind).toBe("added");
|
|
1655
|
+
expect(await storeB.addMember(bCompany.company.id, await t.personB(), null, ACTOR, { maxMembers: 6 })).toEqual({ kind: "memberLimit" });
|
|
1656
|
+
});
|
|
1657
|
+
it(`update and remove of another workspace's company are "not found" and change nothing there (also when a company here has the same key)`, async () => {
|
|
1658
|
+
const t = await twoWorkspaces();
|
|
1659
|
+
const { store, storeB, make } = t;
|
|
1660
|
+
const a1 = await make(store, "Shared", site("a.com"));
|
|
1661
|
+
const b1 = await make(storeB, "Shared", site("b.com"));
|
|
1662
|
+
const pb = await t.personB();
|
|
1663
|
+
await t.joinB(b1.id, pb, "Chair");
|
|
1664
|
+
expect(await store.update(b1.id, { rename: { name: "Hijacked", nameKey: "hijacked" } })).toEqual({ kind: "notFound" });
|
|
1665
|
+
expect(await store.update(b1.id, { website: site("hijacked.com") })).toEqual({ kind: "notFound" });
|
|
1666
|
+
expect(await store.update(b1.id, {})).toEqual({ kind: "notFound" });
|
|
1667
|
+
expect(await store.remove(b1.id)).toEqual({ kind: "notFound" });
|
|
1668
|
+
expect(core(await storeB.get(b1.id))).toEqual(core(b1));
|
|
1669
|
+
expect(await storeB.members(b1.id)).toEqual([{ companyId: b1.id, personId: pb, role: "Chair" }]);
|
|
1670
|
+
expect([...await storeB.memberCounts([b1.id])]).toEqual([[b1.id, 1]]);
|
|
1671
|
+
expect(core(await store.get(a1.id))).toEqual(core(a1));
|
|
1672
|
+
});
|
|
1673
|
+
it("another workspace's person is no person here, and an add that mixes the two workspaces is refused and writes nothing on either side", async () => {
|
|
1674
|
+
const t = await twoWorkspaces();
|
|
1675
|
+
const { store, storeB, make } = t;
|
|
1676
|
+
const a = await make(store, "A");
|
|
1677
|
+
const b = await make(storeB, "B");
|
|
1678
|
+
const [pa, pb] = [await t.person(), await t.personB()];
|
|
1679
|
+
expect(await store.personExists(pa)).toBe(true);
|
|
1680
|
+
expect(await store.personExists(pb)).toBe(false);
|
|
1681
|
+
expect(await storeB.personExists(pb)).toBe(true);
|
|
1682
|
+
expect(await storeB.personExists(pa)).toBe(false);
|
|
1683
|
+
expect(await store.addMember(a.id, pb, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "personGone" });
|
|
1684
|
+
expect(await store.addMember(b.id, pa, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1685
|
+
expect(await store.addMember(b.id, pb, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1686
|
+
expect(await storeB.addMember(b.id, pa, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "personGone" });
|
|
1687
|
+
expect(await storeB.addMember(a.id, pa, null, ACTOR, MEMBER_LIMITS)).toEqual({ kind: "companyGone" });
|
|
1688
|
+
expect(await store.members(a.id)).toEqual([]);
|
|
1689
|
+
expect(await storeB.members(b.id)).toEqual([]);
|
|
1690
|
+
expect((await store.companiesForPeople([pa, pb])).size).toBe(0);
|
|
1691
|
+
expect((await storeB.companiesForPeople([pa, pb])).size).toBe(0);
|
|
1692
|
+
expect([...await store.memberCounts()]).toEqual([[a.id, 0]]);
|
|
1693
|
+
expect([...await storeB.memberCounts()]).toEqual([[b.id, 0]]);
|
|
1694
|
+
});
|
|
1695
|
+
it("setRole, removeMember and members for another workspace's company answer companyGone, companyGone and nothing, and for another workspace's person notMember; the memberships there stay as they were", async () => {
|
|
1696
|
+
const t = await twoWorkspaces();
|
|
1697
|
+
const { store, storeB, make } = t;
|
|
1698
|
+
const a = await make(store, "A");
|
|
1699
|
+
const b = await make(storeB, "B");
|
|
1700
|
+
const [pa, pb] = [await t.person(), await t.personB()];
|
|
1701
|
+
await t.join(a.id, pa, "Chair");
|
|
1702
|
+
await t.joinB(b.id, pb, "Bm");
|
|
1703
|
+
expect(await store.setRole(b.id, pb, "x")).toEqual({ kind: "companyGone" });
|
|
1704
|
+
expect(await store.setRole(b.id, pa, "x")).toEqual({ kind: "companyGone" });
|
|
1705
|
+
expect(await store.removeMember(b.id, pb)).toEqual({ kind: "companyGone" });
|
|
1706
|
+
expect(await store.members(b.id)).toEqual([]);
|
|
1707
|
+
expect(await store.setRole(a.id, pb, "x")).toEqual({ kind: "notMember" });
|
|
1708
|
+
expect(await store.removeMember(a.id, pb)).toEqual({ kind: "notMember" });
|
|
1709
|
+
expect(await storeB.setRole(a.id, pa, "x")).toEqual({ kind: "companyGone" });
|
|
1710
|
+
expect(await store.members(a.id)).toEqual([{ companyId: a.id, personId: pa, role: "Chair" }]);
|
|
1711
|
+
expect(await storeB.members(b.id)).toEqual([{ companyId: b.id, personId: pb, role: "Bm" }]);
|
|
1712
|
+
});
|
|
1713
|
+
it("companiesForPeople and memberCounts never show another workspace's companies, people or ids, with the other workspace large and in a 40,000-id ask", async () => {
|
|
1714
|
+
const t = await twoWorkspaces();
|
|
1715
|
+
const { store, storeB, make } = t;
|
|
1716
|
+
const a1 = await make(store, "A1");
|
|
1717
|
+
const pa = await t.person();
|
|
1718
|
+
await t.join(a1.id, pa, "A");
|
|
1719
|
+
const { companies: bs, pb } = await t.crowdB(105);
|
|
1720
|
+
const mine = await store.companiesForPeople([pa, pb]);
|
|
1721
|
+
expect([...mine.keys()]).toEqual([pa]);
|
|
1722
|
+
expect(mine.get(pa)?.map((chip) => [chip.company.id, chip.role])).toEqual([[a1.id, "A"]]);
|
|
1723
|
+
const theirs = await storeB.companiesForPeople([pa, pb]);
|
|
1724
|
+
expect([...theirs.keys()]).toEqual([pb]);
|
|
1725
|
+
expect(theirs.get(pb)?.length).toBe(105);
|
|
1726
|
+
expect([...await store.memberCounts()]).toEqual([[a1.id, 1]]);
|
|
1727
|
+
expect((await storeB.memberCounts()).size).toBe(105);
|
|
1728
|
+
const bIds = bs.map((company) => company.id);
|
|
1729
|
+
expect([...await store.memberCounts([a1.id, ...bIds])]).toEqual([[a1.id, 1]]);
|
|
1730
|
+
expect((await store.memberCounts(bIds)).size).toBe(0);
|
|
1731
|
+
const crowd = ghosts();
|
|
1732
|
+
crowd.push(...bIds, a1.id);
|
|
1733
|
+
expect([...await store.memberCounts(crowd)]).toEqual([[a1.id, 1]]);
|
|
1734
|
+
const people = ghosts();
|
|
1735
|
+
people.push(pb, pa);
|
|
1736
|
+
expect([...(await store.companiesForPeople(people)).keys()]).toEqual([pa]);
|
|
1737
|
+
});
|
|
1738
|
+
it("removing a company or a person, and renaming a company, touches only its own workspace, also when the other workspace has a company with the same key", async () => {
|
|
1739
|
+
const t = await twoWorkspaces();
|
|
1740
|
+
const { store, storeB, make, workspaceA } = t;
|
|
1741
|
+
const a1 = await make(store, "Shared");
|
|
1742
|
+
const b1 = await make(storeB, "Shared");
|
|
1743
|
+
const [pa, pb] = [await t.person(), await t.personB()];
|
|
1744
|
+
await t.join(a1.id, pa, "A");
|
|
1745
|
+
await t.joinB(b1.id, pb, "B");
|
|
1746
|
+
const a2 = await make(store, "A Two");
|
|
1747
|
+
const b2 = await make(storeB, "A Two");
|
|
1748
|
+
await t.join(a2.id, pa);
|
|
1749
|
+
await t.joinB(b2.id, pb);
|
|
1750
|
+
expect((await store.update(a2.id, { rename: { name: "Renamed", nameKey: "renamed" } })).kind).toBe("updated");
|
|
1751
|
+
expect(core(await storeB.get(b2.id))).toEqual(core(b2));
|
|
1752
|
+
expect((await storeB.findByKey("a two"))?.id).toBe(b2.id);
|
|
1753
|
+
const removed = await store.remove(a1.id);
|
|
1754
|
+
expect(removed.kind === "removed" && removed.memberCount).toBe(1);
|
|
1755
|
+
expect(core(await storeB.get(b1.id))).toEqual(core(b1));
|
|
1756
|
+
expect((await storeB.findByKey("shared"))?.id).toBe(b1.id);
|
|
1757
|
+
expect(await storeB.members(b1.id)).toEqual([{ companyId: b1.id, personId: pb, role: "B" }]);
|
|
1758
|
+
await harness.removePerson(workspaceA, pa);
|
|
1759
|
+
expect(await store.members(a2.id)).toEqual([]);
|
|
1760
|
+
expect(byPerson(await storeB.members(b2.id)).map((member) => member.personId)).toEqual([pb]);
|
|
1761
|
+
expect(await storeB.personExists(pb)).toBe(true);
|
|
1762
|
+
expect([...await storeB.memberCounts()].sort()).toEqual([[b1.id, 1], [b2.id, 1]].sort());
|
|
1763
|
+
});
|
|
1764
|
+
it("a long run of writes in one workspace (create, rename, website, remove, add, setRole, removeMember, removePerson, refusals), using the same names and domains, leaves the other workspace exactly as it was", async () => {
|
|
1765
|
+
const t = await twoWorkspaces();
|
|
1766
|
+
const { store, storeB, make } = t;
|
|
1767
|
+
const b1 = await make(storeB, "Acme", site("acme.com"));
|
|
1768
|
+
const b2 = await make(storeB, "Bolt", site("bolt.com"));
|
|
1769
|
+
const [pb1, pb2] = [await t.personB(), await t.personB()];
|
|
1770
|
+
await t.joinB(b1.id, pb1, "Chair");
|
|
1771
|
+
await t.joinB(b2.id, pb1);
|
|
1772
|
+
await t.joinB(b2.id, pb2, "Clerk");
|
|
1773
|
+
const snapshot = async () => {
|
|
1774
|
+
const counts = [...await storeB.memberCounts()].sort();
|
|
1775
|
+
const chips = [...await storeB.companiesForPeople([pb1, pb2])].map(([id, list]) => [id, list.map((chip) => [core(chip.company), chip.role])]).sort();
|
|
1776
|
+
const members = [byPerson(await storeB.members(b1.id)), byPerson(await storeB.members(b2.id))];
|
|
1777
|
+
return { list: cores(await storeB.list()), counts, chips, members, acme: cores(await storeB.findByDomain("acme.com")), key: core(await storeB.findByKey("bolt")) };
|
|
1778
|
+
};
|
|
1779
|
+
const start = await snapshot();
|
|
1780
|
+
const [pa1, pa2] = [await t.person(), await t.person()];
|
|
1781
|
+
const acme = await make(store, "Acme", site("acme.com"));
|
|
1782
|
+
const bolt = await make(store, "Bolt", site("bolt.com"));
|
|
1783
|
+
expect((await store.create({ name: "Acme", nameKey: "acme", ...NO_SITE }, LIMITS)).kind).toBe("duplicate");
|
|
1784
|
+
expect((await store.create({ name: "Full", nameKey: "full", ...NO_SITE }, { maxCompanies: 2 })).kind).toBe("limit");
|
|
1785
|
+
await t.join(acme.id, pa1, "Chair");
|
|
1786
|
+
await t.join(bolt.id, pa1);
|
|
1787
|
+
await t.join(bolt.id, pa2, "Clerk");
|
|
1788
|
+
expect((await store.addMember(bolt.id, pa1, "Again", ACTOR, MEMBER_LIMITS)).kind).toBe("already");
|
|
1789
|
+
expect((await store.addMember(acme.id, pa2, null, ACTOR, { maxMembers: 1 })).kind).toBe("memberLimit");
|
|
1790
|
+
await store.setRole(acme.id, pa1, "Founder");
|
|
1791
|
+
await store.update(acme.id, { rename: { name: "Acme Two", nameKey: "acme two" }, website: site("acme2.com") });
|
|
1792
|
+
await store.update(bolt.id, { website: NO_SITE });
|
|
1793
|
+
await store.removeMember(bolt.id, pa2);
|
|
1794
|
+
await store.remove(acme.id);
|
|
1795
|
+
await harness.removePerson(t.workspaceA, pa1);
|
|
1796
|
+
await store.remove(bolt.id);
|
|
1797
|
+
expect(await store.update("missing", { rename: { name: "x", nameKey: "x" } })).toEqual({ kind: "notFound" });
|
|
1798
|
+
expect(await snapshot()).toEqual(start);
|
|
1799
|
+
});
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
// src/companies/server/contract.ts
|
|
1804
|
+
function defineCompanyStoreContract(api, harness) {
|
|
1805
|
+
api.describe("CompanyStore contract", () => {
|
|
1806
|
+
const kit = makeKit(api, harness);
|
|
1807
|
+
defineCompanyStoreCompaniesContract(kit);
|
|
1808
|
+
defineCompanyStoreCompaniesMoreContract(kit);
|
|
1809
|
+
defineCompanyStoreMembersContract(kit);
|
|
1810
|
+
defineCompanyStoreRolesContract(kit);
|
|
1811
|
+
defineCompanyStoreReadsContract(kit);
|
|
1812
|
+
defineCompanyStoreTenancyContract(kit);
|
|
1813
|
+
defineCompanyStoreConcurrencyContract(kit);
|
|
1814
|
+
defineCompanyStoreMembersConcurrencyContract(kit);
|
|
1815
|
+
defineCompanyStoreHooksContract(kit);
|
|
1816
|
+
api.it("cleans up", async () => {
|
|
1817
|
+
await harness.cleanup();
|
|
1818
|
+
});
|
|
1819
|
+
});
|
|
1820
|
+
}
|
|
1821
|
+
export {
|
|
1822
|
+
createCompanyService,
|
|
1823
|
+
createMemoryCompanyHarness,
|
|
1824
|
+
createMemoryCompanyWorld,
|
|
1825
|
+
defineCompanyStoreContract
|
|
1826
|
+
};
|