@sprucelabs/spruce-test-fixtures 66.0.19 → 66.0.21
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/build/FakerTracker.d.ts +41 -0
- package/build/FakerTracker.js +56 -0
- package/build/esm/FakerTracker.d.ts +41 -0
- package/build/esm/FakerTracker.js +54 -0
- package/build/esm/tests/AbstractSpruceFixtureTest.d.ts +68 -27
- package/build/esm/tests/AbstractSpruceFixtureTest.js +256 -103
- package/build/esm/tests/decorators/fake.d.ts +11 -11
- package/build/esm/tests/decorators/fake.js +151 -173
- package/build/esm/tests/decorators/install.js +7 -4
- package/build/esm/tests/decorators/login.js +18 -18
- package/build/esm/tests/decorators/seed.js +34 -40
- package/build/esm/tests/fixtures/FixtureFactory.d.ts +2 -1
- package/build/esm/tests/fixtures/FixtureFactory.js +38 -35
- package/build/esm/tests/fixtures/FixtureWarehourse.d.ts +56 -0
- package/build/esm/tests/fixtures/FixtureWarehourse.js +165 -0
- package/build/esm/tests/fixtures/LocationFixture.d.ts +5 -5
- package/build/esm/tests/fixtures/LocationFixture.js +5 -5
- package/build/esm/tests/fixtures/MercuryFixture.d.ts +5 -0
- package/build/esm/tests/fixtures/MercuryFixture.js +50 -25
- package/build/esm/tests/fixtures/OrganizationFixture.d.ts +4 -4
- package/build/esm/tests/fixtures/PersonFixture.d.ts +2 -2
- package/build/esm/tests/fixtures/PersonFixture.js +23 -9
- package/build/esm/tests/fixtures/RoleFixture.d.ts +4 -4
- package/build/esm/tests/fixtures/RoleFixture.js +4 -4
- package/build/esm/tests/fixtures/SeedFixture.d.ts +13 -13
- package/build/esm/tests/fixtures/SkillFixture.d.ts +2 -2
- package/build/esm/tests/fixtures/SkillFixture.js +2 -2
- package/build/esm/tests/fixtures/SpyScope.d.ts +2 -2
- package/build/esm/tests/fixtures/ViewFixture.d.ts +7 -3
- package/build/esm/tests/fixtures/ViewFixture.js +11 -9
- package/build/tests/AbstractSpruceFixtureTest.d.ts +68 -27
- package/build/tests/AbstractSpruceFixtureTest.js +243 -98
- package/build/tests/decorators/fake.d.ts +11 -11
- package/build/tests/decorators/fake.js +151 -171
- package/build/tests/decorators/install.js +9 -3
- package/build/tests/decorators/login.js +20 -17
- package/build/tests/decorators/seed.js +34 -38
- package/build/tests/fixtures/FixtureFactory.d.ts +2 -1
- package/build/tests/fixtures/FixtureFactory.js +37 -35
- package/build/tests/fixtures/FixtureWarehourse.d.ts +56 -0
- package/build/tests/fixtures/FixtureWarehourse.js +170 -0
- package/build/tests/fixtures/LocationFixture.d.ts +5 -5
- package/build/tests/fixtures/LocationFixture.js +5 -5
- package/build/tests/fixtures/MercuryFixture.d.ts +5 -0
- package/build/tests/fixtures/MercuryFixture.js +48 -23
- package/build/tests/fixtures/OrganizationFixture.d.ts +4 -4
- package/build/tests/fixtures/PersonFixture.d.ts +2 -2
- package/build/tests/fixtures/PersonFixture.js +20 -6
- package/build/tests/fixtures/RoleFixture.d.ts +4 -4
- package/build/tests/fixtures/RoleFixture.js +4 -4
- package/build/tests/fixtures/SeedFixture.d.ts +13 -13
- package/build/tests/fixtures/SkillFixture.d.ts +2 -2
- package/build/tests/fixtures/SkillFixture.js +2 -2
- package/build/tests/fixtures/SpyScope.d.ts +2 -2
- package/build/tests/fixtures/ViewFixture.d.ts +7 -3
- package/build/tests/fixtures/ViewFixture.js +13 -9
- package/package.json +25 -25
@@ -14,6 +14,7 @@ const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
|
|
14
14
|
const test_utils_1 = require("@sprucelabs/test-utils");
|
15
15
|
const test_utils_2 = require("@sprucelabs/test-utils");
|
16
16
|
const SpruceError_1 = __importDefault(require("../../errors/SpruceError"));
|
17
|
+
const FakerTracker_1 = __importDefault(require("../../FakerTracker"));
|
17
18
|
const AbstractSpruceFixtureTest_1 = __importDefault(require("../AbstractSpruceFixtureTest"));
|
18
19
|
const eventFaker_1 = __importDefault(require("../eventFaker"));
|
19
20
|
const generateRandomName_1 = __importDefault(require("../fixtures/generateRandomName"));
|
@@ -29,27 +30,12 @@ const strategies = {
|
|
29
30
|
groupManagers: buildSeeder('groupManagers'),
|
30
31
|
owners: buildSeeder('owners'),
|
31
32
|
};
|
32
|
-
function resetFakedRecords(
|
33
|
+
function resetFakedRecords() {
|
33
34
|
if (shouldSkipNextReset) {
|
34
35
|
shouldSkipNextReset = false;
|
35
36
|
return;
|
36
37
|
}
|
37
|
-
|
38
|
-
Class._fakedLocations = [];
|
39
|
-
Class.fakedTeammates = [];
|
40
|
-
Class.fakedInstalledSkills = [];
|
41
|
-
Class.fakedPeopleRoles = [];
|
42
|
-
Class.fakedManagers = [];
|
43
|
-
Class.fakedOwners = [];
|
44
|
-
Class.fakedGroupManagers = [];
|
45
|
-
Class.fakedGuests = [];
|
46
|
-
Class.fakedRoles = [];
|
47
|
-
Class.fakedPeople = Class.fakedPeople?.[0] ? [Class.fakedPeople[0]] : [];
|
48
|
-
Class.fakedSkills = [];
|
49
|
-
Class.fakedTokens = Class.fakedTokens?.[0] ? [Class.fakedTokens[0]] : [];
|
50
|
-
Class.fakedProxyTokens = Class.fakedProxyTokens?.[0]
|
51
|
-
? [Class.fakedProxyTokens[0]]
|
52
|
-
: [];
|
38
|
+
FakerTracker_1.default.resetFakedData();
|
53
39
|
}
|
54
40
|
function fake(target, total) {
|
55
41
|
(0, schema_1.assertOptions)({ target, total }, ['target', 'total']);
|
@@ -57,10 +43,10 @@ function fake(target, total) {
|
|
57
43
|
const Class = TestClass;
|
58
44
|
const bound = descriptor?.value?.bind?.(Class);
|
59
45
|
descriptor.value = async (...args) => {
|
60
|
-
test_utils_1.assert.isTruthy(
|
46
|
+
test_utils_1.assert.isTruthy(FakerTracker_1.default.fakedPerson, `You gotta @faker.login(...) before you can create fake '${target}'!`);
|
61
47
|
const strategy = strategies[target];
|
62
48
|
test_utils_1.assert.isTruthy(strategy, `Faking ${target} is not ready yet!`);
|
63
|
-
await strategy?.(
|
49
|
+
await strategy?.(total);
|
64
50
|
await bound?.(...args);
|
65
51
|
};
|
66
52
|
};
|
@@ -78,49 +64,42 @@ fake.login = (phone = '555-000-0000') => {
|
|
78
64
|
spruce_skill_utils_1.testLog.warn(`@fake.login() is attached to an incompatible test class (${TestClass.name}). You can safely remove it or have your test class extend AbstractSpruceFixtureTest.`);
|
79
65
|
return;
|
80
66
|
}
|
81
|
-
const Class = TestClass;
|
82
|
-
const beforeEach = Class.beforeEach?.bind(Class);
|
83
|
-
const beforeAll = Class.beforeAll?.bind(Class);
|
84
|
-
const afterEach = Class.afterEach?.bind(Class);
|
85
67
|
if (shouldPassHookCalls) {
|
86
68
|
const old = MercuryFixture_1.default.beforeEach.bind(MercuryFixture_1.default);
|
87
69
|
MercuryFixture_1.default.beforeEach = async (...args) => {
|
88
70
|
//@ts-ignore
|
89
71
|
await old(...args);
|
90
|
-
await setupFakeListeners(
|
72
|
+
await setupFakeListeners();
|
91
73
|
};
|
92
74
|
}
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
await
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
resetFakedRecords(Class);
|
106
|
-
await setupFakeListeners(Class);
|
75
|
+
test_utils_1.SpruceTestResolver.onDidCallBeforeAll(async () => {
|
76
|
+
resetFakedRecords();
|
77
|
+
await setupFakeListeners();
|
78
|
+
await login(phone);
|
79
|
+
MercuryFixture_1.default.setDefaultClient(FakerTracker_1.default.fakedClient);
|
80
|
+
});
|
81
|
+
test_utils_1.SpruceTestResolver.onDidCallAfterEach(async () => {
|
82
|
+
await setupFakeListeners();
|
83
|
+
});
|
84
|
+
test_utils_1.SpruceTestResolver.onWillCallBeforeEach(async (TestClass) => {
|
85
|
+
resetFakedRecords();
|
86
|
+
await setupFakeListeners();
|
107
87
|
if (!TestClass.cwd) {
|
108
88
|
return;
|
109
89
|
}
|
110
90
|
ViewFixture_1.default.resetAuth();
|
111
91
|
try {
|
112
|
-
if (
|
113
|
-
const auth =
|
92
|
+
if (FakerTracker_1.default.fakedPerson) {
|
93
|
+
const auth = getFixturesForActiveTest().permissions.getAuthenticator();
|
114
94
|
auth.setSessionToken(
|
115
95
|
//@ts-ignore
|
116
|
-
|
96
|
+
FakerTracker_1.default.fakedClient.auth.token, FakerTracker_1.default.fakedPerson);
|
117
97
|
}
|
118
98
|
}
|
119
99
|
catch {
|
120
100
|
//hits if not in skill because cant find nameplace
|
121
101
|
}
|
122
|
-
|
123
|
-
};
|
102
|
+
});
|
124
103
|
};
|
125
104
|
};
|
126
105
|
fake.getClient = () => {
|
@@ -130,32 +109,32 @@ fake.getPerson = () => {
|
|
130
109
|
//@ts-ignore
|
131
110
|
return fake.getClient().auth.person;
|
132
111
|
};
|
133
|
-
async function login(
|
134
|
-
const { person, client } = await loginUsingViewsFallingBackToPeople(
|
112
|
+
async function login(phone) {
|
113
|
+
const { person, client } = await loginUsingViewsFallingBackToPeople(phone);
|
135
114
|
if (!person.firstName) {
|
136
115
|
givePersonName(person);
|
137
116
|
}
|
138
117
|
await client.registerProxyToken();
|
139
118
|
//@ts-ignore
|
140
119
|
client.auth.person = person;
|
141
|
-
|
142
|
-
|
120
|
+
FakerTracker_1.default.fakedClient = client;
|
121
|
+
FakerTracker_1.default.fakedPerson = person;
|
143
122
|
}
|
144
|
-
async function loginUsingViewsFallingBackToPeople(
|
123
|
+
async function loginUsingViewsFallingBackToPeople(phone) {
|
145
124
|
let person;
|
146
125
|
let client;
|
147
126
|
try {
|
148
|
-
const { person: p, client: c } = await
|
127
|
+
const { person: p, client: c } = await getFixturesForActiveTest().views.loginAsDemoPerson(phone);
|
149
128
|
person = p;
|
150
129
|
client = c;
|
151
130
|
}
|
152
131
|
catch {
|
153
|
-
const { person: p, client: c } = await
|
132
|
+
const { person: p, client: c } = await getFixturesForActiveTest().people.loginAsDemoPerson(phone);
|
154
133
|
person = p;
|
155
134
|
client = c;
|
156
135
|
}
|
157
136
|
return {
|
158
|
-
person:
|
137
|
+
person: FakerTracker_1.default.fakedPeople.find((p) => p.id === person.id),
|
159
138
|
client,
|
160
139
|
};
|
161
140
|
}
|
@@ -165,39 +144,38 @@ function givePersonName(person) {
|
|
165
144
|
person.firstName = names.firstName;
|
166
145
|
person.lastName = names.lastName;
|
167
146
|
}
|
168
|
-
async function setupFakeListeners(
|
147
|
+
async function setupFakeListeners() {
|
169
148
|
await Promise.all([
|
170
|
-
fakeSkillLifecycleEvents(
|
171
|
-
fakeGetPerson(
|
172
|
-
fakeRegisterProxyToken(
|
173
|
-
fakeWhoAmI(
|
174
|
-
fakeInstallEvents(
|
175
|
-
fakeAuthenticationEvents(
|
176
|
-
fakeAddRole(
|
177
|
-
fakeGetRole(
|
178
|
-
fakeRemoveRole(
|
179
|
-
fakeListRoles(
|
180
|
-
fakeListPeople(
|
181
|
-
fakeUpdatePerson(
|
182
|
-
fakeListLocations(
|
183
|
-
fakeGetLocation(
|
184
|
-
fakeUpdateLocation(
|
185
|
-
fakeDeleteOrganization(
|
186
|
-
fakeCreateLocation(
|
187
|
-
fakeCreateOrganization(
|
188
|
-
fakeGetOrganization(
|
189
|
-
fakeUpdateOrganization(
|
190
|
-
fakeListOrganization(
|
149
|
+
fakeSkillLifecycleEvents(),
|
150
|
+
fakeGetPerson(),
|
151
|
+
fakeRegisterProxyToken(),
|
152
|
+
fakeWhoAmI(),
|
153
|
+
fakeInstallEvents(),
|
154
|
+
fakeAuthenticationEvents(),
|
155
|
+
fakeAddRole(),
|
156
|
+
fakeGetRole(),
|
157
|
+
fakeRemoveRole(),
|
158
|
+
fakeListRoles(),
|
159
|
+
fakeListPeople(),
|
160
|
+
fakeUpdatePerson(),
|
161
|
+
fakeListLocations(),
|
162
|
+
fakeGetLocation(),
|
163
|
+
fakeUpdateLocation(),
|
164
|
+
fakeDeleteOrganization(),
|
165
|
+
fakeCreateLocation(),
|
166
|
+
fakeCreateOrganization(),
|
167
|
+
fakeGetOrganization(),
|
168
|
+
fakeUpdateOrganization(),
|
169
|
+
fakeListOrganization(),
|
191
170
|
fakeRegisterListeners(),
|
192
171
|
]);
|
193
172
|
}
|
194
|
-
async function fakeAddRole(
|
173
|
+
async function fakeAddRole() {
|
195
174
|
await eventFaker_1.default.on('add-role::v2020_12_25', ({ payload, target }) => {
|
196
175
|
const { organizationId, locationId } = target ?? {};
|
197
176
|
const { roleId } = payload;
|
198
|
-
const person = getPersonById(
|
177
|
+
const person = getPersonById(payload.personId);
|
199
178
|
addPersonAsRoleToLocationOrOrg({
|
200
|
-
Class,
|
201
179
|
roleId,
|
202
180
|
person,
|
203
181
|
organizationId,
|
@@ -207,32 +185,32 @@ async function fakeAddRole(Class) {
|
|
207
185
|
});
|
208
186
|
}
|
209
187
|
function addPersonAsRoleToLocationOrOrg(options) {
|
210
|
-
const {
|
211
|
-
const role = getRoleById(
|
188
|
+
const { roleId, person, organizationId, locationId } = options;
|
189
|
+
const role = getRoleById(roleId);
|
212
190
|
const key = roleBaseToLocalFakedProp(role.base);
|
213
191
|
//@ts-ignore
|
214
|
-
test_utils_1.assert.isTruthy(
|
215
|
-
const idx =
|
192
|
+
test_utils_1.assert.isTruthy(FakerTracker_1.default[key], `Could not find property ${key}`);
|
193
|
+
const idx = FakerTracker_1.default[key].findIndex((p) => p.id === person.id);
|
216
194
|
if (idx === -1) {
|
217
195
|
//@ts-ignore
|
218
|
-
|
196
|
+
FakerTracker_1.default[key].unshift(person);
|
219
197
|
}
|
220
|
-
|
198
|
+
FakerTracker_1.default.fakedPeopleRoles.push({
|
221
199
|
personId: person.id,
|
222
200
|
roleId: role.id,
|
223
201
|
organizationId,
|
224
202
|
locationId,
|
225
203
|
});
|
226
204
|
}
|
227
|
-
function getRoleById(
|
228
|
-
const role =
|
205
|
+
function getRoleById(roleId) {
|
206
|
+
const role = FakerTracker_1.default.fakedRoles.find((r) => r.id === roleId);
|
229
207
|
test_utils_1.assert.isTruthy(role, `Could not load faked role with the id of ${roleId}.`);
|
230
208
|
return role;
|
231
209
|
}
|
232
|
-
async function fakeGetRole(
|
210
|
+
async function fakeGetRole() {
|
233
211
|
await eventFaker_1.default.on('get-role::v2020_12_25', ({ target }) => {
|
234
212
|
const { roleId } = target;
|
235
|
-
const role =
|
213
|
+
const role = FakerTracker_1.default.fakedRoles.find((r) => r.id === roleId);
|
236
214
|
if (!role) {
|
237
215
|
throw new SpruceError_1.default({
|
238
216
|
code: 'NOT_FOUND',
|
@@ -244,22 +222,22 @@ async function fakeGetRole(Class) {
|
|
244
222
|
};
|
245
223
|
});
|
246
224
|
}
|
247
|
-
async function fakeRemoveRole(
|
225
|
+
async function fakeRemoveRole() {
|
248
226
|
await eventFaker_1.default.on('remove-role::v2020_12_25', ({ payload }) => {
|
249
227
|
const { personId, roleId } = payload;
|
250
|
-
const role = getRoleById(
|
251
|
-
const people =
|
228
|
+
const role = getRoleById(roleId);
|
229
|
+
const people = FakerTracker_1.default[roleBaseToLocalFakedProp(role.base)];
|
252
230
|
const idx = people?.findIndex((p) => p.id === personId) ?? -1;
|
253
231
|
people?.splice(idx, 1);
|
254
|
-
|
232
|
+
FakerTracker_1.default.fakedPeopleRoles = FakerTracker_1.default.fakedPeopleRoles.filter((p) => !(p.personId === personId && p.roleId === roleId));
|
255
233
|
return {};
|
256
234
|
});
|
257
235
|
}
|
258
236
|
function roleBaseToLocalFakedProp(base) {
|
259
237
|
return fakeTargetToPropName((base + 's'));
|
260
238
|
}
|
261
|
-
function getPersonById(
|
262
|
-
const person =
|
239
|
+
function getPersonById(personId, shouldThrowWhenNotFound = true) {
|
240
|
+
const person = FakerTracker_1.default.fakedPeople.find((p) => p.id === personId);
|
263
241
|
if (!person && shouldThrowWhenNotFound) {
|
264
242
|
throw new SpruceError_1.default({
|
265
243
|
code: 'INVALID_TARGET',
|
@@ -268,9 +246,9 @@ function getPersonById(Class, personId, shouldThrowWhenNotFound = true) {
|
|
268
246
|
}
|
269
247
|
return person;
|
270
248
|
}
|
271
|
-
async function fakeUpdatePerson(
|
249
|
+
async function fakeUpdatePerson() {
|
272
250
|
await eventFaker_1.default.on('update-person::v2020_12_25', ({ target, source, payload }) => {
|
273
|
-
const person =
|
251
|
+
const person = FakerTracker_1.default.fakedPeople.find((p) => target?.personId
|
274
252
|
? p.id === target?.personId
|
275
253
|
: p.id === source?.personId);
|
276
254
|
if (!person) {
|
@@ -287,26 +265,26 @@ async function fakeUpdatePerson(Class) {
|
|
287
265
|
};
|
288
266
|
});
|
289
267
|
}
|
290
|
-
async function fakeListRoles(
|
268
|
+
async function fakeListRoles() {
|
291
269
|
await eventFaker_1.default.on('list-roles::v2020_12_25', ({ target, payload }) => {
|
292
270
|
let { personId, organizationId, locationId } = target ?? {};
|
293
271
|
const { shouldIncludeMetaRoles, shouldIncludePrivateRoles } = payload ?? {};
|
294
272
|
let roles = [];
|
295
273
|
if (personId) {
|
296
|
-
const personRoles =
|
274
|
+
const personRoles = FakerTracker_1.default.fakedPeopleRoles
|
297
275
|
.filter((p) => p.personId === personId &&
|
298
276
|
((p.organizationId &&
|
299
277
|
p.organizationId === organizationId) ||
|
300
278
|
(p.locationId && p.locationId === locationId) ||
|
301
279
|
(!locationId && !organizationId)))
|
302
|
-
.map((pr) => getRoleById(
|
280
|
+
.map((pr) => getRoleById(pr.roleId));
|
303
281
|
roles = personRoles;
|
304
282
|
}
|
305
283
|
else {
|
306
284
|
if (locationId) {
|
307
|
-
organizationId =
|
285
|
+
organizationId = FakerTracker_1.default.fakedLocations.find((l) => l.id === locationId)?.organizationId;
|
308
286
|
}
|
309
|
-
roles =
|
287
|
+
roles = FakerTracker_1.default.fakedRoles.filter((r) => r.organizationId === organizationId);
|
310
288
|
}
|
311
289
|
if (!shouldIncludeMetaRoles) {
|
312
290
|
roles = roles.filter((r) => spruce_core_schemas_1.META_BASE_ROLES.findIndex((m) => m.slug === r.base) === -1);
|
@@ -319,37 +297,37 @@ async function fakeListRoles(Class) {
|
|
319
297
|
};
|
320
298
|
});
|
321
299
|
}
|
322
|
-
async function fakeListPeople(
|
300
|
+
async function fakeListPeople() {
|
323
301
|
await eventFaker_1.default.on('list-people::v2020_12_25', ({ payload }) => {
|
324
302
|
let people = [];
|
325
303
|
test_utils_1.assert.isFalsy(payload?.roleIds, `@fake does not support listing people by roleIds. You will need to use 'eventFaker.on('list-people')' to fake your own response OR consider checking this.fakedPeople on your test class.`);
|
326
304
|
test_utils_1.assert.isFalsy(payload?.personIds, `@fake does not support listing people by personIds. You will need to use 'eventFaker.on(...)' to fake your own response OR consider checking this.fakedPeople on your test class`);
|
327
305
|
for (const base of payload?.roleBases ?? []) {
|
328
|
-
const faked = getFakedRecordsByRoleBase(
|
306
|
+
const faked = getFakedRecordsByRoleBase(base);
|
329
307
|
if (faked) {
|
330
308
|
people.push(...faked);
|
331
309
|
}
|
332
310
|
}
|
333
311
|
return {
|
334
|
-
people: payload?.roleBases ? people :
|
312
|
+
people: payload?.roleBases ? people : FakerTracker_1.default.fakedPeople,
|
335
313
|
};
|
336
314
|
});
|
337
315
|
}
|
338
|
-
function getFakedRecordsByRoleBase(
|
316
|
+
function getFakedRecordsByRoleBase(base) {
|
339
317
|
//@ts-ignore
|
340
|
-
return
|
318
|
+
return FakerTracker_1.default[fakeTargetToPropName(singularToPlural(base))];
|
341
319
|
}
|
342
|
-
async function fakeListLocations(
|
320
|
+
async function fakeListLocations() {
|
343
321
|
await eventFaker_1.default.on('list-locations::v2020_12_25', ({ target, payload }) => {
|
344
322
|
return {
|
345
|
-
locations: applyPaging(
|
323
|
+
locations: applyPaging(FakerTracker_1.default.fakedLocations, payload).filter((l) => !target?.organizationId ||
|
346
324
|
l.organizationId === target?.organizationId),
|
347
325
|
};
|
348
326
|
});
|
349
327
|
}
|
350
|
-
async function fakeGetLocation(
|
328
|
+
async function fakeGetLocation() {
|
351
329
|
await eventFaker_1.default.on('get-location::v2020_12_25', ({ target }) => {
|
352
|
-
const match =
|
330
|
+
const match = FakerTracker_1.default.fakedLocations.find((l) => l.id === target.locationId);
|
353
331
|
if (!match) {
|
354
332
|
throw new SpruceError_1.default({
|
355
333
|
code: 'INVALID_TARGET',
|
@@ -361,43 +339,43 @@ async function fakeGetLocation(Class) {
|
|
361
339
|
};
|
362
340
|
});
|
363
341
|
}
|
364
|
-
async function fakeUpdateLocation(
|
342
|
+
async function fakeUpdateLocation() {
|
365
343
|
await eventFaker_1.default.on('update-location::v2020_12_25', ({ target, payload }) => {
|
366
344
|
const { locationId } = target;
|
367
|
-
let idx =
|
368
|
-
if (!
|
345
|
+
let idx = FakerTracker_1.default.fakedLocations.findIndex((l) => l.id === locationId);
|
346
|
+
if (!FakerTracker_1.default.fakedLocations[idx]) {
|
369
347
|
throw new SpruceError_1.default({
|
370
348
|
code: 'INVALID_TARGET',
|
371
349
|
friendlyMessage: `I could not find that location to update!`,
|
372
350
|
});
|
373
351
|
}
|
374
|
-
|
375
|
-
...
|
352
|
+
FakerTracker_1.default.fakedLocations[idx] = {
|
353
|
+
...FakerTracker_1.default.fakedLocations[idx],
|
376
354
|
...payload,
|
377
355
|
dateUpdated: Date.now(),
|
378
356
|
};
|
379
357
|
return {
|
380
|
-
location:
|
358
|
+
location: FakerTracker_1.default.fakedLocations[idx],
|
381
359
|
};
|
382
360
|
});
|
383
361
|
}
|
384
|
-
async function fakeDeleteOrganization(
|
362
|
+
async function fakeDeleteOrganization() {
|
385
363
|
await eventFaker_1.default.on('delete-organization::v2020_12_25', ({ target: { organizationId } }) => {
|
386
|
-
const idx =
|
364
|
+
const idx = FakerTracker_1.default.fakedOrganizations.findIndex((o) => o.id === organizationId);
|
387
365
|
if (idx === -1) {
|
388
366
|
throw new SpruceError_1.default({
|
389
367
|
code: 'INVALID_TARGET',
|
390
368
|
friendlyMessage: `I could not find that organization to delete!`,
|
391
369
|
});
|
392
370
|
}
|
393
|
-
const match =
|
394
|
-
|
371
|
+
const match = FakerTracker_1.default.fakedOrganizations[idx];
|
372
|
+
FakerTracker_1.default.fakedOrganizations.splice(idx, 1);
|
395
373
|
return {
|
396
374
|
organization: match,
|
397
375
|
};
|
398
376
|
});
|
399
377
|
}
|
400
|
-
async function fakeCreateLocation(
|
378
|
+
async function fakeCreateLocation() {
|
401
379
|
await eventFaker_1.default.on('create-location::v2020_12_25', ({ target, payload, source }) => {
|
402
380
|
const { personId } = source ?? {};
|
403
381
|
//@ts-ignore - delete when you find this (pushing through upgrades)
|
@@ -408,13 +386,12 @@ async function fakeCreateLocation(Class) {
|
|
408
386
|
...payload,
|
409
387
|
slug: payload.slug ?? spruce_skill_utils_1.namesUtil.toKebab(payload.name),
|
410
388
|
};
|
411
|
-
|
389
|
+
FakerTracker_1.default.fakedLocations.unshift(location);
|
412
390
|
if (personId) {
|
413
|
-
const role =
|
391
|
+
const role = FakerTracker_1.default.fakedRoles.find((r) => r.base === 'owner');
|
414
392
|
addPersonAsRoleToLocationOrOrg({
|
415
|
-
Class,
|
416
393
|
roleId: role.id,
|
417
|
-
person:
|
394
|
+
person: FakerTracker_1.default.fakedPerson,
|
418
395
|
locationId: location.id,
|
419
396
|
});
|
420
397
|
}
|
@@ -423,7 +400,7 @@ async function fakeCreateLocation(Class) {
|
|
423
400
|
};
|
424
401
|
});
|
425
402
|
}
|
426
|
-
async function fakeCreateOrganization(
|
403
|
+
async function fakeCreateOrganization() {
|
427
404
|
await eventFaker_1.default.on('create-organization::v2020_12_25', ({ payload }) => {
|
428
405
|
const organization = {
|
429
406
|
id: (0, test_utils_2.generateId)(),
|
@@ -432,23 +409,22 @@ async function fakeCreateOrganization(Class) {
|
|
432
409
|
...payload,
|
433
410
|
slug: payload.slug ?? spruce_skill_utils_1.namesUtil.toKebab(payload.name),
|
434
411
|
};
|
435
|
-
|
436
|
-
const roles = seedRoles(
|
412
|
+
FakerTracker_1.default.fakedOrganizations.unshift(organization);
|
413
|
+
const roles = seedRoles(organization.id);
|
437
414
|
addPersonAsRoleToLocationOrOrg({
|
438
|
-
Class,
|
439
415
|
organizationId: organization.id,
|
440
416
|
roleId: roles.find((r) => r.base === 'owner').id,
|
441
|
-
person:
|
417
|
+
person: FakerTracker_1.default.fakedPerson,
|
442
418
|
});
|
443
419
|
return {
|
444
420
|
organization,
|
445
421
|
};
|
446
422
|
});
|
447
423
|
}
|
448
|
-
async function fakeGetPerson(
|
424
|
+
async function fakeGetPerson() {
|
449
425
|
await eventFaker_1.default.on('get-person::v2020_12_25', ({ target }) => {
|
450
426
|
test_utils_1.assert.isTruthy(target?.personId, `@fake only supports 'get-person::v2020_12_25' when passing an id. To fake more, use 'eventFaker.on(...)'.`);
|
451
|
-
const person = getPersonById(
|
427
|
+
const person = getPersonById(target.personId);
|
452
428
|
if (!person) {
|
453
429
|
throw new SpruceError_1.default({
|
454
430
|
code: 'INVALID_TARGET',
|
@@ -460,19 +436,19 @@ async function fakeGetPerson(Class) {
|
|
460
436
|
};
|
461
437
|
});
|
462
438
|
}
|
463
|
-
async function fakeSkillLifecycleEvents(
|
439
|
+
async function fakeSkillLifecycleEvents() {
|
464
440
|
process.env.SKILL_ID = process.env.SKILL_ID || (0, test_utils_2.generateId)();
|
465
441
|
process.env.SKILL_API_KEY = process.env.SKILL_API_KEY || (0, test_utils_2.generateId)();
|
466
442
|
await eventFaker_1.default.on('register-skill::v2020_12_25', ({ payload }) => {
|
467
443
|
const skill = {
|
468
444
|
apiKey: (0, test_utils_2.generateId)(),
|
469
|
-
creators: [{ personId:
|
445
|
+
creators: [{ personId: FakerTracker_1.default.fakedPerson.id }],
|
470
446
|
dateCreated: new Date().getTime(),
|
471
447
|
id: (0, test_utils_2.generateId)(),
|
472
448
|
...payload,
|
473
449
|
slug: payload.slug ?? (0, test_utils_2.generateId)(),
|
474
450
|
};
|
475
|
-
|
451
|
+
FakerTracker_1.default.fakedSkills.unshift(skill);
|
476
452
|
return {
|
477
453
|
skill,
|
478
454
|
};
|
@@ -488,17 +464,17 @@ async function fakeSkillLifecycleEvents(Class) {
|
|
488
464
|
function buildCasualName(names) {
|
489
465
|
return `${names.firstName ?? 'friend'} ${names.lastName ? names.lastName[0] + '.' : ''}`.trim();
|
490
466
|
}
|
491
|
-
async function fakeGetOrganization(
|
467
|
+
async function fakeGetOrganization() {
|
492
468
|
await eventFaker_1.default.on('get-organization::v2020_12_25', ({ target }) => {
|
493
|
-
const match = findOrgFromTarget(
|
469
|
+
const match = findOrgFromTarget(target);
|
494
470
|
return {
|
495
471
|
organization: match,
|
496
472
|
};
|
497
473
|
});
|
498
474
|
}
|
499
|
-
async function fakeUpdateOrganization(
|
475
|
+
async function fakeUpdateOrganization() {
|
500
476
|
await eventFaker_1.default.on('update-organization::v2020_12_25', ({ target, payload }) => {
|
501
|
-
const match = findOrgFromTarget(
|
477
|
+
const match = findOrgFromTarget(target);
|
502
478
|
if (payload?.name) {
|
503
479
|
match.name = payload.name;
|
504
480
|
}
|
@@ -516,11 +492,11 @@ async function fakeUpdateOrganization(Class) {
|
|
516
492
|
};
|
517
493
|
});
|
518
494
|
}
|
519
|
-
async function fakeListOrganization(
|
495
|
+
async function fakeListOrganization() {
|
520
496
|
await eventFaker_1.default.on('list-organizations::v2020_12_25', (targetAndPayload) => {
|
521
497
|
const { payload } = targetAndPayload ?? {};
|
522
498
|
return {
|
523
|
-
organizations: applyPaging(
|
499
|
+
organizations: applyPaging(FakerTracker_1.default.fakedOrganizations, payload),
|
524
500
|
};
|
525
501
|
});
|
526
502
|
}
|
@@ -531,12 +507,12 @@ function applyPaging(records, payload) {
|
|
531
507
|
}
|
532
508
|
return copy;
|
533
509
|
}
|
534
|
-
async function seedOrganizations(
|
535
|
-
await
|
510
|
+
async function seedOrganizations(total) {
|
511
|
+
await getFixturesForActiveTest().seeder.seedOrganizations({
|
536
512
|
totalOrganizations: total,
|
537
513
|
});
|
538
514
|
}
|
539
|
-
function seedRoles(
|
515
|
+
function seedRoles(orgId) {
|
540
516
|
const roles = spruce_core_schemas_1.BASE_ROLES_WITH_META.map((r) => ({
|
541
517
|
id: (0, test_utils_2.generateId)(),
|
542
518
|
name: `Faked ${r.name}`,
|
@@ -545,34 +521,34 @@ function seedRoles(Class, orgId) {
|
|
545
521
|
organizationId: orgId,
|
546
522
|
isPublic: r.slug === 'guest',
|
547
523
|
}));
|
548
|
-
|
524
|
+
FakerTracker_1.default.fakedRoles.push(...roles);
|
549
525
|
return roles;
|
550
526
|
}
|
551
|
-
async function seedLocations(
|
552
|
-
await
|
527
|
+
async function seedLocations(total) {
|
528
|
+
await getFixturesForActiveTest().seeder.seedAccount({
|
553
529
|
totalLocations: total,
|
554
530
|
});
|
555
531
|
}
|
556
532
|
function buildSeeder(target) {
|
557
|
-
return async function seed(
|
558
|
-
if (
|
533
|
+
return async function seed(total) {
|
534
|
+
if (FakerTracker_1.default.fakedLocations.length === 0) {
|
559
535
|
test_utils_1.assert.fail(`You gotta @seed('locations', 1) before seeding teammates!`);
|
560
536
|
}
|
561
537
|
//@ts-ignore
|
562
|
-
await
|
538
|
+
await getFixturesForActiveTest().seeder[`seed${upperCaseFirst(target)}`]({
|
563
539
|
[`total${upperCaseFirst(target)}`]: total,
|
564
540
|
});
|
565
541
|
};
|
566
542
|
}
|
567
|
-
async function fakeWhoAmI(
|
543
|
+
async function fakeWhoAmI() {
|
568
544
|
await eventFaker_1.default.on('whoami::v2020_12_25', (targetAndPayload) => {
|
569
545
|
const { source } = targetAndPayload ?? {};
|
570
546
|
let { personId, proxyToken } = source ?? {};
|
571
547
|
if (proxyToken) {
|
572
|
-
personId =
|
548
|
+
personId = FakerTracker_1.default.fakedProxyTokens.find((t) => t.token === proxyToken)?.personId;
|
573
549
|
}
|
574
|
-
const person = getPersonById(
|
575
|
-
const skill =
|
550
|
+
const person = getPersonById(personId, false);
|
551
|
+
const skill = FakerTracker_1.default.fakedSkills.find((s) => s.id === source?.skillId);
|
576
552
|
return {
|
577
553
|
auth: {
|
578
554
|
skill,
|
@@ -584,12 +560,12 @@ async function fakeWhoAmI(Class) {
|
|
584
560
|
};
|
585
561
|
});
|
586
562
|
}
|
587
|
-
async function fakeRegisterProxyToken(
|
563
|
+
async function fakeRegisterProxyToken() {
|
588
564
|
await eventFaker_1.default.on('register-proxy-token::v2020_12_25', (targeAndPayload) => {
|
589
565
|
const { source } = targeAndPayload ?? {};
|
590
566
|
const token = (0, test_utils_2.generateId)();
|
591
567
|
if (source?.personId) {
|
592
|
-
|
568
|
+
FakerTracker_1.default.fakedProxyTokens.push({
|
593
569
|
personId: source.personId,
|
594
570
|
token,
|
595
571
|
});
|
@@ -599,29 +575,29 @@ async function fakeRegisterProxyToken(Class) {
|
|
599
575
|
};
|
600
576
|
});
|
601
577
|
}
|
602
|
-
async function fakeAuthenticationEvents(
|
578
|
+
async function fakeAuthenticationEvents() {
|
603
579
|
await eventFaker_1.default.on('request-pin::v2020_12_25', ({ payload }) => {
|
604
580
|
const formattedPhone = (0, schema_1.formatPhoneNumber)(payload.phone);
|
605
|
-
let person =
|
581
|
+
let person = FakerTracker_1.default.fakedPeople.find((p) => p.phone === formattedPhone);
|
606
582
|
if (!person) {
|
607
583
|
person =
|
608
|
-
|
609
|
-
?
|
584
|
+
FakerTracker_1.default.fakedPerson?.phone === formattedPhone
|
585
|
+
? FakerTracker_1.default.fakedPerson
|
610
586
|
: {
|
611
587
|
id: (0, test_utils_2.generateId)(),
|
612
588
|
casualName: 'friend',
|
613
589
|
dateCreated: new Date().getTime(),
|
614
590
|
phone: (0, schema_1.formatPhoneNumber)(formattedPhone),
|
615
591
|
};
|
616
|
-
|
592
|
+
FakerTracker_1.default.fakedPeople.push(person);
|
617
593
|
}
|
618
594
|
return {
|
619
595
|
challenge: person.phone,
|
620
596
|
};
|
621
597
|
});
|
622
598
|
await eventFaker_1.default.on('confirm-pin::v2020_12_25', ({ payload }) => {
|
623
|
-
const idx =
|
624
|
-
const person =
|
599
|
+
const idx = FakerTracker_1.default.fakedPeople.findIndex((p) => p.phone === payload.challenge);
|
600
|
+
const person = FakerTracker_1.default.fakedPeople[idx];
|
625
601
|
if (!person) {
|
626
602
|
throw new SpruceError_1.default({
|
627
603
|
code: 'INVALID_PIN',
|
@@ -630,7 +606,7 @@ async function fakeAuthenticationEvents(Class) {
|
|
630
606
|
//@ts-ignore
|
631
607
|
delete person._challenge;
|
632
608
|
const token = (0, test_utils_2.generateId)();
|
633
|
-
|
609
|
+
FakerTracker_1.default.fakedTokens.push({
|
634
610
|
personId: person.id,
|
635
611
|
token,
|
636
612
|
});
|
@@ -643,7 +619,7 @@ async function fakeAuthenticationEvents(Class) {
|
|
643
619
|
});
|
644
620
|
await eventFaker_1.default.on('authenticate::v2020_12_25', ({ payload }) => {
|
645
621
|
const { token, apiKey, skillId } = payload ?? {};
|
646
|
-
let skill =
|
622
|
+
let skill = FakerTracker_1.default.fakedSkills.find((s) => s.apiKey === apiKey && s.id === skillId);
|
647
623
|
if (!skill &&
|
648
624
|
apiKey &&
|
649
625
|
skillId &&
|
@@ -666,12 +642,12 @@ async function fakeAuthenticationEvents(Class) {
|
|
666
642
|
},
|
667
643
|
};
|
668
644
|
}
|
669
|
-
const match =
|
645
|
+
const match = FakerTracker_1.default.fakedTokens.find((f) => f.token === token);
|
670
646
|
if (!match) {
|
671
647
|
//@ts-ignore
|
672
648
|
throw new SpruceError_1.default({ code: 'INVALID_AUTH_TOKEN' });
|
673
649
|
}
|
674
|
-
const person = getPersonById(
|
650
|
+
const person = getPersonById(match.personId);
|
675
651
|
return {
|
676
652
|
type: 'authenticated',
|
677
653
|
auth: {
|
@@ -680,14 +656,14 @@ async function fakeAuthenticationEvents(Class) {
|
|
680
656
|
};
|
681
657
|
});
|
682
658
|
}
|
683
|
-
async function fakeInstallEvents(
|
659
|
+
async function fakeInstallEvents() {
|
684
660
|
await eventFaker_1.default.on('is-skill-installed::v2020_12_25', ({ payload }) => {
|
685
661
|
return {
|
686
|
-
isInstalled: !!
|
662
|
+
isInstalled: !!FakerTracker_1.default.fakedInstalledSkills.find((i) => i.skillId === payload?.skillId),
|
687
663
|
};
|
688
664
|
});
|
689
665
|
await eventFaker_1.default.on('install-skill::v2020_12_25', ({ target, payload }) => {
|
690
|
-
|
666
|
+
FakerTracker_1.default.fakedInstalledSkills.push({
|
691
667
|
orgId: target.organizationId,
|
692
668
|
skillId: payload.skillId,
|
693
669
|
});
|
@@ -695,7 +671,7 @@ async function fakeInstallEvents(Class) {
|
|
695
671
|
});
|
696
672
|
await eventFaker_1.default.on('list-skills::v2020_12_25', ({ payload }) => {
|
697
673
|
const namespaces = payload?.namespaces;
|
698
|
-
const matches =
|
674
|
+
const matches = FakerTracker_1.default.fakedSkills.filter((s) => (namespaces?.indexOf(s.slug) ?? 0) > -1);
|
699
675
|
if (matches.length === 0) {
|
700
676
|
throw new SpruceError_1.default({
|
701
677
|
//@ts-ignore
|
@@ -729,8 +705,8 @@ function singularToPlural(target) {
|
|
729
705
|
return target + 's';
|
730
706
|
}
|
731
707
|
let shouldSkipNextReset = false;
|
732
|
-
function findOrgFromTarget(
|
733
|
-
const match =
|
708
|
+
function findOrgFromTarget(target) {
|
709
|
+
const match = FakerTracker_1.default.fakedOrganizations.find((o) => o.id === target.organizationId);
|
734
710
|
if (!match) {
|
735
711
|
throw new SpruceError_1.default({
|
736
712
|
code: 'INVALID_TARGET',
|
@@ -742,3 +718,7 @@ function findOrgFromTarget(Class, target) {
|
|
742
718
|
async function fakeRegisterListeners() {
|
743
719
|
await eventFaker_1.default.on('register-listeners::v2020_12_25', async () => { });
|
744
720
|
}
|
721
|
+
function getFixturesForActiveTest() {
|
722
|
+
const Test = test_utils_1.SpruceTestResolver.getActiveTest();
|
723
|
+
return FakerTracker_1.default.getFixtures(Test.cwd);
|
724
|
+
}
|