@oxyhq/core 20.1.0 → 21.0.1
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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/HttpService.js +47 -8
- package/dist/cjs/boot/sessionColdBoot.js +107 -8
- package/dist/cjs/i18n/locales/en-US.json +26 -4
- package/dist/cjs/i18n/locales/es-ES.json +26 -4
- package/dist/cjs/i18n/locales/locales/en-US.json +26 -4
- package/dist/cjs/i18n/locales/locales/es-ES.json +26 -4
- package/dist/cjs/index.js +57 -16
- package/dist/cjs/inference/OxyInferenceClient.js +330 -0
- package/dist/cjs/mixins/OxyServices.accounts.js +5 -72
- package/dist/cjs/mixins/OxyServices.auth.js +27 -3
- package/dist/cjs/mixins/OxyServices.inference.js +59 -0
- package/dist/cjs/mixins/OxyServices.utility.js +18 -6
- package/dist/cjs/mixins/index.js +6 -0
- package/dist/cjs/server/auth.js +76 -0
- package/dist/cjs/server/index.js +5 -1
- 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/HttpService.js +47 -8
- package/dist/esm/boot/sessionColdBoot.js +107 -8
- package/dist/esm/i18n/locales/en-US.json +26 -4
- package/dist/esm/i18n/locales/es-ES.json +26 -4
- package/dist/esm/i18n/locales/locales/en-US.json +26 -4
- package/dist/esm/i18n/locales/locales/es-ES.json +26 -4
- package/dist/esm/index.js +36 -10
- package/dist/esm/inference/OxyInferenceClient.js +325 -0
- package/dist/esm/mixins/OxyServices.accounts.js +5 -72
- package/dist/esm/mixins/OxyServices.auth.js +27 -3
- package/dist/esm/mixins/OxyServices.inference.js +56 -0
- package/dist/esm/mixins/OxyServices.utility.js +18 -6
- package/dist/esm/mixins/index.js +6 -0
- package/dist/esm/server/auth.js +72 -0
- package/dist/esm/server/index.js +1 -1
- 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/HttpService.d.ts +39 -1
- package/dist/types/boot/sessionColdBoot.d.ts +24 -4
- package/dist/types/index.d.ts +11 -4
- package/dist/types/inference/OxyInferenceClient.d.ts +324 -0
- package/dist/types/mixins/OxyServices.accounts.d.ts +73 -95
- package/dist/types/mixins/OxyServices.auth.d.ts +75 -3
- package/dist/types/mixins/OxyServices.inference.d.ts +95 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +44 -13
- package/dist/types/mixins/index.d.ts +2 -1
- package/dist/types/models/session.d.ts +11 -0
- package/dist/types/server/auth.d.ts +80 -0
- package/dist/types/server/index.d.ts +2 -2
- 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/HttpService.ts +50 -10
- package/src/__tests__/httpServiceUnwrapEnvelope.test.ts +115 -0
- package/src/boot/__tests__/sessionColdBoot.sharedDevice.test.ts +325 -0
- package/src/boot/sessionColdBoot.ts +133 -9
- package/src/i18n/locales/en-US.json +26 -4
- package/src/i18n/locales/es-ES.json +26 -4
- package/src/index.ts +94 -25
- package/src/inference/OxyInferenceClient.ts +590 -0
- package/src/inference/__tests__/OxyInferenceClient.test.ts +383 -0
- package/src/mixins/OxyServices.accounts.ts +75 -176
- package/src/mixins/OxyServices.auth.ts +67 -5
- package/src/mixins/OxyServices.inference.ts +57 -0
- package/src/mixins/OxyServices.utility.ts +58 -14
- package/src/mixins/__tests__/accounts.test.ts +57 -102
- package/src/mixins/__tests__/inferenceFactory.test.ts +58 -0
- package/src/mixins/__tests__/preSessionSkipAuth.test.ts +54 -1
- package/src/mixins/__tests__/serviceAuth.test.ts +2 -0
- package/src/mixins/index.ts +8 -0
- package/src/models/session.ts +11 -0
- package/src/server/__tests__/serviceTokenAttribution.test.ts +396 -0
- package/src/server/auth.ts +118 -0
- package/src/server/index.ts +6 -0
- 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__/accountDialogShape.test.ts +118 -0
- 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,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two halves of the retired flat account list that a text scan cannot prove
|
|
3
|
+
* gone.
|
|
4
|
+
*
|
|
5
|
+
* `scripts/validate-no-flat-account-list.mjs` bans the identifiers that left the
|
|
6
|
+
* packages outright — `SwitchableAccount`, `projectSwitchableAccounts`,
|
|
7
|
+
* `useSwitchableAccounts`, `useAccountStore` and the rest — because each of
|
|
8
|
+
* those names is unique enough that a match is always a reintroduction. Two
|
|
9
|
+
* pieces of the same removal are NOT: an `accounts` field on the snapshot and a
|
|
10
|
+
* `switchTo` method on the controller are ordinary words. `'accounts'` is a live
|
|
11
|
+
* VIEW name three lines away in the same file, so a scoped grep there would fire
|
|
12
|
+
* on the thing that is supposed to be there.
|
|
13
|
+
*
|
|
14
|
+
* ## Why this is a RUNTIME check and not a type-level one
|
|
15
|
+
*
|
|
16
|
+
* A `type Has<S, K> = K extends keyof S ? true : false` assertion reads better
|
|
17
|
+
* and is checked by nothing here. `packages/core/tsconfig*.json` all carry
|
|
18
|
+
* `"exclude": ["**\/__tests__"]`, so `tsc` never sees a test file, and
|
|
19
|
+
* `jest.config.js` sets `diagnostics: false`, so ts-jest transpiles without
|
|
20
|
+
* typechecking. That version was written first and MUTATION-TESTED: putting
|
|
21
|
+
* `accounts: string[]` back on the interface and `switchTo()` back on the class
|
|
22
|
+
* left both assertions green. It is recorded here because the type-level form is
|
|
23
|
+
* the obvious thing to reach for, and it cannot fail in this package.
|
|
24
|
+
*
|
|
25
|
+
* The residual gap of the runtime form is an OPTIONAL field nobody writes —
|
|
26
|
+
* `accounts?: SwitchableAccount[]` would satisfy `tsc` without appearing on a
|
|
27
|
+
* snapshot. A REQUIRED one cannot hide: `computeSnapshot()` is strict, so the
|
|
28
|
+
* field has to be populated, and then it is here. An optional list nothing
|
|
29
|
+
* writes is not the mechanism coming back.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import type { DeviceSessionState } from '@oxyhq/contracts';
|
|
33
|
+
import type { OxyServices } from '../../OxyServices';
|
|
34
|
+
import { SessionClient, type SessionClientHost } from '../SessionClient';
|
|
35
|
+
import { createAccountDialogController } from '../accountDialogController';
|
|
36
|
+
|
|
37
|
+
const host: SessionClientHost = {
|
|
38
|
+
makeRequest: jest.fn(async () => undefined),
|
|
39
|
+
getBaseURL: () => 'http://test.invalid',
|
|
40
|
+
getAccessToken: () => null,
|
|
41
|
+
getDeviceCredential: () => null,
|
|
42
|
+
onTokensChanged: () => () => undefined,
|
|
43
|
+
setTokens: jest.fn(),
|
|
44
|
+
getCurrentAccountId: () => null,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const oxyServices = {
|
|
48
|
+
getAccessToken: jest.fn(() => null),
|
|
49
|
+
getBaseURL: jest.fn(() => 'http://test.invalid'),
|
|
50
|
+
onTokensChanged: jest.fn(() => () => undefined),
|
|
51
|
+
getFileDownloadUrl: jest.fn((id: string) => `https://cdn/${id}`),
|
|
52
|
+
} as unknown as OxyServices;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Every key the snapshot carries, and the whole set of them.
|
|
56
|
+
*
|
|
57
|
+
* An exact set rather than `not.toContain('accounts')` on purpose: it fails on
|
|
58
|
+
* ANY new field, not only on one spelled the way the old one was. The snapshot
|
|
59
|
+
* is a published contract that four surfaces render from, so growing it should
|
|
60
|
+
* be a deliberate edit here — and a flat list would come back under a new name
|
|
61
|
+
* (`rows`, `switchable`, `entries`) at least as readily as under the old one,
|
|
62
|
+
* which a name-specific check would wave through.
|
|
63
|
+
*/
|
|
64
|
+
const SNAPSHOT_KEYS = [
|
|
65
|
+
'activatingContextId',
|
|
66
|
+
'activeContext',
|
|
67
|
+
'commonsAvailability',
|
|
68
|
+
'directory',
|
|
69
|
+
'error',
|
|
70
|
+
'loading',
|
|
71
|
+
'removingContextId',
|
|
72
|
+
'removingPrincipalId',
|
|
73
|
+
'signIn',
|
|
74
|
+
'view',
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
describe('the account dialog surface after the flat list was retired', () => {
|
|
78
|
+
it('carries the server directory, and no flat account list beside it', () => {
|
|
79
|
+
const controller = createAccountDialogController({
|
|
80
|
+
oxyServices,
|
|
81
|
+
sessionClient: new SessionClient(host),
|
|
82
|
+
clientId: 'oxy_dk_test',
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const snapshot = controller.getSnapshot();
|
|
86
|
+
|
|
87
|
+
// The absence half. `accounts` was the deduped union of the device's
|
|
88
|
+
// sign-ins and the CALLER's account graph — and a client holds one caller's
|
|
89
|
+
// graph and cannot enumerate another principal's, so on a device holding two
|
|
90
|
+
// people it answered with one person's accounts as though they were the
|
|
91
|
+
// device's.
|
|
92
|
+
expect(Object.keys(snapshot).sort()).toEqual(SNAPSHOT_KEYS);
|
|
93
|
+
|
|
94
|
+
// The positive control, in the same assertion and again here: a snapshot
|
|
95
|
+
// that stopped being built at all would satisfy "has no `accounts`" too.
|
|
96
|
+
expect(snapshot).toHaveProperty('directory');
|
|
97
|
+
expect(snapshot).toHaveProperty('activeContext');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('switches on a context pair, and offers no account-id switch', () => {
|
|
101
|
+
const controller = createAccountDialogController({
|
|
102
|
+
oxyServices,
|
|
103
|
+
sessionClient: new SessionClient(host),
|
|
104
|
+
clientId: 'oxy_dk_test',
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// `switchTo(accountId)` could not express WHICH person's route into a shared
|
|
108
|
+
// organization was being taken, so on a device where two people both reach
|
|
109
|
+
// one org it chose for the user. `activateContext(contextId)` names the
|
|
110
|
+
// pair, and `signOutContext` / `signOutPrincipal` are the two removals an
|
|
111
|
+
// account id likewise cannot name.
|
|
112
|
+
expect('switchTo' in controller).toBe(false);
|
|
113
|
+
|
|
114
|
+
expect(typeof controller.activateContext).toBe('function');
|
|
115
|
+
expect(typeof controller.signOutContext).toBe('function');
|
|
116
|
+
expect(typeof controller.signOutPrincipal).toBe('function');
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -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
|
+
});
|