@oxyhq/core 18.0.0 → 19.1.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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/i18n/locales/en-US.json +49 -5
- package/dist/cjs/i18n/locales/es-ES.json +49 -5
- package/dist/cjs/i18n/locales/locales/en-US.json +49 -5
- package/dist/cjs/i18n/locales/locales/es-ES.json +49 -5
- package/dist/cjs/index.js +17 -6
- package/dist/cjs/mixins/OxyServices.accounts.js +69 -31
- package/dist/cjs/mixins/OxyServices.followGraph.js +204 -0
- package/dist/cjs/mixins/OxyServices.user.js +17 -20
- package/dist/cjs/mixins/index.js +4 -0
- package/dist/cjs/server/index.js +8 -2
- package/dist/cjs/server/userInvalidation.js +6 -28
- package/dist/cjs/session/accountProjection.js +45 -9
- package/dist/cjs/utils/accountCacheSweep.js +80 -0
- package/dist/cjs/utils/identityCacheSweep.js +97 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/i18n/locales/en-US.json +49 -5
- package/dist/esm/i18n/locales/es-ES.json +49 -5
- package/dist/esm/i18n/locales/locales/en-US.json +49 -5
- package/dist/esm/i18n/locales/locales/es-ES.json +49 -5
- package/dist/esm/index.js +8 -2
- package/dist/esm/mixins/OxyServices.accounts.js +64 -30
- package/dist/esm/mixins/OxyServices.followGraph.js +201 -0
- package/dist/esm/mixins/OxyServices.user.js +17 -20
- package/dist/esm/mixins/index.js +4 -0
- package/dist/esm/server/index.js +5 -1
- package/dist/esm/server/userInvalidation.js +5 -26
- package/dist/esm/session/accountProjection.js +44 -9
- package/dist/esm/utils/accountCacheSweep.js +75 -0
- package/dist/esm/utils/identityCacheSweep.js +92 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/mixins/OxyServices.accounts.d.ts +91 -34
- package/dist/types/mixins/OxyServices.followGraph.d.ts +211 -0
- package/dist/types/mixins/OxyServices.user.d.ts +10 -7
- package/dist/types/mixins/index.d.ts +2 -1
- package/dist/types/models/interfaces.d.ts +11 -3
- package/dist/types/server/index.d.ts +4 -2
- package/dist/types/server/userInvalidation.d.ts +5 -24
- package/dist/types/session/accountProjection.d.ts +38 -4
- package/dist/types/utils/accountCacheSweep.d.ts +75 -0
- package/dist/types/utils/identityCacheSweep.d.ts +80 -0
- package/package.json +2 -2
- package/src/i18n/locales/en-US.json +49 -5
- package/src/i18n/locales/es-ES.json +49 -5
- package/src/index.ts +15 -2
- package/src/mixins/OxyServices.accounts.ts +123 -45
- package/src/mixins/OxyServices.followGraph.ts +266 -0
- package/src/mixins/OxyServices.user.ts +17 -20
- package/src/mixins/__tests__/accounts.test.ts +5 -0
- package/src/mixins/__tests__/followGraph.test.ts +128 -0
- package/src/mixins/__tests__/identityWriteCacheInvalidation.test.ts +407 -0
- package/src/mixins/index.ts +5 -0
- package/src/models/interfaces.ts +11 -3
- package/src/server/__tests__/userInvalidation.test.ts +3 -20
- package/src/server/index.ts +5 -2
- package/src/server/userInvalidation.ts +8 -36
- package/src/session/__tests__/accountProjection.test.ts +109 -2
- package/src/session/accountProjection.ts +47 -9
- package/src/utils/__tests__/identityCacheSweep.test.ts +151 -0
- package/src/utils/accountCacheSweep.ts +93 -0
- package/src/utils/identityCacheSweep.ts +104 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow Graph Mixin Tests
|
|
3
|
+
*
|
|
4
|
+
* `makeRequest` is stubbed, so what these assert is the CONTRACT this mixin
|
|
5
|
+
* offers the applications above it: which request each method makes, and — the
|
|
6
|
+
* part worth a test rather than a comment — the things it must never send.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { OxyServices } from '../../OxyServices';
|
|
10
|
+
|
|
11
|
+
describe('OxyServices.followGraph', () => {
|
|
12
|
+
let oxy: OxyServices;
|
|
13
|
+
let makeRequest: jest.SpyInstance;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
oxy = new OxyServices({ baseURL: 'http://test.invalid' });
|
|
17
|
+
oxy.httpService.setTokens('test-token');
|
|
18
|
+
makeRequest = jest.spyOn(oxy, 'makeRequest').mockResolvedValue({} as never);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
makeRequest.mockRestore();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('the identities the client must not be able to state', () => {
|
|
26
|
+
it('never sends a follower id', async () => {
|
|
27
|
+
await oxy.followTarget('target-1');
|
|
28
|
+
await oxy.unfollowTarget('rel-1');
|
|
29
|
+
await oxy.listFollows();
|
|
30
|
+
|
|
31
|
+
// A client that could name the follower could forge a follow on somebody
|
|
32
|
+
// else's behalf. The server derives it from the session; there must be no
|
|
33
|
+
// parameter here that even looks like an alternative.
|
|
34
|
+
for (const call of makeRequest.mock.calls) {
|
|
35
|
+
expect(JSON.stringify(call)).not.toMatch(/user_?[Ii]d|follower/);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('sends no application id unless the caller explicitly names another app', async () => {
|
|
40
|
+
await oxy.setFollowApplicationMode('rel-1', 'disabled');
|
|
41
|
+
|
|
42
|
+
// The ordinary case is "this application", derived server-side. Sending an
|
|
43
|
+
// id here by default would make every app's own writes indistinguishable
|
|
44
|
+
// from one app acting on another's behalf, which is the privileged
|
|
45
|
+
// operation.
|
|
46
|
+
expect(makeRequest.mock.calls[0][2]).toEqual({ mode: 'disabled' });
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('passes an explicitly named application through, for the privileged path', async () => {
|
|
50
|
+
await oxy.setFollowApplicationMode('rel-1', 'enabled', 'app-9');
|
|
51
|
+
expect(makeRequest.mock.calls[0][2]).toEqual({ mode: 'enabled', applicationId: 'app-9' });
|
|
52
|
+
|
|
53
|
+
await oxy.restoreFollowInheritance('rel-1', 'app-9');
|
|
54
|
+
expect(makeRequest.mock.calls[1][1]).toContain('applicationId=app-9');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('request shapes', () => {
|
|
59
|
+
it('follows with PUT and no body when the follow is permanent', async () => {
|
|
60
|
+
await oxy.followTarget('target-1');
|
|
61
|
+
const [method, path, body] = makeRequest.mock.calls[0];
|
|
62
|
+
expect(method).toBe('PUT');
|
|
63
|
+
expect(path).toBe('/v2/follows/target-1');
|
|
64
|
+
expect(body).toEqual({});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('carries expiresIn for a timed follow', async () => {
|
|
68
|
+
await oxy.followTarget('target-1', { expiresIn: 72 * 60 * 60 });
|
|
69
|
+
expect(makeRequest.mock.calls[0][2]).toEqual({ expiresIn: 259200 });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('unfollows by relationship id, not by target', async () => {
|
|
73
|
+
// The relationship is the thing that exists; addressing the unfollow by
|
|
74
|
+
// target would make the server re-derive which relationship was meant,
|
|
75
|
+
// and get it wrong for any target a user can follow more than one way.
|
|
76
|
+
await oxy.unfollowTarget('rel-1');
|
|
77
|
+
expect(makeRequest.mock.calls[0].slice(0, 2)).toEqual(['DELETE', '/v2/follows/rel-1']);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('reads status per target', async () => {
|
|
81
|
+
await oxy.getFollowTargetStatus('target-1');
|
|
82
|
+
expect(makeRequest.mock.calls[0].slice(0, 2)).toEqual([
|
|
83
|
+
'GET',
|
|
84
|
+
'/v2/follows/target-1/status',
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('restores inheritance with no query string when the app means itself', async () => {
|
|
89
|
+
await oxy.restoreFollowInheritance('rel-1');
|
|
90
|
+
expect(makeRequest.mock.calls[0][1]).toBe('/v2/follows/rel-1/context');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('paginates by cursor and filters by kind', async () => {
|
|
94
|
+
await oxy.listFollows({ kind: 'oxy.topic', cursor: '2026-01-01T00:00:00.000Z', limit: 20 });
|
|
95
|
+
const path = makeRequest.mock.calls[0][1] as string;
|
|
96
|
+
expect(path).toContain('kind=oxy.topic');
|
|
97
|
+
expect(path).toContain('limit=20');
|
|
98
|
+
expect(path).toContain('cursor=');
|
|
99
|
+
// Never an offset: the list changes while it is read, and an offset skips
|
|
100
|
+
// or repeats rows exactly when it does.
|
|
101
|
+
expect(path).not.toContain('offset');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('escapes an id rather than letting it change the path', async () => {
|
|
105
|
+
await oxy.followTarget('../../admin');
|
|
106
|
+
expect(makeRequest.mock.calls[0][1]).toBe('/v2/follows/..%2F..%2Fadmin');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('caching', () => {
|
|
111
|
+
it('caches nothing', async () => {
|
|
112
|
+
await oxy.followTarget('t');
|
|
113
|
+
await oxy.getFollowTargetStatus('t');
|
|
114
|
+
await oxy.listFollows();
|
|
115
|
+
await oxy.unfollowTarget('r');
|
|
116
|
+
await oxy.setFollowApplicationMode('r', 'disabled');
|
|
117
|
+
await oxy.restoreFollowInheritance('r');
|
|
118
|
+
|
|
119
|
+
// A status cached across a write is the "follow reverts after navigating
|
|
120
|
+
// away and back" bug the legacy path had to fix with explicit
|
|
121
|
+
// invalidation. Not caching here leaves the app's own store as the single
|
|
122
|
+
// cache authority.
|
|
123
|
+
for (const call of makeRequest.mock.calls) {
|
|
124
|
+
expect(call[3]).toEqual({ cache: false });
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identity-cache invalidation for the profile WRITERS, against the REAL
|
|
3
|
+
* response cache.
|
|
4
|
+
*
|
|
5
|
+
* An account IS a user, and a profile screen never reads `/accounts/<id>` — it
|
|
6
|
+
* reads `GET /users/<id>` and `GET /profiles/username/<handle>`, both cached for
|
|
7
|
+
* five minutes in the caller's own process. `updateAccount` used to bust only
|
|
8
|
+
* the account-graph keys, so for up to five minutes after an edit a refetch
|
|
9
|
+
* handed back the PRE-EDIT profile from the client's own cache, with a
|
|
10
|
+
* perfectly healthy server ("I changed my channel's picture and it doesn't
|
|
11
|
+
* update until I reload the page").
|
|
12
|
+
*
|
|
13
|
+
* These tests drive the real `HttpService` cache over a mocked `fetch` rather
|
|
14
|
+
* than spying on `clearCacheEntry` / `clearCacheByPrefix`, because a spy proves
|
|
15
|
+
* only that SOME string was passed — not that the entry a read actually lands
|
|
16
|
+
* under was evicted. The load-bearing case is the RENAME: an implementation
|
|
17
|
+
* that busts the exact key for the handle in the write RESPONSE passes every
|
|
18
|
+
* assertion about the new handle while leaving the OLD handle's entry serving
|
|
19
|
+
* the pre-rename profile until its TTL. Both handles are warmed here so the two
|
|
20
|
+
* implementations disagree.
|
|
21
|
+
*
|
|
22
|
+
* `updateProfile` is covered here too, because it carried a SECOND, drifted
|
|
23
|
+
* hand-written copy of the same key list — it swept four of the six keys,
|
|
24
|
+
* missing `GET:/auth/lookup/` and `GET:/profiles/resolve`. Both writers now
|
|
25
|
+
* share one enumeration (`utils/identityCacheSweep`), and this is where that is
|
|
26
|
+
* asserted from the outside.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { OxyServices } from '../../OxyServices';
|
|
30
|
+
import type { AccountNode } from '../OxyServices.accounts';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A non-verified JWT whose payload decodes to the given claims — enough for the
|
|
34
|
+
* cache's identity tag and the bearer preflight (`jwtDecode` never checks a
|
|
35
|
+
* signature).
|
|
36
|
+
*/
|
|
37
|
+
function makeJwt(payload: Record<string, unknown>): string {
|
|
38
|
+
const b64url = (obj: Record<string, unknown>): string =>
|
|
39
|
+
Buffer.from(JSON.stringify(obj)).toString('base64url');
|
|
40
|
+
return `${b64url({ alg: 'none', typ: 'JWT' })}.${b64url({
|
|
41
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
42
|
+
...payload,
|
|
43
|
+
})}.sig`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A JSON `Response` in the API's `{ data: ... }` success envelope. */
|
|
47
|
+
function jsonResponse(data: unknown): Response {
|
|
48
|
+
return new Response(JSON.stringify({ data }), {
|
|
49
|
+
status: 200,
|
|
50
|
+
headers: { 'content-type': 'application/json' },
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const ACCOUNT_ID = 'acc1';
|
|
55
|
+
const PARENT_ID = 'root1';
|
|
56
|
+
const OLD_USERNAME = 'oldhandle';
|
|
57
|
+
const NEW_USERNAME = 'newhandle';
|
|
58
|
+
|
|
59
|
+
/** The write response: the account after a rename + a new picture. */
|
|
60
|
+
const renamedNode: AccountNode = {
|
|
61
|
+
accountId: ACCOUNT_ID,
|
|
62
|
+
kind: 'channel',
|
|
63
|
+
parentAccountId: PARENT_ID,
|
|
64
|
+
account: {
|
|
65
|
+
id: ACCOUNT_ID,
|
|
66
|
+
publicKey: 'pk-acc1',
|
|
67
|
+
username: NEW_USERNAME,
|
|
68
|
+
name: { displayName: 'Renamed Channel' },
|
|
69
|
+
avatar: 'file_new',
|
|
70
|
+
},
|
|
71
|
+
relationship: 'owner',
|
|
72
|
+
callerMembership: null,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
describe('updateAccount identity-cache invalidation (real cache)', () => {
|
|
76
|
+
let originalFetch: typeof globalThis.fetch;
|
|
77
|
+
let fetchMock: jest.Mock<Promise<Response>, [RequestInfo | URL, RequestInit?]>;
|
|
78
|
+
let oxy: OxyServices;
|
|
79
|
+
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
originalFetch = globalThis.fetch;
|
|
82
|
+
fetchMock = jest.fn();
|
|
83
|
+
globalThis.fetch = fetchMock as unknown as typeof globalThis.fetch;
|
|
84
|
+
|
|
85
|
+
oxy = new OxyServices({
|
|
86
|
+
baseURL: 'http://test.invalid',
|
|
87
|
+
enableRetry: false,
|
|
88
|
+
requestTimeout: 1000,
|
|
89
|
+
});
|
|
90
|
+
oxy.httpService.setTokens(makeJwt({ userId: 'operator-1' }));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
afterEach(() => {
|
|
94
|
+
globalThis.fetch = originalFetch;
|
|
95
|
+
jest.clearAllMocks();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Warm every cache entry an account can be served under, plus one unrelated
|
|
100
|
+
* entry that must SURVIVE. Returns the number of network calls made, so each
|
|
101
|
+
* assertion below can be expressed as "did this read hit the network again".
|
|
102
|
+
*/
|
|
103
|
+
async function warmCaches(): Promise<number> {
|
|
104
|
+
// The profile screen's two reads, under the handle it had BEFORE the edit
|
|
105
|
+
// and under the id.
|
|
106
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: OLD_USERNAME }));
|
|
107
|
+
await oxy.getProfileByUsername(OLD_USERNAME);
|
|
108
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: OLD_USERNAME }));
|
|
109
|
+
await oxy.getUserById(ACCOUNT_ID);
|
|
110
|
+
|
|
111
|
+
// The handle the account is ABOUT to be renamed to may already be warm (a
|
|
112
|
+
// 404-shaped read, a previous holder, a same-session preview). Warming it
|
|
113
|
+
// is what makes the old-handle assertion below non-vacuous: an
|
|
114
|
+
// implementation that busts only the response's handle passes for this one.
|
|
115
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: NEW_USERNAME }));
|
|
116
|
+
await oxy.getProfileByUsername(NEW_USERNAME);
|
|
117
|
+
|
|
118
|
+
// The pre-session login lookup (carries avatar + display name) and handle
|
|
119
|
+
// resolution — two keys the SDK's own profile-write sweep had drifted away
|
|
120
|
+
// from, and which no test previously covered from a write.
|
|
121
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ exists: true, username: OLD_USERNAME }));
|
|
122
|
+
await oxy.lookupUsername(OLD_USERNAME);
|
|
123
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: OLD_USERNAME }));
|
|
124
|
+
await oxy.resolveProfile(`@${OLD_USERNAME}@test.invalid`);
|
|
125
|
+
|
|
126
|
+
// The account-graph reads.
|
|
127
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ account: renamedNode }));
|
|
128
|
+
await oxy.getAccount(ACCOUNT_ID);
|
|
129
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ accounts: [renamedNode] }));
|
|
130
|
+
await oxy.listAccounts();
|
|
131
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ accounts: [renamedNode] }));
|
|
132
|
+
await oxy.listChildAccounts(PARENT_ID);
|
|
133
|
+
|
|
134
|
+
// An unrelated cached read. It must survive — the vacuity floor that tells
|
|
135
|
+
// a targeted sweep from a blanket `clearCache()`.
|
|
136
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ count: 3 }));
|
|
137
|
+
await oxy.httpService.get('/notifications/unread-count', { cache: true });
|
|
138
|
+
|
|
139
|
+
return fetchMock.mock.calls.length;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Perform the rename + picture change. */
|
|
143
|
+
async function performUpdate(): Promise<void> {
|
|
144
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ account: renamedNode }));
|
|
145
|
+
await oxy.updateAccount(ACCOUNT_ID, {
|
|
146
|
+
username: NEW_USERNAME,
|
|
147
|
+
avatar: 'file_new',
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
it('warms every read it later asserts on (control: all are cache hits before the write)', async () => {
|
|
152
|
+
const warmed = await warmCaches();
|
|
153
|
+
|
|
154
|
+
// Re-issue every read with no queued response. A cache MISS would call
|
|
155
|
+
// `fetch`, which now resolves `undefined` and throws — so a green run here
|
|
156
|
+
// is proof that each entry really is resident, and that the assertions
|
|
157
|
+
// below are measuring eviction rather than a cache that was never warm.
|
|
158
|
+
await oxy.getProfileByUsername(OLD_USERNAME);
|
|
159
|
+
await oxy.getProfileByUsername(NEW_USERNAME);
|
|
160
|
+
await oxy.getUserById(ACCOUNT_ID);
|
|
161
|
+
await oxy.lookupUsername(OLD_USERNAME);
|
|
162
|
+
await oxy.resolveProfile(`@${OLD_USERNAME}@test.invalid`);
|
|
163
|
+
await oxy.getAccount(ACCOUNT_ID);
|
|
164
|
+
await oxy.listAccounts();
|
|
165
|
+
await oxy.listChildAccounts(PARENT_ID);
|
|
166
|
+
await oxy.httpService.get('/notifications/unread-count', { cache: true });
|
|
167
|
+
|
|
168
|
+
expect(fetchMock).toHaveBeenCalledTimes(warmed);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('evicts the OLD handle, not just the handle in the write response', async () => {
|
|
172
|
+
await warmCaches();
|
|
173
|
+
await performUpdate();
|
|
174
|
+
const afterWrite = fetchMock.mock.calls.length;
|
|
175
|
+
|
|
176
|
+
// THE assertion. `updateAccount` cannot know the pre-rename handle — it is
|
|
177
|
+
// in neither the request nor the response — so only a PREFIX sweep of
|
|
178
|
+
// `GET:/profiles/username/` reaches it. A targeted `clearCacheEntry` for
|
|
179
|
+
// the response's handle leaves this entry serving the pre-rename profile.
|
|
180
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: NEW_USERNAME }));
|
|
181
|
+
const refetched = await oxy.getProfileByUsername(OLD_USERNAME);
|
|
182
|
+
|
|
183
|
+
expect(fetchMock).toHaveBeenCalledTimes(afterWrite + 1);
|
|
184
|
+
expect(refetched.username).toBe(NEW_USERNAME);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('evicts the by-id profile read the account detail page uses', async () => {
|
|
188
|
+
await warmCaches();
|
|
189
|
+
await performUpdate();
|
|
190
|
+
const afterWrite = fetchMock.mock.calls.length;
|
|
191
|
+
|
|
192
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, avatar: 'file_new' }));
|
|
193
|
+
const refetched = await oxy.getUserById(ACCOUNT_ID);
|
|
194
|
+
|
|
195
|
+
expect(fetchMock).toHaveBeenCalledTimes(afterWrite + 1);
|
|
196
|
+
expect(refetched.avatar).toBe('file_new');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('evicts the new handle, the login lookup, and handle resolution', async () => {
|
|
200
|
+
await warmCaches();
|
|
201
|
+
await performUpdate();
|
|
202
|
+
let calls = fetchMock.mock.calls.length;
|
|
203
|
+
|
|
204
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: NEW_USERNAME }));
|
|
205
|
+
await oxy.getProfileByUsername(NEW_USERNAME);
|
|
206
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
207
|
+
|
|
208
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ exists: false, username: OLD_USERNAME }));
|
|
209
|
+
await oxy.lookupUsername(OLD_USERNAME);
|
|
210
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
211
|
+
|
|
212
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, username: NEW_USERNAME }));
|
|
213
|
+
await oxy.resolveProfile(`@${OLD_USERNAME}@test.invalid`);
|
|
214
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('evicts the account detail, the account lists, and the PARENT children list', async () => {
|
|
218
|
+
await warmCaches();
|
|
219
|
+
await performUpdate();
|
|
220
|
+
let calls = fetchMock.mock.calls.length;
|
|
221
|
+
|
|
222
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ account: renamedNode }));
|
|
223
|
+
await oxy.getAccount(ACCOUNT_ID);
|
|
224
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
225
|
+
|
|
226
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ accounts: [renamedNode] }));
|
|
227
|
+
await oxy.listAccounts();
|
|
228
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
229
|
+
|
|
230
|
+
// Keyed by the PARENT id, which is reachable only from the response node —
|
|
231
|
+
// the child's own id does not build this key.
|
|
232
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ accounts: [renamedNode] }));
|
|
233
|
+
await oxy.listChildAccounts(PARENT_ID);
|
|
234
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('leaves unrelated cached reads alone (it is a sweep, not a cache wipe)', async () => {
|
|
238
|
+
await warmCaches();
|
|
239
|
+
await performUpdate();
|
|
240
|
+
const afterWrite = fetchMock.mock.calls.length;
|
|
241
|
+
|
|
242
|
+
// No queued response: a miss would call `fetch` and throw.
|
|
243
|
+
const cached = await oxy.httpService.get<{ count: number }>(
|
|
244
|
+
'/notifications/unread-count',
|
|
245
|
+
{ cache: true },
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
expect(fetchMock).toHaveBeenCalledTimes(afterWrite);
|
|
249
|
+
expect(cached.count).toBe(3);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('does not sweep when the write fails', async () => {
|
|
253
|
+
await warmCaches();
|
|
254
|
+
const warmed = fetchMock.mock.calls.length;
|
|
255
|
+
|
|
256
|
+
fetchMock.mockResolvedValueOnce(
|
|
257
|
+
new Response(JSON.stringify({ message: 'forbidden' }), {
|
|
258
|
+
status: 403,
|
|
259
|
+
headers: { 'content-type': 'application/json' },
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
262
|
+
await expect(
|
|
263
|
+
oxy.updateAccount(ACCOUNT_ID, { avatar: 'file_new' }),
|
|
264
|
+
).rejects.toThrow();
|
|
265
|
+
|
|
266
|
+
// The failed PATCH is one call; every read below must still be a cache hit.
|
|
267
|
+
await oxy.getProfileByUsername(OLD_USERNAME);
|
|
268
|
+
await oxy.getUserById(ACCOUNT_ID);
|
|
269
|
+
await oxy.getAccount(ACCOUNT_ID);
|
|
270
|
+
|
|
271
|
+
expect(fetchMock).toHaveBeenCalledTimes(warmed + 1);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('still sweeps the identity keys when the response carries no parent', async () => {
|
|
275
|
+
await warmCaches();
|
|
276
|
+
const rootNode: AccountNode = { ...renamedNode, parentAccountId: null };
|
|
277
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ account: rootNode }));
|
|
278
|
+
await oxy.updateAccount(ACCOUNT_ID, { avatar: 'file_new' });
|
|
279
|
+
const afterWrite = fetchMock.mock.calls.length;
|
|
280
|
+
|
|
281
|
+
// A root account has no children list to bust, but its identity keys go
|
|
282
|
+
// stale exactly the same way — the `parentAccountId` guard must not gate
|
|
283
|
+
// the identity sweep.
|
|
284
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: ACCOUNT_ID, avatar: 'file_new' }));
|
|
285
|
+
await oxy.getUserById(ACCOUNT_ID);
|
|
286
|
+
|
|
287
|
+
expect(fetchMock).toHaveBeenCalledTimes(afterWrite + 1);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
describe('updateProfile identity-cache invalidation (real cache)', () => {
|
|
292
|
+
let originalFetch: typeof globalThis.fetch;
|
|
293
|
+
let fetchMock: jest.Mock<Promise<Response>, [RequestInfo | URL, RequestInit?]>;
|
|
294
|
+
let oxy: OxyServices;
|
|
295
|
+
|
|
296
|
+
const SELF_ID = 'me-1';
|
|
297
|
+
|
|
298
|
+
beforeEach(() => {
|
|
299
|
+
originalFetch = globalThis.fetch;
|
|
300
|
+
fetchMock = jest.fn();
|
|
301
|
+
globalThis.fetch = fetchMock as unknown as typeof globalThis.fetch;
|
|
302
|
+
oxy = new OxyServices({
|
|
303
|
+
baseURL: 'http://test.invalid',
|
|
304
|
+
enableRetry: false,
|
|
305
|
+
requestTimeout: 1000,
|
|
306
|
+
});
|
|
307
|
+
oxy.httpService.setTokens(makeJwt({ userId: SELF_ID }));
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
afterEach(() => {
|
|
311
|
+
globalThis.fetch = originalFetch;
|
|
312
|
+
jest.clearAllMocks();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The two keys `updateProfile`'s own hand-written sweep MISSED. They are the
|
|
317
|
+
* whole point of this block — a test that only re-checked `GET:/users/me` and
|
|
318
|
+
* `GET:/profiles/username/` would have passed against the drifted version.
|
|
319
|
+
*/
|
|
320
|
+
it('evicts the login lookup and handle resolution, not just the keys it used to know', async () => {
|
|
321
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ exists: true, username: 'alice', avatar: 'old' }));
|
|
322
|
+
await oxy.lookupUsername('alice');
|
|
323
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'old' }));
|
|
324
|
+
await oxy.resolveProfile('@alice@test.invalid');
|
|
325
|
+
|
|
326
|
+
// Control: both are warm (a miss would call the un-queued mock and throw).
|
|
327
|
+
await oxy.lookupUsername('alice');
|
|
328
|
+
await oxy.resolveProfile('@alice@test.invalid');
|
|
329
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
330
|
+
|
|
331
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'new' }));
|
|
332
|
+
await oxy.updateProfile({ avatar: 'new' });
|
|
333
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
334
|
+
|
|
335
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ exists: true, username: 'alice', avatar: 'new' }));
|
|
336
|
+
await oxy.lookupUsername('alice');
|
|
337
|
+
expect(fetchMock).toHaveBeenCalledTimes(4);
|
|
338
|
+
|
|
339
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'new' }));
|
|
340
|
+
await oxy.resolveProfile('@alice@test.invalid');
|
|
341
|
+
expect(fetchMock).toHaveBeenCalledTimes(5);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('still evicts the self, by-id and handle reads it always did', async () => {
|
|
345
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'old' }));
|
|
346
|
+
await oxy.getCurrentUser();
|
|
347
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'old' }));
|
|
348
|
+
await oxy.getUserById(SELF_ID);
|
|
349
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'old' }));
|
|
350
|
+
await oxy.getProfileByUsername('alice');
|
|
351
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'old' }));
|
|
352
|
+
await oxy.getUserBySession('sess-1');
|
|
353
|
+
expect(fetchMock).toHaveBeenCalledTimes(4);
|
|
354
|
+
|
|
355
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'new' }));
|
|
356
|
+
await oxy.updateProfile({ avatar: 'new' });
|
|
357
|
+
|
|
358
|
+
let calls = 5;
|
|
359
|
+
for (const read of [
|
|
360
|
+
() => oxy.getCurrentUser(),
|
|
361
|
+
() => oxy.getUserById(SELF_ID),
|
|
362
|
+
() => oxy.getProfileByUsername('alice'),
|
|
363
|
+
() => oxy.getUserBySession('sess-1'),
|
|
364
|
+
]) {
|
|
365
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'new' }));
|
|
366
|
+
await read();
|
|
367
|
+
expect(fetchMock).toHaveBeenCalledTimes(++calls);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('evicts the account forest list and detail after a self-profile edit', async () => {
|
|
372
|
+
const selfNode: AccountNode = {
|
|
373
|
+
accountId: SELF_ID,
|
|
374
|
+
kind: 'personal',
|
|
375
|
+
parentAccountId: null,
|
|
376
|
+
account: {
|
|
377
|
+
id: SELF_ID,
|
|
378
|
+
publicKey: 'pk-me',
|
|
379
|
+
username: 'alice',
|
|
380
|
+
name: { displayName: 'Alice' },
|
|
381
|
+
avatar: 'old',
|
|
382
|
+
},
|
|
383
|
+
relationship: 'self',
|
|
384
|
+
callerMembership: null,
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
fetchMock.mockResolvedValueOnce(jsonResponse([selfNode]));
|
|
388
|
+
await oxy.listAccounts();
|
|
389
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ account: selfNode }));
|
|
390
|
+
await oxy.getAccount(SELF_ID);
|
|
391
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
392
|
+
|
|
393
|
+
fetchMock.mockResolvedValueOnce(jsonResponse({ id: SELF_ID, avatar: 'new' }));
|
|
394
|
+
await oxy.updateProfile({ avatar: 'new' });
|
|
395
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
396
|
+
|
|
397
|
+
fetchMock.mockResolvedValueOnce(jsonResponse([{ ...selfNode, account: { ...selfNode.account!, avatar: 'new' } }]));
|
|
398
|
+
await oxy.listAccounts();
|
|
399
|
+
expect(fetchMock).toHaveBeenCalledTimes(4);
|
|
400
|
+
|
|
401
|
+
fetchMock.mockResolvedValueOnce(
|
|
402
|
+
jsonResponse({ account: { ...selfNode, account: { ...selfNode.account!, avatar: 'new' } } }),
|
|
403
|
+
);
|
|
404
|
+
await oxy.getAccount(SELF_ID);
|
|
405
|
+
expect(fetchMock).toHaveBeenCalledTimes(5);
|
|
406
|
+
});
|
|
407
|
+
});
|
package/src/mixins/index.ts
CHANGED
|
@@ -30,6 +30,7 @@ import { OxyServicesAppDataMixin } from './OxyServices.appData';
|
|
|
30
30
|
import { OxyServicesCivicMixin } from './OxyServices.civic';
|
|
31
31
|
import { OxyServicesNodesMixin } from './OxyServices.nodes';
|
|
32
32
|
import { OxyServicesLinksMixin } from './OxyServices.links';
|
|
33
|
+
import { OxyServicesFollowGraphMixin } from './OxyServices.followGraph';
|
|
33
34
|
import { OxyServicesDeviceBootMixin } from './OxyServices.deviceBoot';
|
|
34
35
|
import { OxyServicesDeviceTransferMixin } from './OxyServices.deviceTransfer';
|
|
35
36
|
|
|
@@ -66,6 +67,7 @@ type AllMixinInstances =
|
|
|
66
67
|
& InstanceType<ReturnType<typeof OxyServicesCivicMixin<typeof OxyServicesBase>>>
|
|
67
68
|
& InstanceType<ReturnType<typeof OxyServicesNodesMixin<typeof OxyServicesBase>>>
|
|
68
69
|
& InstanceType<ReturnType<typeof OxyServicesLinksMixin<typeof OxyServicesBase>>>
|
|
70
|
+
& InstanceType<ReturnType<typeof OxyServicesFollowGraphMixin<typeof OxyServicesBase>>>
|
|
69
71
|
& InstanceType<ReturnType<typeof OxyServicesDeviceBootMixin<typeof OxyServicesBase>>>
|
|
70
72
|
& InstanceType<ReturnType<typeof OxyServicesDeviceTransferMixin<typeof OxyServicesBase>>>
|
|
71
73
|
& InstanceType<ReturnType<typeof OxyServicesUtilityMixin<typeof OxyServicesBase>>>;
|
|
@@ -141,6 +143,9 @@ const MIXIN_PIPELINE: MixinFunction[] = [
|
|
|
141
143
|
// Link previews / unfurls: SDK-owned link-metadata resolution via oxy-api,
|
|
142
144
|
// so apps stop scraping link metadata locally.
|
|
143
145
|
OxyServicesLinksMixin,
|
|
146
|
+
// The user-owned follow graph (#809). One relationship per user and target,
|
|
147
|
+
// shared across applications, with per-application context on top.
|
|
148
|
+
OxyServicesFollowGraphMixin,
|
|
144
149
|
|
|
145
150
|
// Device-first token mint: the client half of the zero-cookie transport
|
|
146
151
|
// (`mintFromDeviceSecret` → `POST /session/device/token`).
|
package/src/models/interfaces.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AccountKind,
|
|
3
|
-
|
|
3
|
+
AccountCategoryId,
|
|
4
4
|
UserNameResponse,
|
|
5
5
|
UserRelationship,
|
|
6
6
|
ThemePreference,
|
|
@@ -170,8 +170,16 @@ export interface User {
|
|
|
170
170
|
// Managed account fields
|
|
171
171
|
isManagedAccount?: boolean;
|
|
172
172
|
managedBy?: string;
|
|
173
|
-
/**
|
|
174
|
-
|
|
173
|
+
/**
|
|
174
|
+
* What this account is about, for any NON-personal account. ORDERED — the
|
|
175
|
+
* first element is the primary category, and nothing may reorder it.
|
|
176
|
+
*
|
|
177
|
+
* Stable ids, not labels: render each through the
|
|
178
|
+
* `accounts.accountCategory.<id>` translation key so the reader sees their own
|
|
179
|
+
* language rather than the language of whoever chose it. Absent when the
|
|
180
|
+
* account has none.
|
|
181
|
+
*/
|
|
182
|
+
accountCategories?: AccountCategoryId[];
|
|
175
183
|
/**
|
|
176
184
|
* The account's languages as full BCP-47 locales (`language-REGION`, e.g.
|
|
177
185
|
* `en-US`, `es-MX`, `pt-BR`), ordered with the PRIMARY (UI) locale first.
|
|
@@ -2,10 +2,11 @@ import { OXY_USER_INVALIDATION_CHANNEL } from '@oxyhq/contracts';
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
createOxyUserInvalidationHandler,
|
|
5
|
-
evictOxyIdentityCache,
|
|
6
5
|
publishOxyUserInvalidation,
|
|
7
|
-
type OxyIdentityCacheEvictor,
|
|
8
6
|
} from '../userInvalidation';
|
|
7
|
+
// The key enumeration this subscriber sweeps is platform-neutral and shared
|
|
8
|
+
// with the client mixins — see `utils/identityCacheSweep` and its own suite.
|
|
9
|
+
import type { OxyIdentityCacheEvictor } from '../../utils/identityCacheSweep';
|
|
9
10
|
|
|
10
11
|
function makePublisher() {
|
|
11
12
|
const calls: Array<{ channel: string; message: string }> = [];
|
|
@@ -200,21 +201,3 @@ describe('@oxyhq/core/server createOxyUserInvalidationHandler', () => {
|
|
|
200
201
|
expect(() => createOxyUserInvalidationHandler()(validMessage)).not.toThrow();
|
|
201
202
|
});
|
|
202
203
|
});
|
|
203
|
-
|
|
204
|
-
describe('@oxyhq/core/server evictOxyIdentityCache', () => {
|
|
205
|
-
it('clears the exact by-id entry and the handle-keyed prefixes', () => {
|
|
206
|
-
// The by-id key is exact. The handle-keyed ones cannot be derived from an
|
|
207
|
-
// id without the lookup being invalidated, so they are swept by prefix.
|
|
208
|
-
const { evictor, entries, prefixes } = makeEvictor();
|
|
209
|
-
evictOxyIdentityCache(evictor, 'abc123');
|
|
210
|
-
|
|
211
|
-
expect(entries).toEqual(['GET:/users/abc123']);
|
|
212
|
-
expect(prefixes).toEqual([
|
|
213
|
-
'GET:/session/user/',
|
|
214
|
-
'GET:/users/me',
|
|
215
|
-
'GET:/auth/lookup/',
|
|
216
|
-
'GET:/profiles/username/',
|
|
217
|
-
'GET:/profiles/resolve',
|
|
218
|
-
]);
|
|
219
|
-
});
|
|
220
|
-
});
|
package/src/server/index.ts
CHANGED
|
@@ -86,14 +86,17 @@ export { verifySecret } from './verifySecret';
|
|
|
86
86
|
// changes, every consuming backend sweeps its caches instead of waiting out a TTL.
|
|
87
87
|
export {
|
|
88
88
|
createOxyUserInvalidationHandler,
|
|
89
|
-
evictOxyIdentityCache,
|
|
90
89
|
publishOxyUserInvalidation,
|
|
91
90
|
} from './userInvalidation';
|
|
92
91
|
export type {
|
|
93
|
-
OxyIdentityCacheEvictor,
|
|
94
92
|
OxyInvalidationPublisher,
|
|
95
93
|
OxyUserInvalidationHandlerOptions,
|
|
96
94
|
} from './userInvalidation';
|
|
95
|
+
// The identity-key enumeration itself is platform-neutral (`src/utils/`) so the
|
|
96
|
+
// client mixins and this Node-only subscriber sweep the SAME list — a second
|
|
97
|
+
// copy is what let `updateAccount` and `updateProfile` drift apart.
|
|
98
|
+
export { evictOxyIdentityCache, oxyUserByIdCacheKey, OXY_IDENTITY_CACHE_PREFIXES } from '../utils/identityCacheSweep';
|
|
99
|
+
export type { OxyIdentityCacheEvictor } from '../utils/identityCacheSweep';
|
|
97
100
|
|
|
98
101
|
// Registrable-apex (eTLD+1) derivation via the Public Suffix List — the SINGLE
|
|
99
102
|
// SOURCE OF TRUTH shared with the IdP worker and the client FAPI auto-detect.
|