@oxyhq/core 20.0.0 → 21.0.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/NOTICE +10 -9
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/boot/sessionColdBoot.js +107 -8
- package/dist/cjs/i18n/locales/en-US.json +19 -2
- package/dist/cjs/i18n/locales/es-ES.json +19 -2
- package/dist/cjs/i18n/locales/locales/en-US.json +19 -2
- package/dist/cjs/i18n/locales/locales/es-ES.json +19 -2
- package/dist/cjs/index.js +50 -16
- package/dist/cjs/mixins/OxyServices.auth.js +27 -3
- package/dist/cjs/mixins/OxyServices.chains.js +73 -0
- package/dist/cjs/mixins/OxyServices.store.js +266 -0
- package/dist/cjs/mixins/OxyServices.utility.js +159 -104
- package/dist/cjs/mixins/index.js +7 -0
- package/dist/cjs/server/rateLimit.js +15 -6
- package/dist/cjs/session/SessionClient.js +361 -1
- package/dist/cjs/session/accountDialogController.js +121 -147
- package/dist/cjs/session/accountSwitchTargets.js +75 -0
- package/dist/cjs/session/deviceDirectory.js +143 -0
- package/dist/cjs/session/deviceSwitcherRows.js +76 -0
- package/dist/cjs/session/projectSessionState.js +8 -1
- package/dist/cjs/session/sharedDeviceCredential.js +247 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/boot/sessionColdBoot.js +107 -8
- package/dist/esm/i18n/locales/en-US.json +19 -2
- package/dist/esm/i18n/locales/es-ES.json +19 -2
- package/dist/esm/i18n/locales/locales/en-US.json +19 -2
- package/dist/esm/i18n/locales/locales/es-ES.json +19 -2
- package/dist/esm/index.js +32 -10
- package/dist/esm/mixins/OxyServices.auth.js +27 -3
- package/dist/esm/mixins/OxyServices.chains.js +70 -0
- package/dist/esm/mixins/OxyServices.store.js +263 -0
- package/dist/esm/mixins/OxyServices.utility.js +159 -104
- package/dist/esm/mixins/index.js +7 -0
- package/dist/esm/server/rateLimit.js +15 -6
- package/dist/esm/session/SessionClient.js +362 -2
- package/dist/esm/session/accountDialogController.js +121 -147
- package/dist/esm/session/accountSwitchTargets.js +71 -0
- package/dist/esm/session/deviceDirectory.js +135 -0
- package/dist/esm/session/deviceSwitcherRows.js +72 -0
- package/dist/esm/session/projectSessionState.js +8 -2
- package/dist/esm/session/sharedDeviceCredential.js +239 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/boot/sessionColdBoot.d.ts +24 -4
- package/dist/types/index.d.ts +15 -3
- package/dist/types/mixins/OxyServices.auth.d.ts +75 -3
- package/dist/types/mixins/OxyServices.chains.d.ts +156 -0
- package/dist/types/mixins/OxyServices.store.d.ts +334 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +31 -8
- package/dist/types/mixins/index.d.ts +3 -1
- package/dist/types/models/session.d.ts +11 -0
- package/dist/types/session/SessionClient.d.ts +202 -1
- package/dist/types/session/accountDialogController.d.ts +76 -64
- package/dist/types/session/accountSwitchTargets.d.ts +64 -0
- package/dist/types/session/deviceDirectory.d.ts +182 -0
- package/dist/types/session/deviceSwitcherRows.d.ts +92 -0
- package/dist/types/session/projectSessionState.d.ts +29 -0
- package/dist/types/session/sharedDeviceCredential.d.ts +202 -0
- package/package.json +3 -3
- package/src/boot/__tests__/sessionColdBoot.sharedDevice.test.ts +325 -0
- package/src/boot/sessionColdBoot.ts +133 -9
- package/src/i18n/locales/en-US.json +19 -2
- package/src/i18n/locales/es-ES.json +19 -2
- package/src/index.ts +105 -18
- package/src/mixins/OxyServices.auth.ts +67 -5
- package/src/mixins/OxyServices.chains.ts +134 -0
- package/src/mixins/OxyServices.store.ts +585 -0
- package/src/mixins/OxyServices.utility.ts +161 -108
- package/src/mixins/__tests__/chains.test.ts +113 -0
- package/src/mixins/__tests__/preSessionSkipAuth.test.ts +54 -1
- package/src/mixins/__tests__/store.test.ts +304 -0
- package/src/mixins/__tests__/userTokenAuth.test.ts +746 -0
- package/src/mixins/index.ts +9 -0
- package/src/models/session.ts +11 -0
- package/src/server/__tests__/rateLimit.test.ts +47 -0
- package/src/server/rateLimit.ts +18 -8
- package/src/session/SessionClient.ts +386 -1
- package/src/session/__tests__/SessionClient.directory.test.ts +688 -0
- package/src/session/__tests__/accountDialogController.test.ts +411 -278
- package/src/session/__tests__/accountSwitchTargets.test.ts +132 -0
- package/src/session/__tests__/deviceDirectory.test.ts +422 -0
- package/src/session/__tests__/deviceSwitcherRows.test.ts +223 -0
- package/src/session/__tests__/projectSessionState.test.ts +17 -0
- package/src/session/__tests__/sharedDeviceCredential.test.ts +300 -0
- package/src/session/accountDialogController.ts +141 -179
- package/src/session/accountSwitchTargets.ts +87 -0
- package/src/session/deviceDirectory.ts +269 -0
- package/src/session/deviceSwitcherRows.ts +145 -0
- package/src/session/projectSessionState.ts +9 -3
- package/src/session/sharedDeviceCredential.ts +349 -0
- package/dist/cjs/session/accountProjection.js +0 -213
- package/dist/esm/session/accountProjection.js +0 -207
- package/dist/types/session/accountProjection.d.ts +0 -198
- package/src/session/__tests__/accountProjection.test.ts +0 -447
- package/src/session/accountProjection.ts +0 -354
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { ACCOUNT_KINDS } from '@oxyhq/contracts';
|
|
2
|
+
import type { User } from '../../models/interfaces';
|
|
3
|
+
import type { AccountNode } from '../../mixins/OxyServices.accounts';
|
|
4
|
+
import { canSwitchIntoAccount, isSwitchTargetAccount } from '../accountSwitchTargets';
|
|
5
|
+
|
|
6
|
+
function graphNode(id: string, over: Partial<AccountNode> = {}): AccountNode {
|
|
7
|
+
return {
|
|
8
|
+
accountId: id,
|
|
9
|
+
kind: 'organization',
|
|
10
|
+
parentAccountId: null,
|
|
11
|
+
account: { id, publicKey: `pk_${id}`, username: `user_${id}` } as User,
|
|
12
|
+
relationship: 'owner',
|
|
13
|
+
callerMembership: null,
|
|
14
|
+
...over,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('isSwitchTargetAccount', () => {
|
|
19
|
+
/**
|
|
20
|
+
* EXHAUSTIVE over `ACCOUNT_KINDS`, as one object equality rather than a
|
|
21
|
+
* per-kind assertion, for two reasons that a `channel → false` spot-check
|
|
22
|
+
* cannot give:
|
|
23
|
+
*
|
|
24
|
+
* - It distinguishes "excludes channels" from "excludes everything". Three
|
|
25
|
+
* kinds must come back `true` here, so a predicate that answered `false`
|
|
26
|
+
* unconditionally — the shape that empties a switcher instead of filtering
|
|
27
|
+
* it — fails on those three, not on the channel.
|
|
28
|
+
* - Adding a sixth kind fails this test with a missing key, forcing the
|
|
29
|
+
* decision to be made HERE rather than inherited silently from whichever
|
|
30
|
+
* literal comparison happened to be written first.
|
|
31
|
+
*/
|
|
32
|
+
it('answers by kind for an account the caller merely owns or is a member of', () => {
|
|
33
|
+
expect(
|
|
34
|
+
Object.fromEntries(
|
|
35
|
+
ACCOUNT_KINDS.map((kind) => [kind, isSwitchTargetAccount({ kind, relationship: 'owner' })]),
|
|
36
|
+
),
|
|
37
|
+
).toEqual({
|
|
38
|
+
personal: false,
|
|
39
|
+
organization: true,
|
|
40
|
+
project: true,
|
|
41
|
+
bot: true,
|
|
42
|
+
channel: false,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The `self` ground, and the reason this predicate is not `isActAsEligibleKind`.
|
|
48
|
+
*
|
|
49
|
+
* `personal` is act-as INELIGIBLE — assuming somebody's human login would be
|
|
50
|
+
* impersonation — so a switcher gated on that predicate alone would drop the
|
|
51
|
+
* caller's OWN account and render an empty list. `GET /accounts` resolves its
|
|
52
|
+
* caller through `resolveOperatorId`, so a `self` node is always the human
|
|
53
|
+
* operator's own personal account, even while they operate an org.
|
|
54
|
+
*/
|
|
55
|
+
it('admits the caller’s own personal account, which is act-as ineligible', () => {
|
|
56
|
+
expect(isSwitchTargetAccount({ kind: 'personal', relationship: 'self' })).toBe(true);
|
|
57
|
+
// Same kind, not the caller's own → refused. The `relationship` is doing the
|
|
58
|
+
// work, so neither half of the predicate can be deleted without a failure.
|
|
59
|
+
expect(isSwitchTargetAccount({ kind: 'personal', relationship: 'member' })).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('refuses an account with no kind information rather than assuming', () => {
|
|
63
|
+
expect(isSwitchTargetAccount({})).toBe(false);
|
|
64
|
+
expect(isSwitchTargetAccount({ kind: null })).toBe(false);
|
|
65
|
+
expect(isSwitchTargetAccount({ kind: undefined, relationship: 'owner' })).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('canSwitchIntoAccount', () => {
|
|
70
|
+
it('admits self without membership permissions', () => {
|
|
71
|
+
expect(canSwitchIntoAccount({ kind: 'personal', relationship: 'self' })).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('admits an owned switch target when membership is absent (owner baseline)', () => {
|
|
75
|
+
expect(canSwitchIntoAccount({ kind: 'organization', relationship: 'owner' })).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('requires account:act_as for member relationships', () => {
|
|
79
|
+
expect(
|
|
80
|
+
canSwitchIntoAccount({
|
|
81
|
+
kind: 'organization',
|
|
82
|
+
relationship: 'member',
|
|
83
|
+
callerMembership: {
|
|
84
|
+
_id: 'm1',
|
|
85
|
+
accountId: 'org1',
|
|
86
|
+
memberUserId: 'u1',
|
|
87
|
+
role: 'billing',
|
|
88
|
+
status: 'active',
|
|
89
|
+
permissions: ['account:read', 'billing:manage'],
|
|
90
|
+
inherit: true,
|
|
91
|
+
source: 'direct',
|
|
92
|
+
},
|
|
93
|
+
}),
|
|
94
|
+
).toBe(false);
|
|
95
|
+
|
|
96
|
+
expect(
|
|
97
|
+
canSwitchIntoAccount({
|
|
98
|
+
kind: 'organization',
|
|
99
|
+
relationship: 'member',
|
|
100
|
+
callerMembership: {
|
|
101
|
+
_id: 'm2',
|
|
102
|
+
accountId: 'org1',
|
|
103
|
+
memberUserId: 'u1',
|
|
104
|
+
role: 'admin',
|
|
105
|
+
status: 'active',
|
|
106
|
+
permissions: ['account:act_as', 'account:read'],
|
|
107
|
+
inherit: true,
|
|
108
|
+
source: 'direct',
|
|
109
|
+
},
|
|
110
|
+
}),
|
|
111
|
+
).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('refuses channels even with act_as permission', () => {
|
|
115
|
+
expect(
|
|
116
|
+
canSwitchIntoAccount({
|
|
117
|
+
kind: 'channel',
|
|
118
|
+
relationship: 'owner',
|
|
119
|
+
callerMembership: {
|
|
120
|
+
_id: 'm3',
|
|
121
|
+
accountId: 'chan1',
|
|
122
|
+
memberUserId: 'u1',
|
|
123
|
+
role: 'owner',
|
|
124
|
+
status: 'active',
|
|
125
|
+
permissions: ['account:act_as'],
|
|
126
|
+
inherit: true,
|
|
127
|
+
source: 'direct',
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device directory's two halves, kept apart.
|
|
3
|
+
*
|
|
4
|
+
* Every fixture here puts the SAME organization under TWO principals, because
|
|
5
|
+
* that is the shape the flat projection cannot hold and the shape where a loose
|
|
6
|
+
* reading of "find the active context" and the correct one disagree: matching on
|
|
7
|
+
* `accountId` finds whichever person is enumerated first, and on this device
|
|
8
|
+
* that is the wrong human.
|
|
9
|
+
*/
|
|
10
|
+
import type { DeviceDirectory, DeviceDirectoryProfile } from '@oxyhq/contracts';
|
|
11
|
+
import {
|
|
12
|
+
canActivateContext,
|
|
13
|
+
directoryDisplayName,
|
|
14
|
+
directoryHandle,
|
|
15
|
+
projectDevicePrincipals,
|
|
16
|
+
resolveActiveContext,
|
|
17
|
+
resolveDeviceContext,
|
|
18
|
+
} from '../deviceDirectory';
|
|
19
|
+
|
|
20
|
+
const profileFor = (id: string): DeviceDirectoryProfile => ({ id, username: id });
|
|
21
|
+
|
|
22
|
+
const NATE = profileFor('nate');
|
|
23
|
+
const ALICE = profileFor('alice');
|
|
24
|
+
const ORG = profileFor('org');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `The Oxy Collective` reachable through Nate (who owns it) AND through Alice
|
|
28
|
+
* (a member). Nate's route is enumerated FIRST; Alice's is the active one.
|
|
29
|
+
*/
|
|
30
|
+
function sharedDeviceDirectory(activeContextId: string | null): DeviceDirectory {
|
|
31
|
+
return {
|
|
32
|
+
deviceId: 'd1',
|
|
33
|
+
revision: 7,
|
|
34
|
+
activeContextId,
|
|
35
|
+
updatedAt: 1_720_000_000_000,
|
|
36
|
+
principals: [
|
|
37
|
+
{
|
|
38
|
+
id: 'p-nate',
|
|
39
|
+
userId: 'nate',
|
|
40
|
+
authuser: 0,
|
|
41
|
+
user: NATE,
|
|
42
|
+
contexts: [
|
|
43
|
+
{
|
|
44
|
+
id: 'ctx-nate-self',
|
|
45
|
+
accountId: 'nate',
|
|
46
|
+
kind: 'personal',
|
|
47
|
+
relationship: 'self',
|
|
48
|
+
account: NATE,
|
|
49
|
+
onDevice: true,
|
|
50
|
+
available: true,
|
|
51
|
+
active: false,
|
|
52
|
+
lastUsedAt: 1_719_000_000_000,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'ctx-nate-org',
|
|
56
|
+
accountId: 'org',
|
|
57
|
+
kind: 'organization',
|
|
58
|
+
relationship: 'owner',
|
|
59
|
+
account: ORG,
|
|
60
|
+
onDevice: true,
|
|
61
|
+
available: true,
|
|
62
|
+
active: false,
|
|
63
|
+
lastUsedAt: 1_719_500_000_000,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: 'p-alice',
|
|
69
|
+
userId: 'alice',
|
|
70
|
+
authuser: 1,
|
|
71
|
+
user: ALICE,
|
|
72
|
+
contexts: [
|
|
73
|
+
{
|
|
74
|
+
id: 'ctx-alice-self',
|
|
75
|
+
accountId: 'alice',
|
|
76
|
+
kind: 'personal',
|
|
77
|
+
relationship: 'self',
|
|
78
|
+
account: ALICE,
|
|
79
|
+
onDevice: true,
|
|
80
|
+
available: true,
|
|
81
|
+
active: false,
|
|
82
|
+
lastUsedAt: null,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 'ctx-alice-org',
|
|
86
|
+
accountId: 'org',
|
|
87
|
+
kind: 'organization',
|
|
88
|
+
relationship: 'member',
|
|
89
|
+
account: ORG,
|
|
90
|
+
onDevice: false,
|
|
91
|
+
available: false,
|
|
92
|
+
active: true,
|
|
93
|
+
lastUsedAt: null,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
describe('resolveActiveContext', () => {
|
|
102
|
+
it('resolves the actor through the CONTEXT, not through the account', () => {
|
|
103
|
+
const context = resolveActiveContext(sharedDeviceDirectory('ctx-alice-org'));
|
|
104
|
+
|
|
105
|
+
// Both principals reach `org`; only one of them is the active context's.
|
|
106
|
+
expect(context?.actor).toEqual({
|
|
107
|
+
principalId: 'p-alice',
|
|
108
|
+
userId: 'alice',
|
|
109
|
+
authuser: 1,
|
|
110
|
+
profile: ALICE,
|
|
111
|
+
});
|
|
112
|
+
expect(context?.subject.accountId).toBe('org');
|
|
113
|
+
expect(context?.contextId).toBe('ctx-alice-org');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('marks a delegated context delegated and a personal one not', () => {
|
|
117
|
+
expect(resolveActiveContext(sharedDeviceDirectory('ctx-alice-org'))?.isDelegated).toBe(true);
|
|
118
|
+
expect(resolveActiveContext(sharedDeviceDirectory('ctx-nate-self'))?.isDelegated).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('carries the subject facts a switcher renders verbatim', () => {
|
|
122
|
+
const context = resolveActiveContext(sharedDeviceDirectory('ctx-alice-org'));
|
|
123
|
+
|
|
124
|
+
// `available: false` is a membership that was revoked. It is returned as a
|
|
125
|
+
// row, not omitted, so the UI can explain it instead of silently dropping it.
|
|
126
|
+
expect(context?.subject).toEqual({
|
|
127
|
+
accountId: 'org',
|
|
128
|
+
kind: 'organization',
|
|
129
|
+
relationship: 'member',
|
|
130
|
+
profile: ORG,
|
|
131
|
+
onDevice: false,
|
|
132
|
+
available: false,
|
|
133
|
+
lastUsedAt: null,
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('is null for no directory, no active context, and an active id nobody holds', () => {
|
|
138
|
+
expect(resolveActiveContext(null)).toBeNull();
|
|
139
|
+
expect(resolveActiveContext(sharedDeviceDirectory(null))).toBeNull();
|
|
140
|
+
// A directory that disagrees with itself resolves to "nothing active",
|
|
141
|
+
// never to whichever context happened to look closest.
|
|
142
|
+
expect(resolveActiveContext(sharedDeviceDirectory('ctx-healed-away'))).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('refuses to read an ACCOUNT id as if it named a context', () => {
|
|
146
|
+
// `org` is a real account on this device, reachable through both people. A
|
|
147
|
+
// resolver that fell back to matching on `accountId` would answer with one
|
|
148
|
+
// of them — a guess about WHICH HUMAN is signed in, made inside the path
|
|
149
|
+
// that decides what the app renders and which bearer it holds. The whole
|
|
150
|
+
// reason activation takes a `contextId` is that this guess is not ours to
|
|
151
|
+
// make, so it resolves to nothing instead.
|
|
152
|
+
expect(resolveActiveContext(sharedDeviceDirectory('org'))).toBeNull();
|
|
153
|
+
expect(resolveDeviceContext(sharedDeviceDirectory(null), 'org')).toBeNull();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('resolveDeviceContext', () => {
|
|
158
|
+
it('resolves each route to the same account under its own principal', () => {
|
|
159
|
+
const directory = sharedDeviceDirectory('ctx-alice-org');
|
|
160
|
+
|
|
161
|
+
expect(resolveDeviceContext(directory, 'ctx-nate-org')?.actor.userId).toBe('nate');
|
|
162
|
+
expect(resolveDeviceContext(directory, 'ctx-alice-org')?.actor.userId).toBe('alice');
|
|
163
|
+
expect(resolveDeviceContext(directory, 'ctx-nate-org')?.subject.accountId).toBe(
|
|
164
|
+
resolveDeviceContext(directory, 'ctx-alice-org')?.subject.accountId,
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('is null for an unknown context id and a null directory', () => {
|
|
169
|
+
expect(resolveDeviceContext(sharedDeviceDirectory(null), 'nope')).toBeNull();
|
|
170
|
+
expect(resolveDeviceContext(null, 'ctx-nate-self')).toBeNull();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('canActivateContext', () => {
|
|
175
|
+
/**
|
|
176
|
+
* A principal whose OWN personal session has died. Their delegated context
|
|
177
|
+
* still holds a live delegated session, so `onDevice` is true — and it is
|
|
178
|
+
* still not activatable, because activation has no proof of who is acting
|
|
179
|
+
* once the human's session is gone. The server answers such a request with a
|
|
180
|
+
* 403 and heals the row away, so a switcher that offered it would be offering
|
|
181
|
+
* a button that deletes the row it sits on.
|
|
182
|
+
*/
|
|
183
|
+
function deadPrincipalDirectory(): DeviceDirectory {
|
|
184
|
+
return {
|
|
185
|
+
deviceId: 'd1',
|
|
186
|
+
revision: 3,
|
|
187
|
+
activeContextId: null,
|
|
188
|
+
updatedAt: 1_720_000_000_000,
|
|
189
|
+
principals: [
|
|
190
|
+
{
|
|
191
|
+
id: 'p-nate',
|
|
192
|
+
userId: 'nate',
|
|
193
|
+
authuser: 0,
|
|
194
|
+
user: NATE,
|
|
195
|
+
contexts: [
|
|
196
|
+
{
|
|
197
|
+
id: 'ctx-nate-self',
|
|
198
|
+
accountId: 'nate',
|
|
199
|
+
kind: 'personal',
|
|
200
|
+
relationship: 'self',
|
|
201
|
+
account: NATE,
|
|
202
|
+
onDevice: true,
|
|
203
|
+
available: false,
|
|
204
|
+
active: false,
|
|
205
|
+
lastUsedAt: null,
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
id: 'ctx-nate-org',
|
|
209
|
+
accountId: 'org',
|
|
210
|
+
kind: 'organization',
|
|
211
|
+
relationship: 'owner',
|
|
212
|
+
account: ORG,
|
|
213
|
+
// A LIVE delegated session, under a dead principal.
|
|
214
|
+
onDevice: true,
|
|
215
|
+
available: false,
|
|
216
|
+
active: false,
|
|
217
|
+
lastUsedAt: null,
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
},
|
|
221
|
+
],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
it('refuses every context of a principal whose own session died', () => {
|
|
226
|
+
const directory = deadPrincipalDirectory();
|
|
227
|
+
const personal = resolveDeviceContext(directory, 'ctx-nate-self');
|
|
228
|
+
const delegated = resolveDeviceContext(directory, 'ctx-nate-org');
|
|
229
|
+
|
|
230
|
+
expect(delegated?.subject.onDevice).toBe(true);
|
|
231
|
+
expect(canActivateContext(delegated?.subject ?? { available: true })).toBe(false);
|
|
232
|
+
expect(canActivateContext(personal?.subject ?? { available: true })).toBe(false);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('admits a reachable context that has never been activated here', () => {
|
|
236
|
+
// `onDevice: false` is the ordinary "reachable, session minted on first
|
|
237
|
+
// activation" row. Requiring `onDevice` would hide every organization the
|
|
238
|
+
// person has not used on this device yet.
|
|
239
|
+
const directory: DeviceDirectory = {
|
|
240
|
+
deviceId: 'd1',
|
|
241
|
+
revision: 3,
|
|
242
|
+
activeContextId: null,
|
|
243
|
+
updatedAt: 1_720_000_000_000,
|
|
244
|
+
principals: [
|
|
245
|
+
{
|
|
246
|
+
id: 'p-nate',
|
|
247
|
+
userId: 'nate',
|
|
248
|
+
authuser: 0,
|
|
249
|
+
user: NATE,
|
|
250
|
+
contexts: [
|
|
251
|
+
{
|
|
252
|
+
id: 'ctx-nate-org',
|
|
253
|
+
accountId: 'org',
|
|
254
|
+
kind: 'organization',
|
|
255
|
+
relationship: 'owner',
|
|
256
|
+
account: ORG,
|
|
257
|
+
onDevice: false,
|
|
258
|
+
available: true,
|
|
259
|
+
active: false,
|
|
260
|
+
lastUsedAt: null,
|
|
261
|
+
},
|
|
262
|
+
],
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
};
|
|
266
|
+
const context = resolveDeviceContext(directory, 'ctx-nate-org');
|
|
267
|
+
|
|
268
|
+
expect(context?.subject.onDevice).toBe(false);
|
|
269
|
+
expect(canActivateContext(context?.subject ?? { available: false })).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('projectDevicePrincipals', () => {
|
|
274
|
+
it('puts the same organization under BOTH people, as two distinct rows', () => {
|
|
275
|
+
const groups = projectDevicePrincipals(sharedDeviceDirectory('ctx-alice-org'));
|
|
276
|
+
|
|
277
|
+
expect(groups.map((group) => group.principalId)).toEqual(['p-nate', 'p-alice']);
|
|
278
|
+
// The fact the flat list structurally cannot hold: one account, two routes,
|
|
279
|
+
// two different humans, two different context ids.
|
|
280
|
+
const orgRows = groups.flatMap((group) =>
|
|
281
|
+
group.contexts.filter((context) => context.subject.accountId === 'org'),
|
|
282
|
+
);
|
|
283
|
+
expect(orgRows).toHaveLength(2);
|
|
284
|
+
expect(orgRows.map((context) => context.contextId)).toEqual(['ctx-nate-org', 'ctx-alice-org']);
|
|
285
|
+
expect(orgRows.map((context) => context.actor.userId)).toEqual(['nate', 'alice']);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('marks only the person the ACTIVE context belongs to as active', () => {
|
|
289
|
+
const throughAlice = projectDevicePrincipals(sharedDeviceDirectory('ctx-alice-org'));
|
|
290
|
+
expect(throughAlice.map((group) => group.isActive)).toEqual([false, true]);
|
|
291
|
+
|
|
292
|
+
// Same account, reached through the other person: the active flag moves.
|
|
293
|
+
const throughNate = projectDevicePrincipals(sharedDeviceDirectory('ctx-nate-org'));
|
|
294
|
+
expect(throughNate.map((group) => group.isActive)).toEqual([true, false]);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('marks nobody active when the active id names a row nobody holds', () => {
|
|
298
|
+
const groups = projectDevicePrincipals(sharedDeviceDirectory('ctx-healed-away'));
|
|
299
|
+
expect(groups.some((group) => group.isActive)).toBe(false);
|
|
300
|
+
|
|
301
|
+
// An ACCOUNT id is not a context id, so it activates nobody either — the
|
|
302
|
+
// same refusal `resolveActiveContext` makes, and for the same reason.
|
|
303
|
+
expect(projectDevicePrincipals(sharedDeviceDirectory('org')).some((g) => g.isActive)).toBe(
|
|
304
|
+
false,
|
|
305
|
+
);
|
|
306
|
+
expect(projectDevicePrincipals(sharedDeviceDirectory(null)).some((g) => g.isActive)).toBe(
|
|
307
|
+
false,
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('preserves the server order and does not re-sort', () => {
|
|
312
|
+
// The account ids are chosen so the server's order and a plausible
|
|
313
|
+
// client-side re-sort DISAGREE: the server puts a principal's PERSONAL
|
|
314
|
+
// context first, and `zeta` sorts after `org`. With ids that happen to
|
|
315
|
+
// already be alphabetical, a re-sorting projection passes this test.
|
|
316
|
+
const directory: DeviceDirectory = {
|
|
317
|
+
deviceId: 'd1',
|
|
318
|
+
revision: 8,
|
|
319
|
+
activeContextId: null,
|
|
320
|
+
updatedAt: 1_720_000_000_000,
|
|
321
|
+
principals: [
|
|
322
|
+
{
|
|
323
|
+
id: 'p-zeta',
|
|
324
|
+
userId: 'zeta',
|
|
325
|
+
authuser: 0,
|
|
326
|
+
user: profileFor('zeta'),
|
|
327
|
+
contexts: [
|
|
328
|
+
{
|
|
329
|
+
id: 'ctx-zeta-self',
|
|
330
|
+
accountId: 'zeta',
|
|
331
|
+
kind: 'personal',
|
|
332
|
+
relationship: 'self',
|
|
333
|
+
account: profileFor('zeta'),
|
|
334
|
+
onDevice: true,
|
|
335
|
+
available: true,
|
|
336
|
+
active: false,
|
|
337
|
+
lastUsedAt: null,
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
id: 'ctx-zeta-org',
|
|
341
|
+
accountId: 'org',
|
|
342
|
+
kind: 'organization',
|
|
343
|
+
relationship: 'owner',
|
|
344
|
+
account: ORG,
|
|
345
|
+
onDevice: true,
|
|
346
|
+
available: true,
|
|
347
|
+
active: false,
|
|
348
|
+
lastUsedAt: null,
|
|
349
|
+
},
|
|
350
|
+
],
|
|
351
|
+
},
|
|
352
|
+
],
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const groups = projectDevicePrincipals(directory);
|
|
356
|
+
|
|
357
|
+
expect(groups[0].contexts.map((context) => context.contextId)).toEqual([
|
|
358
|
+
'ctx-zeta-self',
|
|
359
|
+
'ctx-zeta-org',
|
|
360
|
+
]);
|
|
361
|
+
// And principals keep the server's `authuser` ordering, not a re-derived one.
|
|
362
|
+
expect(projectDevicePrincipals(sharedDeviceDirectory(null)).map((g) => g.authuser)).toEqual([
|
|
363
|
+
0, 1,
|
|
364
|
+
]);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('keeps a person who has no contexts left', () => {
|
|
368
|
+
// Their every context was removed while they remain on the device. Dropping
|
|
369
|
+
// the group would strand "sign out of this person" with no row to hang off.
|
|
370
|
+
const directory: DeviceDirectory = {
|
|
371
|
+
deviceId: 'd1',
|
|
372
|
+
revision: 4,
|
|
373
|
+
activeContextId: null,
|
|
374
|
+
updatedAt: 1_720_000_000_000,
|
|
375
|
+
principals: [{ id: 'p-nate', userId: 'nate', authuser: 0, user: NATE, contexts: [] }],
|
|
376
|
+
};
|
|
377
|
+
const groups = projectDevicePrincipals(directory);
|
|
378
|
+
|
|
379
|
+
expect(groups).toHaveLength(1);
|
|
380
|
+
expect(groups[0].principalId).toBe('p-nate');
|
|
381
|
+
expect(groups[0].contexts).toEqual([]);
|
|
382
|
+
expect(groups[0].isActive).toBe(false);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it('is empty for no directory', () => {
|
|
386
|
+
expect(projectDevicePrincipals(null)).toEqual([]);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
describe('directoryDisplayName / directoryHandle', () => {
|
|
391
|
+
it('prefers the API displayName, then the handle, then the sentinel', () => {
|
|
392
|
+
expect(directoryDisplayName({ id: 'u', username: 'nate', name: { displayName: 'Nate I.' } }))
|
|
393
|
+
.toBe('Nate I.');
|
|
394
|
+
expect(directoryDisplayName({ id: 'u', username: 'nate' })).toBe('nate');
|
|
395
|
+
// No displayName and no username: the localized unnamed sentinel, never an
|
|
396
|
+
// account id and never a synthesized name.
|
|
397
|
+
const unnamed = directoryDisplayName({ id: 'u', username: '' });
|
|
398
|
+
expect(unnamed).not.toBe('');
|
|
399
|
+
expect(unnamed).not.toBe('u');
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('does not rebuild the first/last chain the identity contract forbids', () => {
|
|
403
|
+
// `name.first`/`name.last` without a `displayName` means the server
|
|
404
|
+
// deliberately composed no display name. Falling through to the handle is
|
|
405
|
+
// the contract; recomposing "Nate Isern" here would put a second, divergent
|
|
406
|
+
// naming rule in the client.
|
|
407
|
+
expect(
|
|
408
|
+
directoryDisplayName({ id: 'u', username: 'nate', name: { first: 'Nate', last: 'Isern' } }),
|
|
409
|
+
).toBe('nate');
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it('treats a whitespace-only displayName as absent', () => {
|
|
413
|
+
expect(directoryDisplayName({ id: 'u', username: 'nate', name: { displayName: ' ' } })).toBe(
|
|
414
|
+
'nate',
|
|
415
|
+
);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('answers the handle, or null when there is no usable username', () => {
|
|
419
|
+
expect(directoryHandle({ id: 'u', username: 'nate' })).toBe('nate');
|
|
420
|
+
expect(directoryHandle({ id: 'u', username: '' })).toBeNull();
|
|
421
|
+
});
|
|
422
|
+
});
|