@rocapine/react-native-onboarding 1.77.0 → 1.78.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -6
- package/dist/OnboardingStudio.d.ts +14 -1
- package/dist/OnboardingStudio.d.ts.map +1 -1
- package/dist/OnboardingStudio.js +5 -1
- package/dist/OnboardingStudio.js.map +1 -1
- package/dist/OnboardingStudioClient.d.ts +29 -0
- package/dist/OnboardingStudioClient.d.ts.map +1 -1
- package/dist/OnboardingStudioClient.js +76 -19
- package/dist/OnboardingStudioClient.js.map +1 -1
- package/dist/assignmentId.d.ts +107 -0
- package/dist/assignmentId.d.ts.map +1 -0
- package/dist/assignmentId.js +215 -0
- package/dist/assignmentId.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/paywalls/customScreens.d.ts +7 -0
- package/dist/paywalls/customScreens.d.ts.map +1 -1
- package/dist/paywalls/observer.d.ts +2 -0
- package/dist/paywalls/observer.d.ts.map +1 -1
- package/dist/paywalls/observer.js +21 -9
- package/dist/paywalls/observer.js.map +1 -1
- package/dist/paywalls/types.d.ts +22 -0
- package/dist/paywalls/types.d.ts.map +1 -1
- package/dist/products/adapters/expoIap.d.ts +34 -13
- package/dist/products/adapters/expoIap.d.ts.map +1 -1
- package/dist/products/adapters/expoIap.js +136 -21
- package/dist/products/adapters/expoIap.js.map +1 -1
- package/dist/types.d.ts +47 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -1
- package/dist/userProperties/reserved.d.ts +3 -2
- package/dist/userProperties/reserved.d.ts.map +1 -1
- package/dist/userProperties/reserved.js +3 -1
- package/dist/userProperties/reserved.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/OnboardingStudio.ts +19 -1
- package/src/OnboardingStudioClient.ts +79 -31
- package/src/__tests__/assignmentId.clear.test.ts +413 -0
- package/src/__tests__/assignmentId.client.test.ts +288 -0
- package/src/__tests__/assignmentId.test.ts +195 -0
- package/src/__tests__/onboardingMetadata.client.test.ts +76 -0
- package/src/assignmentId.ts +278 -0
- package/src/index.ts +1 -0
- package/src/paywalls/__tests__/PaywallProvider.observer.test.tsx +51 -0
- package/src/paywalls/__tests__/getPaywalls.test.ts +77 -2
- package/src/paywalls/__tests__/observer.test.ts +37 -0
- package/src/paywalls/__tests__/present.test.ts +14 -0
- package/src/paywalls/customScreens.ts +7 -0
- package/src/paywalls/observer.ts +22 -2
- package/src/paywalls/types.ts +22 -0
- package/src/products/adapters/__tests__/expoIap.contract.test.ts +305 -0
- package/src/products/adapters/expoIap.ts +171 -32
- package/src/types.ts +48 -0
- package/src/userProperties/__tests__/primitives.test.ts +1 -1
- package/src/userProperties/reserved.ts +3 -1
- package/src/version.ts +1 -1
|
@@ -20,6 +20,10 @@ import {
|
|
|
20
20
|
|
|
21
21
|
import { Platform } from "react-native";
|
|
22
22
|
import { readNativeAppInfo, resolveRequestIdentity } from "./requestIdentity";
|
|
23
|
+
import { getInstallationIdStore, resolveAssignmentId } from "./assignmentId";
|
|
24
|
+
|
|
25
|
+
/** Request header carrying the `assignmentId`; the server reads it case-insensitively. */
|
|
26
|
+
const ASSIGNMENT_ID_HEADER = "X-Assignment-Id";
|
|
23
27
|
|
|
24
28
|
export class OnboardingStudioClient {
|
|
25
29
|
private baseUrl: string;
|
|
@@ -48,13 +52,67 @@ export class OnboardingStudioClient {
|
|
|
48
52
|
* when neither the host nor `expo-application` has a value, and `set` alone
|
|
49
53
|
* would then leave a same-named user param on the wire (a property persisted
|
|
50
54
|
* before these names were reserved is re-read without a reserved-key check).
|
|
55
|
+
*
|
|
56
|
+
* Returns the request headers: the `assignmentId` travels as the
|
|
57
|
+
* `X-Assignment-Id` header, never on the querystring, because edge invocation
|
|
58
|
+
* logs record the full URL and the id may be a host's user id.
|
|
51
59
|
*/
|
|
52
|
-
private writeIdentity(urlParams: URLSearchParams):
|
|
60
|
+
private async writeIdentity(urlParams: URLSearchParams): Promise<Record<string, string>> {
|
|
53
61
|
urlParams.delete("appVersion");
|
|
54
62
|
urlParams.delete("build");
|
|
55
63
|
for (const [key, value] of Object.entries(this.identityParams())) {
|
|
56
64
|
urlParams.set(key, value);
|
|
57
65
|
}
|
|
66
|
+
// A user param of that name never reaches the URL, whatever the id resolves to.
|
|
67
|
+
urlParams.delete("assignmentId");
|
|
68
|
+
const assignmentId = await this.getAssignmentId();
|
|
69
|
+
return assignmentId ? { [ASSIGNMENT_ID_HEADER]: assignmentId } : {};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The `assignmentId` the next request will carry: the host's `assignmentId`
|
|
74
|
+
* option, else the persisted installation id, else `undefined` (sent as
|
|
75
|
+
* absent). Resolved per request, never memoized here, because a host getter
|
|
76
|
+
* may change value on login. Never throws. See `assignmentId.ts`.
|
|
77
|
+
*/
|
|
78
|
+
getAssignmentId(): Promise<string | undefined> {
|
|
79
|
+
return resolveAssignmentId(this.options.assignmentId, getInstallationIdStore());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Deletes the stored installation id (`rocapine-assignment-id`) and forgets
|
|
84
|
+
* the in-memory one; a request already resolving it sends none. The next
|
|
85
|
+
* request without a host id mints a NEW id — to stop sending one at all (e.g.
|
|
86
|
+
* consent withdrawn), also make the `assignmentId` option return `false`.
|
|
87
|
+
* Process-wide, like the id: same effect as `OnboardingStudio.clearAssignmentId()`.
|
|
88
|
+
* Rejects if the removal fails or is still waiting after 2s.
|
|
89
|
+
*/
|
|
90
|
+
clearAssignmentId(): Promise<void> {
|
|
91
|
+
return getInstallationIdStore().clear();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Races `work` against `options.timeout`, rejecting with `Error("timeout")`.
|
|
96
|
+
* `work` covers identity resolution AND the fetch, so `timeout` is one
|
|
97
|
+
* end-to-end budget: a slow `assignmentId` getter cannot push
|
|
98
|
+
* `fallbackOnboarding` (or the rejection) past it. `work` gets an `expired()`
|
|
99
|
+
* check so a request whose budget ran out while the id resolved is never
|
|
100
|
+
* sent afterwards (the server would count a serve nobody receives).
|
|
101
|
+
*/
|
|
102
|
+
private withTimeout<T>(work: (expired: () => boolean) => Promise<T>): Promise<T> {
|
|
103
|
+
const ms = this.options.timeout;
|
|
104
|
+
if (!ms) return work(() => false);
|
|
105
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
106
|
+
let timedOut = false;
|
|
107
|
+
return Promise.race([
|
|
108
|
+
work(() => timedOut),
|
|
109
|
+
new Promise<never>((_, reject) => {
|
|
110
|
+
timer = setTimeout(() => {
|
|
111
|
+
timedOut = true;
|
|
112
|
+
reject(new Error("timeout"));
|
|
113
|
+
}, ms);
|
|
114
|
+
}),
|
|
115
|
+
]).finally(() => clearTimeout(timer));
|
|
58
116
|
}
|
|
59
117
|
|
|
60
118
|
constructor(projectId: string, options: OnboardingStudioClientOptions) {
|
|
@@ -117,7 +175,6 @@ export class OnboardingStudioClient {
|
|
|
117
175
|
// value would lose to a same-named user param added above.
|
|
118
176
|
urlParams.set("projectId", this.projectId);
|
|
119
177
|
urlParams.set("platform", Platform.OS);
|
|
120
|
-
this.writeIdentity(urlParams);
|
|
121
178
|
|
|
122
179
|
if (isSandbox) {
|
|
123
180
|
urlParams.set("draft", "true");
|
|
@@ -128,21 +185,17 @@ export class OnboardingStudioClient {
|
|
|
128
185
|
urlParams.set("locale", onboardingOptions.locale);
|
|
129
186
|
}
|
|
130
187
|
|
|
131
|
-
const url = `${this.baseUrl}/get-onboarding-steps?${urlParams.toString()}`;
|
|
132
|
-
console.info("OnboardingStudioClient getSteps url", url);
|
|
133
188
|
try {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
]
|
|
145
|
-
: [fetch(url)]
|
|
189
|
+
// Identity (incl. the host's assignmentId getter) resolves inside the
|
|
190
|
+
// same timeout budget as the fetch — see `withTimeout`.
|
|
191
|
+
const response = await this.withTimeout(
|
|
192
|
+
async (expired) => {
|
|
193
|
+
const headers = await this.writeIdentity(urlParams);
|
|
194
|
+
if (expired()) throw new Error("timeout");
|
|
195
|
+
const url = `${this.baseUrl}/get-onboarding-steps?${urlParams.toString()}`;
|
|
196
|
+
console.info("OnboardingStudioClient getSteps url", url);
|
|
197
|
+
return fetch(url, { headers });
|
|
198
|
+
}
|
|
146
199
|
);
|
|
147
200
|
if (!response.ok) {
|
|
148
201
|
throw new Error(
|
|
@@ -205,7 +258,6 @@ export class OnboardingStudioClient {
|
|
|
205
258
|
// SDK-owned keys use `set`, never `append` (see getSteps).
|
|
206
259
|
urlParams.set("projectId", this.projectId);
|
|
207
260
|
urlParams.set("platform", Platform.OS);
|
|
208
|
-
this.writeIdentity(urlParams);
|
|
209
261
|
|
|
210
262
|
if (isSandbox) {
|
|
211
263
|
urlParams.set("draft", "true");
|
|
@@ -228,21 +280,17 @@ export class OnboardingStudioClient {
|
|
|
228
280
|
// and `Paywall`'s declared `| null` fields. This client parses into those
|
|
229
281
|
// typed objects, so the fields must always be present (possibly `null`),
|
|
230
282
|
// not sometimes missing.
|
|
231
|
-
const url = `${this.baseUrl}/get-paywalls?${urlParams.toString()}`;
|
|
232
|
-
console.info("OnboardingStudioClient getPaywalls url", url);
|
|
233
283
|
try {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
]
|
|
245
|
-
: [fetch(url)]
|
|
284
|
+
// Identity (incl. the host's assignmentId getter) resolves inside the
|
|
285
|
+
// same timeout budget as the fetch — see `withTimeout`.
|
|
286
|
+
const response = await this.withTimeout(
|
|
287
|
+
async (expired) => {
|
|
288
|
+
const headers = await this.writeIdentity(urlParams);
|
|
289
|
+
if (expired()) throw new Error("timeout");
|
|
290
|
+
const url = `${this.baseUrl}/get-paywalls?${urlParams.toString()}`;
|
|
291
|
+
console.info("OnboardingStudioClient getPaywalls url", url);
|
|
292
|
+
return fetch(url, { headers });
|
|
293
|
+
}
|
|
246
294
|
);
|
|
247
295
|
if (!response.ok) {
|
|
248
296
|
throw new Error(
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
|
|
4
|
+
const disk = vi.hoisted(() => ({ data: {} as Record<string, string> }));
|
|
5
|
+
vi.mock("@react-native-async-storage/async-storage", () => ({
|
|
6
|
+
default: {
|
|
7
|
+
getItem: vi.fn(async (k: string) => disk.data[k] ?? null),
|
|
8
|
+
setItem: vi.fn(async (k: string, v: string) => {
|
|
9
|
+
disk.data[k] = v;
|
|
10
|
+
}),
|
|
11
|
+
removeItem: vi.fn(async (k: string) => {
|
|
12
|
+
delete disk.data[k];
|
|
13
|
+
}),
|
|
14
|
+
getAllKeys: vi.fn(async () => []),
|
|
15
|
+
multiRemove: vi.fn(async () => {}),
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
ASSIGNMENT_ID_STORAGE_KEY,
|
|
21
|
+
STORAGE_OP_TIMEOUT_MS,
|
|
22
|
+
createInstallationIdStore,
|
|
23
|
+
resolveAssignmentId,
|
|
24
|
+
__resetInstallationIdForTests,
|
|
25
|
+
type AssignmentIdStorage,
|
|
26
|
+
} from "../assignmentId";
|
|
27
|
+
import { OnboardingStudioClient } from "../OnboardingStudioClient";
|
|
28
|
+
import { createOnboardingStudio } from "../OnboardingStudio";
|
|
29
|
+
import type { UserPropertyStore } from "../userProperties/store";
|
|
30
|
+
|
|
31
|
+
const KEY = ASSIGNMENT_ID_STORAGE_KEY;
|
|
32
|
+
|
|
33
|
+
/** Storage whose every call can be held open, to interleave operations. */
|
|
34
|
+
const controllableStorage = (initial: Record<string, string> = {}) => {
|
|
35
|
+
const data: Record<string, string> = { ...initial };
|
|
36
|
+
const held: Array<() => void> = [];
|
|
37
|
+
let holding = false;
|
|
38
|
+
const maybeHold = () =>
|
|
39
|
+
holding ? new Promise<void>((resolve) => held.push(resolve)) : Promise.resolve();
|
|
40
|
+
const storage = {
|
|
41
|
+
getItem: vi.fn(async (k: string): Promise<string | null> => {
|
|
42
|
+
const v = data[k] ?? null; // read at call time, like a real store
|
|
43
|
+
await maybeHold();
|
|
44
|
+
return v;
|
|
45
|
+
}),
|
|
46
|
+
setItem: vi.fn(async (k: string, v: string) => {
|
|
47
|
+
await maybeHold();
|
|
48
|
+
data[k] = v;
|
|
49
|
+
}),
|
|
50
|
+
removeItem: vi.fn(async (k: string) => {
|
|
51
|
+
await maybeHold();
|
|
52
|
+
delete data[k];
|
|
53
|
+
}),
|
|
54
|
+
} satisfies AssignmentIdStorage;
|
|
55
|
+
return {
|
|
56
|
+
data,
|
|
57
|
+
storage,
|
|
58
|
+
hold: () => {
|
|
59
|
+
holding = true;
|
|
60
|
+
},
|
|
61
|
+
/** Stop holding and let every held call through, in order. */
|
|
62
|
+
releaseAll: async () => {
|
|
63
|
+
holding = false;
|
|
64
|
+
while (held.length) held.shift()!();
|
|
65
|
+
for (let i = 0; i < 20; i++) await Promise.resolve();
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
let n = 0;
|
|
71
|
+
const counter = () => `generated-${++n}`;
|
|
72
|
+
|
|
73
|
+
beforeEach(() => {
|
|
74
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
75
|
+
});
|
|
76
|
+
afterEach(() => {
|
|
77
|
+
vi.restoreAllMocks();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("installation id store: clear()", () => {
|
|
81
|
+
it("removes the stored id, and the next get mints and persists a different one", async () => {
|
|
82
|
+
const s = controllableStorage();
|
|
83
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
84
|
+
const first = await store.get();
|
|
85
|
+
expect(s.data[KEY]).toBe(first);
|
|
86
|
+
|
|
87
|
+
await store.clear();
|
|
88
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
89
|
+
|
|
90
|
+
const second = await store.get();
|
|
91
|
+
expect(second).toMatch(/^generated-/);
|
|
92
|
+
expect(second).not.toBe(first);
|
|
93
|
+
expect(s.data[KEY]).toBe(second);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("running during an in-flight FIRST-LAUNCH load does not resurrect the id", async () => {
|
|
97
|
+
const s = controllableStorage();
|
|
98
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
99
|
+
s.hold(); // getItem → null is held, then generate + setItem would follow
|
|
100
|
+
const inFlight = store.get();
|
|
101
|
+
const cleared = store.clear();
|
|
102
|
+
await s.releaseAll();
|
|
103
|
+
await cleared;
|
|
104
|
+
|
|
105
|
+
// The request that was in flight must not send an id that no longer exists…
|
|
106
|
+
expect(await inFlight).toBeUndefined();
|
|
107
|
+
// …and the id it generated must not be on disk afterwards.
|
|
108
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("running while the first-launch WRITE is in flight does not resurrect the id", async () => {
|
|
112
|
+
const s = controllableStorage();
|
|
113
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
114
|
+
const inFlight = store.get();
|
|
115
|
+
// Let getItem resolve, then hold the setItem that follows.
|
|
116
|
+
await Promise.resolve();
|
|
117
|
+
s.hold();
|
|
118
|
+
for (let i = 0; i < 5; i++) await Promise.resolve();
|
|
119
|
+
const cleared = store.clear();
|
|
120
|
+
await s.releaseAll();
|
|
121
|
+
await cleared;
|
|
122
|
+
|
|
123
|
+
expect(await inFlight).toBeUndefined();
|
|
124
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("a removal that the platform would finish BEFORE an earlier, slow write still wins", async () => {
|
|
128
|
+
// AsyncStorage does not promise ordering between calls, so a removeItem
|
|
129
|
+
// issued after a setItem may complete first. Here the write is slow and the
|
|
130
|
+
// removal instant: without ordering, the write lands last and the id is back.
|
|
131
|
+
const data: Record<string, string> = {};
|
|
132
|
+
let finishWrite!: () => void;
|
|
133
|
+
const storage = {
|
|
134
|
+
getItem: vi.fn(async (k: string) => data[k] ?? null),
|
|
135
|
+
setItem: vi.fn(
|
|
136
|
+
(k: string, v: string) =>
|
|
137
|
+
new Promise<void>((resolve) => {
|
|
138
|
+
finishWrite = () => {
|
|
139
|
+
data[k] = v;
|
|
140
|
+
resolve();
|
|
141
|
+
};
|
|
142
|
+
}),
|
|
143
|
+
),
|
|
144
|
+
removeItem: vi.fn(async (k: string) => {
|
|
145
|
+
delete data[k];
|
|
146
|
+
}),
|
|
147
|
+
} satisfies AssignmentIdStorage;
|
|
148
|
+
const store = createInstallationIdStore(storage, counter);
|
|
149
|
+
const inFlight = store.get();
|
|
150
|
+
for (let i = 0; i < 10; i++) await Promise.resolve();
|
|
151
|
+
expect(storage.setItem).toHaveBeenCalledTimes(1); // the write is in flight
|
|
152
|
+
const cleared = store.clear();
|
|
153
|
+
for (let i = 0; i < 10; i++) await Promise.resolve();
|
|
154
|
+
finishWrite();
|
|
155
|
+
await cleared;
|
|
156
|
+
expect(await inFlight).toBeUndefined();
|
|
157
|
+
expect(data[KEY]).toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("running during an in-flight read of an existing id: that request sends nothing, disk is empty", async () => {
|
|
161
|
+
const s = controllableStorage({ [KEY]: "existing" });
|
|
162
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
163
|
+
s.hold();
|
|
164
|
+
const inFlight = store.get();
|
|
165
|
+
const cleared = store.clear();
|
|
166
|
+
await s.releaseAll();
|
|
167
|
+
await cleared;
|
|
168
|
+
expect(await inFlight).toBeUndefined();
|
|
169
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("a get started after clear, while the old load is still writing, keeps ITS id on disk", async () => {
|
|
173
|
+
const s = controllableStorage();
|
|
174
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
175
|
+
s.hold();
|
|
176
|
+
const old = store.get();
|
|
177
|
+
const cleared = store.clear();
|
|
178
|
+
const fresh = store.get();
|
|
179
|
+
await s.releaseAll();
|
|
180
|
+
await cleared;
|
|
181
|
+
|
|
182
|
+
expect(await old).toBeUndefined();
|
|
183
|
+
const id = await fresh;
|
|
184
|
+
expect(id).toMatch(/^generated-/);
|
|
185
|
+
expect(s.data[KEY]).toBe(id);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("rejects when the removal fails, and the store still works afterwards", async () => {
|
|
189
|
+
const s = controllableStorage({ [KEY]: "existing" });
|
|
190
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
191
|
+
expect(await store.get()).toBe("existing");
|
|
192
|
+
s.storage.removeItem.mockRejectedValueOnce(new Error("disk"));
|
|
193
|
+
await expect(store.clear()).rejects.toThrow("disk");
|
|
194
|
+
// The memo was reset: the next get reads disk again.
|
|
195
|
+
expect(await store.get()).toBe("existing");
|
|
196
|
+
expect(s.storage.getItem).toHaveBeenCalledTimes(2);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("clear() against a request whose host getter is still pending", () => {
|
|
201
|
+
it("a consent getter pending across clear() does not mint, persist or send an id", async () => {
|
|
202
|
+
vi.useFakeTimers();
|
|
203
|
+
try {
|
|
204
|
+
const s = controllableStorage();
|
|
205
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
206
|
+
let consented = true;
|
|
207
|
+
// Async consent check: answers `null` ("no host id") 50ms later.
|
|
208
|
+
const getter = () => new Promise<null>((r) => setTimeout(() => r(null), 50));
|
|
209
|
+
|
|
210
|
+
const request = resolveAssignmentId(getter, store);
|
|
211
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
212
|
+
consented = false; // consent withdrawn while the request is in flight
|
|
213
|
+
await store.clear();
|
|
214
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
215
|
+
|
|
216
|
+
expect(consented).toBe(false);
|
|
217
|
+
expect(await request).toBeUndefined();
|
|
218
|
+
expect(s.storage.setItem).not.toHaveBeenCalled();
|
|
219
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
220
|
+
} finally {
|
|
221
|
+
vi.useRealTimers();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("a request that starts after clear() still mints normally", async () => {
|
|
226
|
+
const s = controllableStorage();
|
|
227
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
228
|
+
await store.clear();
|
|
229
|
+
const id = await resolveAssignmentId(async () => null, store);
|
|
230
|
+
expect(id).toMatch(/^generated-/);
|
|
231
|
+
expect(s.data[KEY]).toBe(id);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe("a storage call slower than the bound", () => {
|
|
236
|
+
/** A storage call that settles after `ms` (fake timers), running `effect` then. */
|
|
237
|
+
const slow =
|
|
238
|
+
<T>(ms: number, effect: () => T) =>
|
|
239
|
+
() =>
|
|
240
|
+
new Promise<T>((resolve) => setTimeout(() => resolve(effect()), ms));
|
|
241
|
+
|
|
242
|
+
beforeEach(() => {
|
|
243
|
+
vi.useFakeTimers();
|
|
244
|
+
});
|
|
245
|
+
afterEach(() => {
|
|
246
|
+
vi.useRealTimers();
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("getItem takes 2.5s: get() omits at 2s, and the abandoned load never mints or writes", async () => {
|
|
250
|
+
const s = controllableStorage();
|
|
251
|
+
s.storage.getItem.mockImplementationOnce(slow(2500, () => null));
|
|
252
|
+
const generate = vi.fn(counter);
|
|
253
|
+
const store = createInstallationIdStore(s.storage, generate);
|
|
254
|
+
|
|
255
|
+
const first = store.get();
|
|
256
|
+
const firstAt = settleTime(first);
|
|
257
|
+
await vi.advanceTimersByTimeAsync(STORAGE_OP_TIMEOUT_MS);
|
|
258
|
+
expect(await first).toBeUndefined();
|
|
259
|
+
expect(await firstAt).toBe(STORAGE_OP_TIMEOUT_MS);
|
|
260
|
+
|
|
261
|
+
// The read finishes at 2.5s with "no id": the abandoned load must stop there.
|
|
262
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
263
|
+
expect(generate).not.toHaveBeenCalled();
|
|
264
|
+
expect(s.storage.setItem).not.toHaveBeenCalled();
|
|
265
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
266
|
+
|
|
267
|
+
// A later request is not affected.
|
|
268
|
+
const later = store.get();
|
|
269
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
270
|
+
expect(await later).toMatch(/^generated-/);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("a getItem that never settles does not hold the queue: the next get() mints at once", async () => {
|
|
274
|
+
const s = controllableStorage();
|
|
275
|
+
s.storage.getItem.mockImplementationOnce(() => new Promise<never>(() => {}));
|
|
276
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
277
|
+
|
|
278
|
+
const first = store.get();
|
|
279
|
+
await vi.advanceTimersByTimeAsync(STORAGE_OP_TIMEOUT_MS);
|
|
280
|
+
expect(await first).toBeUndefined();
|
|
281
|
+
|
|
282
|
+
const next = store.get();
|
|
283
|
+
const nextAt = settleTime(next);
|
|
284
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
285
|
+
const id = await next;
|
|
286
|
+
expect(id).toMatch(/^generated-/);
|
|
287
|
+
expect(await nextAt).toBeLessThan(10); // did not wait behind the hung read
|
|
288
|
+
expect(s.data[KEY]).toBe(id);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("a setItem that never settles keeps holding the queue: later get() calls fail closed", async () => {
|
|
292
|
+
const s = controllableStorage();
|
|
293
|
+
s.storage.setItem.mockImplementationOnce(() => new Promise<never>(() => {}));
|
|
294
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
295
|
+
|
|
296
|
+
const first = store.get();
|
|
297
|
+
await vi.advanceTimersByTimeAsync(STORAGE_OP_TIMEOUT_MS);
|
|
298
|
+
expect(await first).toBeUndefined();
|
|
299
|
+
|
|
300
|
+
const next = store.get();
|
|
301
|
+
const nextAt = settleTime(next);
|
|
302
|
+
await vi.advanceTimersByTimeAsync(STORAGE_OP_TIMEOUT_MS);
|
|
303
|
+
expect(await next).toBeUndefined();
|
|
304
|
+
expect(await nextAt).toBe(STORAGE_OP_TIMEOUT_MS); // waited its own bound
|
|
305
|
+
expect(s.storage.getItem).toHaveBeenCalledTimes(1); // never ran: queue held
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("setItem takes 2.5s: a clear() issued meanwhile removes the key AFTER the write lands", async () => {
|
|
309
|
+
const s = controllableStorage();
|
|
310
|
+
s.storage.setItem.mockImplementationOnce((k: string, v: string) =>
|
|
311
|
+
slow(2500, () => {
|
|
312
|
+
s.data[k] = v;
|
|
313
|
+
})(),
|
|
314
|
+
);
|
|
315
|
+
const store = createInstallationIdStore(s.storage, counter);
|
|
316
|
+
|
|
317
|
+
const request = store.get();
|
|
318
|
+
await vi.advanceTimersByTimeAsync(STORAGE_OP_TIMEOUT_MS);
|
|
319
|
+
expect(await request).toBeUndefined(); // the caller stopped waiting at 2s
|
|
320
|
+
|
|
321
|
+
// The abandoned write is still in flight. The removal must queue behind it.
|
|
322
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
323
|
+
const cleared = store.clear().catch(() => {});
|
|
324
|
+
await vi.advanceTimersByTimeAsync(1000); // the write lands at 2.5s
|
|
325
|
+
await cleared;
|
|
326
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("removeItem takes 5s: clear() rejects at 2s, a get() meanwhile fails closed, then things proceed", async () => {
|
|
330
|
+
const s = controllableStorage({ [KEY]: "existing" });
|
|
331
|
+
s.storage.removeItem.mockImplementationOnce((k: string) =>
|
|
332
|
+
slow(5000, () => {
|
|
333
|
+
delete s.data[k];
|
|
334
|
+
})(),
|
|
335
|
+
);
|
|
336
|
+
const generate = vi.fn(counter);
|
|
337
|
+
const store = createInstallationIdStore(s.storage, generate);
|
|
338
|
+
|
|
339
|
+
const outcome = store.clear().then(
|
|
340
|
+
() => "resolved",
|
|
341
|
+
(e: Error) => e.message,
|
|
342
|
+
);
|
|
343
|
+
const during = store.get(); // queued behind the hung removal
|
|
344
|
+
await vi.advanceTimersByTimeAsync(STORAGE_OP_TIMEOUT_MS);
|
|
345
|
+
expect(await outcome).toMatch(/timed out/);
|
|
346
|
+
// It did not skip ahead of the removal: it waited its own 2s and omitted.
|
|
347
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
348
|
+
expect(await during).toBeUndefined();
|
|
349
|
+
expect(s.data[KEY]).toBe("existing"); // removal still in flight
|
|
350
|
+
|
|
351
|
+
// The removal settles at 5s; the abandoned get never mints or writes.
|
|
352
|
+
await vi.advanceTimersByTimeAsync(3500);
|
|
353
|
+
expect(s.data[KEY]).toBeUndefined();
|
|
354
|
+
expect(generate).not.toHaveBeenCalled();
|
|
355
|
+
|
|
356
|
+
const after = store.get();
|
|
357
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
358
|
+
expect(await after).toMatch(/^generated-/);
|
|
359
|
+
expect(s.data[KEY]).toBe(await after);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
/** Fake-clock time (ms) at which `p` settles, measured from now. */
|
|
364
|
+
const settleTime = (p: Promise<unknown>) => {
|
|
365
|
+
const start = Date.now();
|
|
366
|
+
return p.then(
|
|
367
|
+
() => Date.now() - start,
|
|
368
|
+
() => Date.now() - start,
|
|
369
|
+
);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
describe("clearAssignmentId() on the client and the facade", () => {
|
|
373
|
+
const fakeResponse = {
|
|
374
|
+
ok: true,
|
|
375
|
+
status: 200,
|
|
376
|
+
statusText: "OK",
|
|
377
|
+
json: async () => ({ metadata: {}, steps: [], paywalls: {} }),
|
|
378
|
+
headers: { get: () => null },
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
beforeEach(() => {
|
|
382
|
+
disk.data = {};
|
|
383
|
+
__resetInstallationIdForTests();
|
|
384
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
385
|
+
});
|
|
386
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
387
|
+
|
|
388
|
+
it("client.clearAssignmentId() removes the stored id; the next request sends a new one", async () => {
|
|
389
|
+
const fetchMock = vi.fn().mockResolvedValue(fakeResponse);
|
|
390
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
391
|
+
const client = new OnboardingStudioClient("proj-1", {});
|
|
392
|
+
const sent = (i: number) =>
|
|
393
|
+
new Headers((fetchMock.mock.calls[i][1] as RequestInit | undefined)?.headers).get("X-Assignment-Id");
|
|
394
|
+
|
|
395
|
+
await client.getSteps();
|
|
396
|
+
const first = sent(0);
|
|
397
|
+
expect(disk.data[KEY]).toBe(first);
|
|
398
|
+
|
|
399
|
+
await client.clearAssignmentId();
|
|
400
|
+
expect(disk.data[KEY]).toBeUndefined();
|
|
401
|
+
|
|
402
|
+
await client.getPaywalls();
|
|
403
|
+
expect(sent(1)).toBeTruthy();
|
|
404
|
+
expect(sent(1)).not.toBe(first);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("OnboardingStudio.clearAssignmentId() works before init and clears the process-wide id", async () => {
|
|
408
|
+
disk.data[KEY] = "existing";
|
|
409
|
+
const studio = createOnboardingStudio({} as UserPropertyStore);
|
|
410
|
+
await studio.clearAssignmentId();
|
|
411
|
+
expect(disk.data[KEY]).toBeUndefined();
|
|
412
|
+
});
|
|
413
|
+
});
|