@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
|
@@ -0,0 +1,288 @@
|
|
|
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 () => {}),
|
|
12
|
+
getAllKeys: vi.fn(async () => []),
|
|
13
|
+
multiRemove: vi.fn(async () => {}),
|
|
14
|
+
},
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
import { OnboardingStudioClient } from "../OnboardingStudioClient";
|
|
18
|
+
import { ASSIGNMENT_ID_STORAGE_KEY, __resetInstallationIdForTests } from "../assignmentId";
|
|
19
|
+
import { isReservedUserPropertyKey } from "../userProperties/reserved";
|
|
20
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
21
|
+
|
|
22
|
+
const fakeResponse = (body: unknown) => ({
|
|
23
|
+
ok: true,
|
|
24
|
+
status: 200,
|
|
25
|
+
statusText: "OK",
|
|
26
|
+
json: async () => body,
|
|
27
|
+
headers: { get: () => null },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// The id travels as the `X-Assignment-Id` header, never on the URL: edge
|
|
31
|
+
// invocation logs record the full URL, and the id may be a host's user id.
|
|
32
|
+
const idHeader = (fetchMock: ReturnType<typeof vi.fn>, i = 0) =>
|
|
33
|
+
new Headers((fetchMock.mock.calls[i][1] as RequestInit | undefined)?.headers).get("X-Assignment-Id");
|
|
34
|
+
|
|
35
|
+
const fetches = {
|
|
36
|
+
getSteps: (c: OnboardingStudioClient, params?: Record<string, string>) =>
|
|
37
|
+
c.getSteps({ locale: "en" }, params),
|
|
38
|
+
getPaywalls: (c: OnboardingStudioClient, params?: Record<string, string>) =>
|
|
39
|
+
c.getPaywalls({ locale: "en" }, params),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe.each(Object.entries(fetches))("%s assignmentId", (_name, fetchWith) => {
|
|
43
|
+
let fetchMock: ReturnType<typeof vi.fn>;
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.clearAllMocks(); // the AsyncStorage module mock keeps call history across tests
|
|
47
|
+
disk.data = {};
|
|
48
|
+
__resetInstallationIdForTests();
|
|
49
|
+
fetchMock = vi.fn().mockResolvedValue(fakeResponse({ metadata: {}, steps: [], paywalls: {} }));
|
|
50
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
51
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
afterEach(() => {
|
|
55
|
+
vi.unstubAllGlobals();
|
|
56
|
+
vi.restoreAllMocks();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const sent = (i = 0) => new URL(fetchMock.mock.calls[i][0] as string).searchParams;
|
|
60
|
+
const sentId = (i = 0) => idHeader(fetchMock, i);
|
|
61
|
+
|
|
62
|
+
it("sends a persisted installation id, the same one across launches", async () => {
|
|
63
|
+
await fetchWith(new OnboardingStudioClient("proj-1", {}));
|
|
64
|
+
const first = sentId();
|
|
65
|
+
expect(first).toBeTruthy();
|
|
66
|
+
expect(disk.data[ASSIGNMENT_ID_STORAGE_KEY]).toBe(first);
|
|
67
|
+
|
|
68
|
+
// Next launch: module memo gone, disk kept.
|
|
69
|
+
__resetInstallationIdForTests();
|
|
70
|
+
await fetchWith(new OnboardingStudioClient("proj-1", {}));
|
|
71
|
+
expect(sentId(1)).toBe(first);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("sends it in sandbox (draft) requests too", async () => {
|
|
75
|
+
await fetchWith(new OnboardingStudioClient("proj-1", { isSandbox: true }));
|
|
76
|
+
expect(sent().get("draft")).toBe("true");
|
|
77
|
+
expect(sentId()).toBeTruthy();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("a host override wins and is read on every request", async () => {
|
|
81
|
+
let id = "anon-1";
|
|
82
|
+
const client = new OnboardingStudioClient("proj-1", { assignmentId: () => id });
|
|
83
|
+
await fetchWith(client);
|
|
84
|
+
id = "user-42";
|
|
85
|
+
await fetchWith(client);
|
|
86
|
+
expect(sentId(0)).toBe("anon-1");
|
|
87
|
+
expect(sentId(1)).toBe("user-42");
|
|
88
|
+
expect(disk.data[ASSIGNMENT_ID_STORAGE_KEY]).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("assignmentId: false sends nothing and never touches storage", async () => {
|
|
92
|
+
const client = new OnboardingStudioClient("proj-1", { assignmentId: false });
|
|
93
|
+
await fetchWith(client);
|
|
94
|
+
expect(sentId()).toBeNull();
|
|
95
|
+
expect(sent().has("assignmentId")).toBe(false);
|
|
96
|
+
expect(AsyncStorage.getItem).not.toHaveBeenCalledWith(ASSIGNMENT_ID_STORAGE_KEY);
|
|
97
|
+
expect(AsyncStorage.setItem).not.toHaveBeenCalledWith(ASSIGNMENT_ID_STORAGE_KEY, expect.anything());
|
|
98
|
+
expect(disk.data[ASSIGNMENT_ID_STORAGE_KEY]).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("opting out drops a user param named assignmentId too", async () => {
|
|
102
|
+
await fetchWith(new OnboardingStudioClient("proj-1", { assignmentId: () => false }), {
|
|
103
|
+
assignmentId: "spoofed",
|
|
104
|
+
});
|
|
105
|
+
expect(sentId()).toBeNull();
|
|
106
|
+
expect(sent().has("assignmentId")).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("is absent when nothing stable is available (storage unreadable)", async () => {
|
|
110
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
111
|
+
vi.mocked(AsyncStorage.getItem).mockRejectedValueOnce(new Error("disk"));
|
|
112
|
+
await fetchWith(new OnboardingStudioClient("proj-1", {}));
|
|
113
|
+
expect(sentId()).toBeNull();
|
|
114
|
+
expect(sent().has("assignmentId")).toBe(false);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("sends the id as the X-Assignment-Id header and never on the URL", async () => {
|
|
118
|
+
await fetchWith(new OnboardingStudioClient("proj-1", { assignmentId: "user-42" }));
|
|
119
|
+
expect(sentId()).toBe("user-42");
|
|
120
|
+
const url = fetchMock.mock.calls[0][0] as string;
|
|
121
|
+
expect(url).not.toMatch(/assignmentId/i);
|
|
122
|
+
expect(url).not.toContain("user-42");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("a getter that throws fails closed: no header, nothing on the URL", async () => {
|
|
126
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
127
|
+
await fetchWith(
|
|
128
|
+
new OnboardingStudioClient("proj-1", {
|
|
129
|
+
assignmentId: () => {
|
|
130
|
+
throw new Error("boom");
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
);
|
|
134
|
+
expect(sentId()).toBeNull();
|
|
135
|
+
expect(sent().has("assignmentId")).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("a user param named assignmentId never reaches the wire as the id", async () => {
|
|
139
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
140
|
+
vi.mocked(AsyncStorage.getItem).mockRejectedValueOnce(new Error("disk"));
|
|
141
|
+
await fetchWith(new OnboardingStudioClient("proj-1", {}), { assignmentId: "spoofed" });
|
|
142
|
+
expect(sent().getAll("assignmentId")).toEqual([]);
|
|
143
|
+
expect(sentId()).toBeNull();
|
|
144
|
+
|
|
145
|
+
await fetchWith(new OnboardingStudioClient("proj-1", { assignmentId: "host" }), {
|
|
146
|
+
assignmentId: "spoofed",
|
|
147
|
+
});
|
|
148
|
+
expect(sent(1).getAll("assignmentId")).toEqual([]);
|
|
149
|
+
expect(sentId(1)).toBe("host");
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("getSteps and getPaywalls fired together on a first launch send the same id", async () => {
|
|
154
|
+
disk.data = {};
|
|
155
|
+
__resetInstallationIdForTests();
|
|
156
|
+
const fetchMock = vi.fn().mockResolvedValue(fakeResponse({ metadata: {}, steps: [], paywalls: {} }));
|
|
157
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
158
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
159
|
+
const client = new OnboardingStudioClient("proj-1", {});
|
|
160
|
+
await Promise.all([fetches.getSteps(client), fetches.getPaywalls(client)]);
|
|
161
|
+
const ids = [idHeader(fetchMock, 0), idHeader(fetchMock, 1)];
|
|
162
|
+
expect(ids[0]).toBeTruthy();
|
|
163
|
+
expect(ids[1]).toBe(ids[0]);
|
|
164
|
+
vi.unstubAllGlobals();
|
|
165
|
+
vi.restoreAllMocks();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("assignmentId is a reserved user property key", () => {
|
|
169
|
+
expect(isReservedUserPropertyKey("assignmentId")).toBe(true);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe.each(Object.entries(fetches))("%s: fail closed and one timeout budget", (name, fetchWith) => {
|
|
173
|
+
const fallbackOnboarding = { metadata: { id: "fb" }, steps: [], configuration: {} } as never;
|
|
174
|
+
let fetchMock: ReturnType<typeof vi.fn>;
|
|
175
|
+
|
|
176
|
+
beforeEach(() => {
|
|
177
|
+
vi.clearAllMocks();
|
|
178
|
+
vi.useFakeTimers();
|
|
179
|
+
disk.data = {};
|
|
180
|
+
__resetInstallationIdForTests();
|
|
181
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
182
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
183
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
afterEach(() => {
|
|
187
|
+
vi.useRealTimers();
|
|
188
|
+
vi.unstubAllGlobals();
|
|
189
|
+
vi.restoreAllMocks();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
/** fetch that answers after `ms`. */
|
|
193
|
+
const slowFetch = (ms: number) => {
|
|
194
|
+
fetchMock = vi.fn(
|
|
195
|
+
() => new Promise((r) => setTimeout(() => r(fakeResponse({ metadata: {}, steps: [], paywalls: {} })), ms)),
|
|
196
|
+
);
|
|
197
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/** Settles the request, recording when (in fake ms) it did and how. */
|
|
201
|
+
const settle = async (p: Promise<unknown>, budget: number) => {
|
|
202
|
+
const out: { at?: number; value?: unknown; error?: unknown } = {};
|
|
203
|
+
const start = Date.now();
|
|
204
|
+
p.then(
|
|
205
|
+
(value) => Object.assign(out, { at: Date.now() - start, value }),
|
|
206
|
+
(error) => Object.assign(out, { at: Date.now() - start, error }),
|
|
207
|
+
);
|
|
208
|
+
await vi.advanceTimersByTimeAsync(budget);
|
|
209
|
+
return out;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
it("a getter that resolves false after 2.5s: nothing sent, nothing stored", async () => {
|
|
213
|
+
slowFetch(0);
|
|
214
|
+
const client = new OnboardingStudioClient("proj-1", {
|
|
215
|
+
assignmentId: () => new Promise<false>((r) => setTimeout(() => r(false), 2500)),
|
|
216
|
+
});
|
|
217
|
+
const out = await settle(fetchWith(client), 3000);
|
|
218
|
+
expect(out.error).toBeUndefined();
|
|
219
|
+
expect(idHeader(fetchMock)).toBeNull();
|
|
220
|
+
expect(new URL(fetchMock.mock.calls[0][0] as string).searchParams.has("assignmentId")).toBe(false);
|
|
221
|
+
expect(AsyncStorage.getItem).not.toHaveBeenCalledWith(ASSIGNMENT_ID_STORAGE_KEY);
|
|
222
|
+
expect(AsyncStorage.setItem).not.toHaveBeenCalledWith(ASSIGNMENT_ID_STORAGE_KEY, expect.anything());
|
|
223
|
+
expect(disk.data[ASSIGNMENT_ID_STORAGE_KEY]).toBeUndefined();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("a getter that resolves an id after 2.5s fails closed: no header, nothing on the URL", async () => {
|
|
227
|
+
slowFetch(0);
|
|
228
|
+
const client = new OnboardingStudioClient("proj-1", {
|
|
229
|
+
assignmentId: () => new Promise<string>((r) => setTimeout(() => r("late-user"), 2500)),
|
|
230
|
+
});
|
|
231
|
+
const out = await settle(fetchWith(client), 3000);
|
|
232
|
+
expect(out.error).toBeUndefined();
|
|
233
|
+
expect(idHeader(fetchMock)).toBeNull();
|
|
234
|
+
expect(fetchMock.mock.calls[0][0] as string).not.toMatch(/assignmentId|late-user/);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("options.timeout covers id resolution AND fetch: 800ms + 500ms against 1000ms times out at 1000ms", async () => {
|
|
238
|
+
slowFetch(500);
|
|
239
|
+
const client = new OnboardingStudioClient("proj-1", {
|
|
240
|
+
timeout: 1000,
|
|
241
|
+
fallbackOnboarding,
|
|
242
|
+
assignmentId: () => new Promise<string>((r) => setTimeout(() => r("user-42"), 800)),
|
|
243
|
+
});
|
|
244
|
+
const out = await settle(fetchWith(client), 3000);
|
|
245
|
+
expect(out.at).toBe(1000);
|
|
246
|
+
if (name === "getSteps") {
|
|
247
|
+
expect((out.value as { headers: Record<string, string> }).headers["ONBS-Onboarding-Id"]).toBe("fallback");
|
|
248
|
+
} else {
|
|
249
|
+
expect(String(out.error)).toMatch(/timeout/);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("a hanging getter does not delay fallbackOnboarding past options.timeout", async () => {
|
|
254
|
+
slowFetch(0);
|
|
255
|
+
const client = new OnboardingStudioClient("proj-1", {
|
|
256
|
+
timeout: 500,
|
|
257
|
+
fallbackOnboarding,
|
|
258
|
+
assignmentId: () => new Promise<string>(() => {}),
|
|
259
|
+
});
|
|
260
|
+
const out = await settle(fetchWith(client), 3000);
|
|
261
|
+
expect(out.at).toBe(500);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("a request that timed out while resolving the id is never sent afterwards", async () => {
|
|
265
|
+
slowFetch(0);
|
|
266
|
+
const client = new OnboardingStudioClient("proj-1", {
|
|
267
|
+
timeout: 500,
|
|
268
|
+
fallbackOnboarding,
|
|
269
|
+
assignmentId: () => new Promise<string>((r) => setTimeout(() => r("user-42"), 800)),
|
|
270
|
+
});
|
|
271
|
+
const out = await settle(fetchWith(client), 3000);
|
|
272
|
+
expect(out.at).toBe(500);
|
|
273
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("within budget the request succeeds: 300ms + 300ms against 1000ms", async () => {
|
|
277
|
+
slowFetch(300);
|
|
278
|
+
const client = new OnboardingStudioClient("proj-1", {
|
|
279
|
+
timeout: 1000,
|
|
280
|
+
fallbackOnboarding,
|
|
281
|
+
assignmentId: () => new Promise<string>((r) => setTimeout(() => r("user-42"), 300)),
|
|
282
|
+
});
|
|
283
|
+
const out = await settle(fetchWith(client), 3000);
|
|
284
|
+
expect(out.error).toBeUndefined();
|
|
285
|
+
expect(out.at).toBe(600);
|
|
286
|
+
expect(idHeader(fetchMock)).toBe("user-42");
|
|
287
|
+
});
|
|
288
|
+
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
ASSIGNMENT_ID_STORAGE_KEY,
|
|
4
|
+
createInstallationIdStore,
|
|
5
|
+
resolveAssignmentId,
|
|
6
|
+
type AssignmentIdStorage,
|
|
7
|
+
} from "../assignmentId";
|
|
8
|
+
import { ONBOARDING_CACHE_KEY_PREFIX, PAYWALLS_CACHE_KEY_PREFIX } from "../infra/queries/cacheKey";
|
|
9
|
+
import { USER_PROPERTIES_STORAGE_KEY } from "../userProperties/store";
|
|
10
|
+
|
|
11
|
+
const memoryStorage = (initial: Record<string, string> = {}) => {
|
|
12
|
+
const data = { ...initial };
|
|
13
|
+
const storage = {
|
|
14
|
+
getItem: vi.fn(async (k: string) => data[k] ?? null),
|
|
15
|
+
setItem: vi.fn(async (k: string, v: string) => {
|
|
16
|
+
data[k] = v;
|
|
17
|
+
}),
|
|
18
|
+
removeItem: vi.fn(async (k: string) => {
|
|
19
|
+
delete data[k];
|
|
20
|
+
}),
|
|
21
|
+
} satisfies AssignmentIdStorage;
|
|
22
|
+
return { data, storage };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let n = 0;
|
|
26
|
+
const counter = () => `generated-${++n}`;
|
|
27
|
+
|
|
28
|
+
describe("installation id store", () => {
|
|
29
|
+
it("generates once, persists it, and reads it back on the next launch", async () => {
|
|
30
|
+
const { data, storage } = memoryStorage();
|
|
31
|
+
const first = await createInstallationIdStore(storage, counter).get();
|
|
32
|
+
expect(first).toMatch(/^generated-/);
|
|
33
|
+
expect(data[ASSIGNMENT_ID_STORAGE_KEY]).toBe(first);
|
|
34
|
+
|
|
35
|
+
// A new process: a fresh store over the same disk.
|
|
36
|
+
const generate = vi.fn(counter);
|
|
37
|
+
const second = await createInstallationIdStore(storage, generate).get();
|
|
38
|
+
expect(second).toBe(first);
|
|
39
|
+
expect(generate).not.toHaveBeenCalled();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("concurrent first calls share one id and write it once", async () => {
|
|
43
|
+
const { storage } = memoryStorage();
|
|
44
|
+
const store = createInstallationIdStore(storage, counter);
|
|
45
|
+
const [a, b] = await Promise.all([store.get(), store.get()]);
|
|
46
|
+
expect(a).toBe(b);
|
|
47
|
+
expect(storage.setItem).toHaveBeenCalledTimes(1);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("a failed read is absent, and never overwrites what may be on disk", async () => {
|
|
51
|
+
const { storage } = memoryStorage({ [ASSIGNMENT_ID_STORAGE_KEY]: "kept" });
|
|
52
|
+
storage.getItem.mockRejectedValueOnce(new Error("disk"));
|
|
53
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
54
|
+
const store = createInstallationIdStore(storage, counter);
|
|
55
|
+
expect(await store.get()).toBeUndefined();
|
|
56
|
+
expect(storage.setItem).not.toHaveBeenCalled();
|
|
57
|
+
// Not memoized: the next request retries and finds the stored id.
|
|
58
|
+
expect(await store.get()).toBe("kept");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("an id that could not be persisted is absent rather than sent", async () => {
|
|
62
|
+
const { storage } = memoryStorage();
|
|
63
|
+
storage.setItem.mockRejectedValueOnce(new Error("disk full"));
|
|
64
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
65
|
+
const store = createInstallationIdStore(storage, counter);
|
|
66
|
+
expect(await store.get()).toBeUndefined();
|
|
67
|
+
const retried = await store.get();
|
|
68
|
+
expect(retried).toMatch(/^generated-/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("a blank stored value is replaced", async () => {
|
|
72
|
+
const { storage } = memoryStorage({ [ASSIGNMENT_ID_STORAGE_KEY]: " " });
|
|
73
|
+
expect(await createInstallationIdStore(storage, counter).get()).toMatch(/^generated-/);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("the default generator yields distinct uuid-shaped ids", async () => {
|
|
77
|
+
const a = await createInstallationIdStore(memoryStorage().storage).get();
|
|
78
|
+
const b = await createInstallationIdStore(memoryStorage().storage).get();
|
|
79
|
+
expect(a).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
|
|
80
|
+
expect(a).not.toBe(b);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("the default generator works without crypto.randomUUID (Hermes)", async () => {
|
|
84
|
+
vi.stubGlobal("crypto", undefined);
|
|
85
|
+
try {
|
|
86
|
+
const id = await createInstallationIdStore(memoryStorage().storage).get();
|
|
87
|
+
expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
|
|
88
|
+
} finally {
|
|
89
|
+
vi.unstubAllGlobals();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("the key survives clearCache() and OnboardingStudio.reset()", () => {
|
|
94
|
+
expect(ASSIGNMENT_ID_STORAGE_KEY.startsWith(ONBOARDING_CACHE_KEY_PREFIX)).toBe(false);
|
|
95
|
+
expect(ASSIGNMENT_ID_STORAGE_KEY.startsWith(PAYWALLS_CACHE_KEY_PREFIX)).toBe(false);
|
|
96
|
+
expect(ASSIGNMENT_ID_STORAGE_KEY).not.toBe(USER_PROPERTIES_STORAGE_KEY);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("resolveAssignmentId", () => {
|
|
101
|
+
const installation = (id: string | undefined) => ({ token: () => 0, get: vi.fn(async () => id) });
|
|
102
|
+
|
|
103
|
+
it("no override: the installation id", async () => {
|
|
104
|
+
expect(await resolveAssignmentId(undefined, installation("inst"))).toBe("inst");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("no override and no installation id: absent", async () => {
|
|
108
|
+
expect(await resolveAssignmentId(undefined, installation(undefined))).toBeUndefined();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("a host string wins, trimmed, and the installation id is never generated", async () => {
|
|
112
|
+
const inst = installation("inst");
|
|
113
|
+
expect(await resolveAssignmentId(" user-42 ", inst)).toBe("user-42");
|
|
114
|
+
expect(inst.get).not.toHaveBeenCalled();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("a host getter is called on every resolution (sync or async)", async () => {
|
|
118
|
+
let current = "$RCAnonymousID:abc";
|
|
119
|
+
const getter = vi.fn(async () => current);
|
|
120
|
+
expect(await resolveAssignmentId(getter, installation("inst"))).toBe("$RCAnonymousID:abc");
|
|
121
|
+
current = "user-42";
|
|
122
|
+
expect(await resolveAssignmentId(getter, installation("inst"))).toBe("user-42");
|
|
123
|
+
expect(await resolveAssignmentId(() => "sync-id", installation("inst"))).toBe("sync-id");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it.each([
|
|
127
|
+
["empty string", ""],
|
|
128
|
+
["blank", " "],
|
|
129
|
+
["null", null],
|
|
130
|
+
["undefined", undefined],
|
|
131
|
+
])("a host value that is %s falls through to the installation id", async (_l, value) => {
|
|
132
|
+
expect(await resolveAssignmentId(value as string | undefined, installation("inst"))).toBe("inst");
|
|
133
|
+
expect(await resolveAssignmentId(() => value, installation("inst"))).toBe("inst");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("false opts out: absent, and the installation id is never read, created or written", async () => {
|
|
137
|
+
const inst = installation("inst");
|
|
138
|
+
expect(await resolveAssignmentId(false, inst)).toBeUndefined();
|
|
139
|
+
expect(await resolveAssignmentId(() => false, inst)).toBeUndefined();
|
|
140
|
+
expect(await resolveAssignmentId(async () => false as const, inst)).toBeUndefined();
|
|
141
|
+
expect(inst.get).not.toHaveBeenCalled();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("a getter can opt out per request (e.g. until consent) and opt back in", async () => {
|
|
145
|
+
let consented = false;
|
|
146
|
+
const inst = installation("inst");
|
|
147
|
+
const getter = () => (consented ? undefined : false);
|
|
148
|
+
expect(await resolveAssignmentId(getter, inst)).toBeUndefined();
|
|
149
|
+
expect(inst.get).not.toHaveBeenCalled();
|
|
150
|
+
consented = true;
|
|
151
|
+
expect(await resolveAssignmentId(getter, inst)).toBe("inst");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Fail closed: a getter that cannot answer may be one that would have said
|
|
155
|
+
// `false` (no consent yet), so it must never fall back to the stored id.
|
|
156
|
+
it("a throwing or rejecting getter omits the id and never touches the store", async () => {
|
|
157
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
158
|
+
const inst = installation("inst");
|
|
159
|
+
const boom = () => {
|
|
160
|
+
throw new Error("Purchases not configured");
|
|
161
|
+
};
|
|
162
|
+
expect(await resolveAssignmentId(boom, inst)).toBeUndefined();
|
|
163
|
+
expect(await resolveAssignmentId(async () => boom(), inst)).toBeUndefined();
|
|
164
|
+
expect(inst.get).not.toHaveBeenCalled();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("a getter that never settles is abandoned after the bound: omitted, store untouched", async () => {
|
|
168
|
+
vi.useFakeTimers();
|
|
169
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
170
|
+
try {
|
|
171
|
+
const inst = installation("inst");
|
|
172
|
+
const pending = resolveAssignmentId(() => new Promise<string>(() => {}), inst);
|
|
173
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
174
|
+
expect(await pending).toBeUndefined();
|
|
175
|
+
expect(inst.get).not.toHaveBeenCalled();
|
|
176
|
+
} finally {
|
|
177
|
+
vi.useRealTimers();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("a getter that settles false after the bound (2.5s) still sends nothing and stores nothing", async () => {
|
|
182
|
+
vi.useFakeTimers();
|
|
183
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
184
|
+
try {
|
|
185
|
+
const inst = installation("inst");
|
|
186
|
+
const late = () => new Promise<false>((r) => setTimeout(() => r(false), 2500));
|
|
187
|
+
const pending = resolveAssignmentId(late, inst);
|
|
188
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
189
|
+
expect(await pending).toBeUndefined();
|
|
190
|
+
expect(inst.get).not.toHaveBeenCalled();
|
|
191
|
+
} finally {
|
|
192
|
+
vi.useRealTimers();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, it, expect, expectTypeOf, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
|
|
4
|
+
vi.mock("@react-native-async-storage/async-storage", () => ({
|
|
5
|
+
default: {
|
|
6
|
+
getItem: vi.fn(async () => null),
|
|
7
|
+
setItem: vi.fn(async () => {}),
|
|
8
|
+
removeItem: vi.fn(async () => {}),
|
|
9
|
+
getAllKeys: vi.fn(async () => []),
|
|
10
|
+
multiRemove: vi.fn(async () => {}),
|
|
11
|
+
},
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
import { OnboardingStudioClient } from "../OnboardingStudioClient";
|
|
15
|
+
import { MAIN_VARIANT_KEY, type OnboardingMetadata } from "../types";
|
|
16
|
+
|
|
17
|
+
const fakeResponse = (body: unknown) => ({
|
|
18
|
+
ok: true,
|
|
19
|
+
status: 200,
|
|
20
|
+
statusText: "OK",
|
|
21
|
+
json: async () => body,
|
|
22
|
+
headers: { get: () => null },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const getMetadata = async (metadata: Record<string, unknown>) => {
|
|
26
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(fakeResponse({ metadata, steps: [] })));
|
|
27
|
+
const { data } = await new OnboardingStudioClient("proj-1", {}).getSteps({ locale: "en" });
|
|
28
|
+
return data.metadata;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe("OnboardingMetadata variant and deployment identity", () => {
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
vi.unstubAllGlobals();
|
|
38
|
+
vi.restoreAllMocks();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("types variantKey and deploymentId as optional and nullable, with no experimentId", () => {
|
|
42
|
+
expectTypeOf<OnboardingMetadata["variantKey"]>().toEqualTypeOf<string | null | undefined>();
|
|
43
|
+
expectTypeOf<OnboardingMetadata["deploymentId"]>().toEqualTypeOf<number | null | undefined>();
|
|
44
|
+
expectTypeOf<OnboardingMetadata>().not.toHaveProperty("experimentId");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("surfaces variantKey and deploymentId from the payload through getSteps", async () => {
|
|
48
|
+
const metadata = await getMetadata({ id: "onb-1", variantKey: "b", deploymentId: 42 });
|
|
49
|
+
expect(metadata.variantKey).toBe("b");
|
|
50
|
+
expect(metadata.deploymentId).toBe(42);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("exports the reserved main variant key as a literal from the package index", () => {
|
|
54
|
+
expect(MAIN_VARIANT_KEY).toBe("main");
|
|
55
|
+
expectTypeOf(MAIN_VARIANT_KEY).toEqualTypeOf<"main">();
|
|
56
|
+
// Type-only: proves the public entry point re-exports it without loading it.
|
|
57
|
+
expectTypeOf<(typeof import("../index"))["MAIN_VARIANT_KEY"]>().toEqualTypeOf<"main">();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("passes the reserved main variantKey through unchanged", async () => {
|
|
61
|
+
const metadata = await getMetadata({ id: "onb-1", variantKey: MAIN_VARIANT_KEY, deploymentId: 7 });
|
|
62
|
+
expect(metadata.variantKey).toBe(MAIN_VARIANT_KEY);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("keeps null variantKey and deploymentId null", async () => {
|
|
66
|
+
const metadata = await getMetadata({ id: "onb-1", draft: true, variantKey: null, deploymentId: null });
|
|
67
|
+
expect(metadata.variantKey).toBeNull();
|
|
68
|
+
expect(metadata.deploymentId).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("reads the fields as undefined on a payload that does not carry them", async () => {
|
|
72
|
+
const metadata = await getMetadata({ id: "onb-1", audienceId: "7" });
|
|
73
|
+
expect(metadata.variantKey).toBeUndefined();
|
|
74
|
+
expect(metadata.deploymentId).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
});
|