@terreno/rtk 0.9.3 → 0.11.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/authSlice.test.js +257 -1
- package/dist/authSlice.test.js.map +1 -1
- package/dist/authSliceNative.test.d.ts +2 -0
- package/dist/authSliceNative.test.d.ts.map +1 -0
- package/dist/authSliceNative.test.js +167 -0
- package/dist/authSliceNative.test.js.map +1 -0
- package/dist/betterAuthClient.d.ts +16 -0
- package/dist/betterAuthClient.d.ts.map +1 -1
- package/dist/betterAuthClient.js +5 -2
- package/dist/betterAuthClient.js.map +1 -1
- package/dist/betterAuthClient.test.d.ts +2 -0
- package/dist/betterAuthClient.test.d.ts.map +1 -0
- package/dist/betterAuthClient.test.js +151 -0
- package/dist/betterAuthClient.test.js.map +1 -0
- package/dist/betterAuthSlice.test.js +54 -1
- package/dist/betterAuthSlice.test.js.map +1 -1
- package/dist/buildNumber.test.d.ts +2 -0
- package/dist/buildNumber.test.d.ts.map +1 -0
- package/dist/buildNumber.test.js +95 -0
- package/dist/buildNumber.test.js.map +1 -0
- package/dist/constants.d.ts +27 -3
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +45 -56
- package/dist/constants.js.map +1 -1
- package/dist/constants.test.d.ts +2 -0
- package/dist/constants.test.d.ts.map +1 -0
- package/dist/constants.test.js +195 -0
- package/dist/constants.test.js.map +1 -0
- package/dist/mongooseSlice.test.d.ts +2 -0
- package/dist/mongooseSlice.test.d.ts.map +1 -0
- package/dist/mongooseSlice.test.js +39 -0
- package/dist/mongooseSlice.test.js.map +1 -0
- package/dist/tagGenerator.test.d.ts +2 -0
- package/dist/tagGenerator.test.d.ts.map +1 -0
- package/dist/tagGenerator.test.js +96 -0
- package/dist/tagGenerator.test.js.map +1 -0
- package/dist/testPreload.test.d.ts +2 -0
- package/dist/testPreload.test.d.ts.map +1 -0
- package/dist/testPreload.test.js +27 -0
- package/dist/testPreload.test.js.map +1 -0
- package/dist/useFeatureFlags.d.ts +25 -1
- package/dist/useFeatureFlags.d.ts.map +1 -1
- package/dist/useFeatureFlags.js +18 -16
- package/dist/useFeatureFlags.js.map +1 -1
- package/dist/useFeatureFlags.test.d.ts +2 -0
- package/dist/useFeatureFlags.test.d.ts.map +1 -0
- package/dist/useFeatureFlags.test.js +162 -0
- package/dist/useFeatureFlags.test.js.map +1 -0
- package/package.json +6 -3
- package/src/authSlice.test.ts +298 -0
- package/src/authSliceNative.test.ts +187 -0
- package/src/betterAuthClient.test.ts +176 -0
- package/src/betterAuthClient.ts +6 -3
- package/src/betterAuthSlice.test.ts +67 -0
- package/src/buildNumber.test.ts +120 -0
- package/src/constants.test.ts +228 -0
- package/src/constants.ts +72 -70
- package/src/mongooseSlice.test.ts +46 -0
- package/src/tagGenerator.test.ts +109 -0
- package/src/testPreload.test.ts +30 -0
- package/src/useFeatureFlags.test.ts +209 -0
- package/src/useFeatureFlags.ts +44 -5
- package/dist/test-preload.d.ts +0 -2
- package/dist/test-preload.d.ts.map +0 -1
- package/dist/test-preload.js +0 -24
- package/dist/test-preload.js.map +0 -1
- package/src/test-preload.ts +0 -28
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AUTH_DEBUG,
|
|
5
|
+
baseTasksUrl,
|
|
6
|
+
baseUrl,
|
|
7
|
+
baseWebsocketsUrl,
|
|
8
|
+
logAuth,
|
|
9
|
+
logSocket,
|
|
10
|
+
resolveBaseUrls,
|
|
11
|
+
} from "./constants";
|
|
12
|
+
|
|
13
|
+
describe("resolveBaseUrls", () => {
|
|
14
|
+
it("treats an empty envApiUrl as unset and falls back to localhost", () => {
|
|
15
|
+
const urls = resolveBaseUrls({
|
|
16
|
+
envApiUrl: "",
|
|
17
|
+
expoConstants: {expoConfig: {extra: {}}},
|
|
18
|
+
isDev: false,
|
|
19
|
+
});
|
|
20
|
+
expect(urls.baseUrl).toBe("http://localhost:4000");
|
|
21
|
+
expect(urls.baseWebsocketsUrl).toBe("ws://localhost:4000/");
|
|
22
|
+
expect(urls.baseTasksUrl).toBe("http://localhost:4000/tasks");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("treats an empty BASE_URL extra as unset and falls back to localhost in non-dev", () => {
|
|
26
|
+
const urls = resolveBaseUrls({
|
|
27
|
+
expoConstants: {expoConfig: {extra: {BASE_URL: ""}}},
|
|
28
|
+
isDev: false,
|
|
29
|
+
});
|
|
30
|
+
expect(urls.baseUrl).toBe("http://localhost:4000");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("uses env override when provided", () => {
|
|
34
|
+
const urls = resolveBaseUrls({
|
|
35
|
+
envApiUrl: "https://api.example.com",
|
|
36
|
+
expoConstants: {expoConfig: {extra: {}}},
|
|
37
|
+
isDev: false,
|
|
38
|
+
});
|
|
39
|
+
expect(urls.baseUrl).toBe("https://api.example.com");
|
|
40
|
+
expect(urls.baseWebsocketsUrl).toBe("https://ws.example.com/");
|
|
41
|
+
expect(urls.baseTasksUrl).toBe("https://tasks.example.com/tasks");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("uses hostUri in dev mode", () => {
|
|
45
|
+
const urls = resolveBaseUrls({
|
|
46
|
+
expoConstants: {expoConfig: {extra: {}, hostUri: "10.0.0.12:8081"}},
|
|
47
|
+
isDev: true,
|
|
48
|
+
});
|
|
49
|
+
expect(urls.baseUrl).toBe("http://10.0.0.12:4000");
|
|
50
|
+
expect(urls.baseWebsocketsUrl).toBe("ws://10.0.0.12:4000/");
|
|
51
|
+
expect(urls.baseTasksUrl).toBe("http://10.0.0.12:4000/tasks");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("falls back to experienceUrl in dev mode when hostUri missing", () => {
|
|
55
|
+
const urls = resolveBaseUrls({
|
|
56
|
+
expoConstants: {
|
|
57
|
+
experienceUrl: "exp://192.168.1.20:19000",
|
|
58
|
+
expoConfig: {extra: {}},
|
|
59
|
+
},
|
|
60
|
+
isDev: true,
|
|
61
|
+
});
|
|
62
|
+
expect(urls.baseUrl).toBe("http://192.168.1.20:4000");
|
|
63
|
+
expect(urls.baseWebsocketsUrl).toBe("ws://192.168.1.20:4000/");
|
|
64
|
+
expect(urls.baseTasksUrl).toBe("http://192.168.1.20:4000/tasks");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("falls back to localhost in dev mode when nothing else is available", () => {
|
|
68
|
+
const urls = resolveBaseUrls({
|
|
69
|
+
expoConstants: {expoConfig: {extra: {}}},
|
|
70
|
+
isDev: true,
|
|
71
|
+
});
|
|
72
|
+
expect(urls.baseUrl).toBe("http://localhost:4000");
|
|
73
|
+
expect(urls.baseWebsocketsUrl).toBe("ws://localhost:4000/");
|
|
74
|
+
expect(urls.baseTasksUrl).toBe("http://localhost:4000/tasks");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("uses BASE_URL from extra when not in dev mode", () => {
|
|
78
|
+
const urls = resolveBaseUrls({
|
|
79
|
+
expoConstants: {expoConfig: {extra: {BASE_URL: "https://api.prod.com"}}},
|
|
80
|
+
isDev: false,
|
|
81
|
+
});
|
|
82
|
+
expect(urls.baseUrl).toBe("https://api.prod.com");
|
|
83
|
+
expect(urls.baseWebsocketsUrl).toBe("https://ws.prod.com/");
|
|
84
|
+
expect(urls.baseTasksUrl).toBe("https://tasks.prod.com/tasks");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("falls back to hostUri when BASE_URL absent in non-dev", () => {
|
|
88
|
+
const urls = resolveBaseUrls({
|
|
89
|
+
expoConstants: {expoConfig: {extra: {}, hostUri: "172.16.0.3:8081"}},
|
|
90
|
+
isDev: false,
|
|
91
|
+
});
|
|
92
|
+
expect(urls.baseUrl).toBe("http://172.16.0.3:4000");
|
|
93
|
+
expect(urls.baseWebsocketsUrl).toBe("ws://172.16.0.3:4000/");
|
|
94
|
+
expect(urls.baseTasksUrl).toBe("http://172.16.0.3:4000/tasks");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("falls back to experienceUrl in non-dev when hostUri absent", () => {
|
|
98
|
+
const urls = resolveBaseUrls({
|
|
99
|
+
expoConstants: {
|
|
100
|
+
experienceUrl: "exp://10.1.2.3:19000",
|
|
101
|
+
expoConfig: {extra: {}},
|
|
102
|
+
},
|
|
103
|
+
isDev: false,
|
|
104
|
+
});
|
|
105
|
+
expect(urls.baseUrl).toBe("http://10.1.2.3:4000");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("defaults to localhost in non-dev when nothing is configured", () => {
|
|
109
|
+
const urls = resolveBaseUrls({
|
|
110
|
+
expoConstants: {expoConfig: {extra: {}}},
|
|
111
|
+
isDev: false,
|
|
112
|
+
});
|
|
113
|
+
expect(urls.baseUrl).toBe("http://localhost:4000");
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("module-level exports", () => {
|
|
118
|
+
it("exports baseUrl / websockets / tasks URLs that resolve to localhost with default mocks", () => {
|
|
119
|
+
expect(baseUrl).toBe("http://localhost:4000");
|
|
120
|
+
expect(baseWebsocketsUrl).toBe("ws://localhost:4000/");
|
|
121
|
+
expect(baseTasksUrl).toBe("http://localhost:4000/tasks");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("AUTH_DEBUG is false by default with test-preload mocks", () => {
|
|
125
|
+
expect(AUTH_DEBUG).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("logAuth / logSocket", () => {
|
|
130
|
+
const originalDebug = console.debug;
|
|
131
|
+
const calls: unknown[][] = [];
|
|
132
|
+
|
|
133
|
+
beforeEach(() => {
|
|
134
|
+
calls.length = 0;
|
|
135
|
+
console.debug = (...args: unknown[]): void => {
|
|
136
|
+
calls.push(args);
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
afterEach(() => {
|
|
141
|
+
console.debug = originalDebug;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("logAuth is a no-op when AUTH_DEBUG is disabled", () => {
|
|
145
|
+
logAuth("auth message");
|
|
146
|
+
expect(calls).toEqual([]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("logSocket logs when passed boolean true", () => {
|
|
150
|
+
logSocket(true, "socket message");
|
|
151
|
+
expect(calls).toEqual([["[websocket]", "socket message"]]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("logSocket does not log when passed boolean false", () => {
|
|
155
|
+
logSocket(false, "suppressed");
|
|
156
|
+
expect(calls).toEqual([]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("logSocket logs when user has debugWebsockets feature flag enabled", () => {
|
|
160
|
+
logSocket({featureFlags: {debugWebsockets: {enabled: true}}}, "enabled");
|
|
161
|
+
expect(calls).toEqual([["[websocket]", "enabled"]]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("logSocket does not log when user has debugWebsockets disabled", () => {
|
|
165
|
+
logSocket({featureFlags: {debugWebsockets: {enabled: false}}}, "disabled");
|
|
166
|
+
expect(calls).toEqual([]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("logSocket does not log with undefined user", () => {
|
|
170
|
+
logSocket(undefined, "no user");
|
|
171
|
+
expect(calls).toEqual([]);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe("expo tunnel warning", () => {
|
|
176
|
+
it("warns when expoGoConfig.debuggerHost contains exp.direct", async () => {
|
|
177
|
+
const errorCalls: unknown[][] = [];
|
|
178
|
+
const originalError = console.error;
|
|
179
|
+
console.error = (...args: unknown[]): void => {
|
|
180
|
+
errorCalls.push(args);
|
|
181
|
+
};
|
|
182
|
+
mock.module("expo-constants", () => ({
|
|
183
|
+
default: {
|
|
184
|
+
expoConfig: {extra: {}},
|
|
185
|
+
expoGoConfig: {debuggerHost: "abc.exp.direct"},
|
|
186
|
+
},
|
|
187
|
+
}));
|
|
188
|
+
try {
|
|
189
|
+
const testId = `${Date.now()}-${Math.random()}`;
|
|
190
|
+
await import(`./constants?case=${testId}`);
|
|
191
|
+
const warning = errorCalls.find((args) =>
|
|
192
|
+
args.some((v) => typeof v === "string" && v.includes("Expo Tunnel is not currently"))
|
|
193
|
+
);
|
|
194
|
+
expect(warning).toBeDefined();
|
|
195
|
+
} finally {
|
|
196
|
+
console.error = originalError;
|
|
197
|
+
mock.module("expo-constants", () => ({default: {expoConfig: {extra: {}}}}));
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe("AUTH_DEBUG enabled path", () => {
|
|
203
|
+
it("logs debug messages from logAuth when AUTH_DEBUG is true on module load", async () => {
|
|
204
|
+
const debugCalls: unknown[][] = [];
|
|
205
|
+
const originalDebug = console.debug;
|
|
206
|
+
console.debug = (...args: unknown[]): void => {
|
|
207
|
+
debugCalls.push(args);
|
|
208
|
+
};
|
|
209
|
+
mock.module("expo-constants", () => ({
|
|
210
|
+
default: {expoConfig: {extra: {AUTH_DEBUG: "true", WEBSOCKETS_DEBUG: "true"}}},
|
|
211
|
+
}));
|
|
212
|
+
try {
|
|
213
|
+
const testId = `${Date.now()}-${Math.random()}`;
|
|
214
|
+
const loaded = (await import(`./constants?case=${testId}`)) as typeof import("./constants");
|
|
215
|
+
expect(loaded.AUTH_DEBUG).toBe(true);
|
|
216
|
+
const preLength = debugCalls.length;
|
|
217
|
+
loaded.logAuth("hello");
|
|
218
|
+
expect(debugCalls.length).toBe(preLength + 1);
|
|
219
|
+
loaded.logSocket(undefined, "ws on");
|
|
220
|
+
expect(debugCalls.some((args) => args[0] === "[websocket]" && args[1] === "ws on")).toBe(
|
|
221
|
+
true
|
|
222
|
+
);
|
|
223
|
+
} finally {
|
|
224
|
+
console.debug = originalDebug;
|
|
225
|
+
mock.module("expo-constants", () => ({default: {expoConfig: {extra: {}}}}));
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
package/src/constants.ts
CHANGED
|
@@ -37,6 +37,65 @@ export const logSocket = (
|
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
+
export interface ExpoConstantsShape {
|
|
41
|
+
experienceUrl?: string;
|
|
42
|
+
expoConfig?: {
|
|
43
|
+
extra?: Record<string, string | undefined>;
|
|
44
|
+
hostUri?: string;
|
|
45
|
+
};
|
|
46
|
+
expoGoConfig?: {
|
|
47
|
+
debuggerHost?: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface BaseUrls {
|
|
52
|
+
baseUrl: string;
|
|
53
|
+
baseWebsocketsUrl: string;
|
|
54
|
+
baseTasksUrl: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const LOCALHOST: BaseUrls = {
|
|
58
|
+
baseTasksUrl: "http://localhost:4000/tasks",
|
|
59
|
+
baseUrl: "http://localhost:4000",
|
|
60
|
+
baseWebsocketsUrl: "ws://localhost:4000/",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Pure resolver for the base URLs used throughout the RTK package.
|
|
65
|
+
* Decoupled from the Expo-constants module so it can be unit tested.
|
|
66
|
+
*/
|
|
67
|
+
export const resolveBaseUrls = (args: {
|
|
68
|
+
envApiUrl?: string;
|
|
69
|
+
expoConstants: ExpoConstantsShape;
|
|
70
|
+
isDev: boolean;
|
|
71
|
+
}): BaseUrls => {
|
|
72
|
+
const hostUriPrefix = args.expoConstants.expoConfig?.hostUri?.split(":").shift();
|
|
73
|
+
const experiencePrefix = args.expoConstants.experienceUrl?.split(":")[1];
|
|
74
|
+
const baseFromExtra = args.expoConstants.expoConfig?.extra?.BASE_URL;
|
|
75
|
+
const base = args.envApiUrl || (!args.isDev ? baseFromExtra : undefined);
|
|
76
|
+
const host = args.isDev ? hostUriPrefix : !base ? hostUriPrefix : undefined;
|
|
77
|
+
const experience = !base && !host ? experiencePrefix : undefined;
|
|
78
|
+
if (base)
|
|
79
|
+
return {
|
|
80
|
+
baseTasksUrl: `${base.replace("api.", "tasks.")}/tasks`,
|
|
81
|
+
baseUrl: base,
|
|
82
|
+
baseWebsocketsUrl: `${base.replace("api.", "ws.")}/`,
|
|
83
|
+
};
|
|
84
|
+
if (host)
|
|
85
|
+
return {
|
|
86
|
+
baseTasksUrl: `http://${host}:4000/tasks`,
|
|
87
|
+
baseUrl: `http://${host}:4000`,
|
|
88
|
+
baseWebsocketsUrl: `ws://${host}:4000/`,
|
|
89
|
+
};
|
|
90
|
+
if (experience)
|
|
91
|
+
return {
|
|
92
|
+
baseTasksUrl: `http:${experience}:4000/tasks`,
|
|
93
|
+
baseUrl: `http:${experience}:4000`,
|
|
94
|
+
baseWebsocketsUrl: `ws:${experience}:4000/`,
|
|
95
|
+
};
|
|
96
|
+
return LOCALHOST;
|
|
97
|
+
};
|
|
98
|
+
|
|
40
99
|
// When we use "expo publish", we want to point the API at the prod API. In the future,
|
|
41
100
|
// we'll want to point at the staging API, and probably have a development release channel.
|
|
42
101
|
if (Constants.expoGoConfig?.debuggerHost?.includes("exp.direct")) {
|
|
@@ -45,77 +104,20 @@ if (Constants.expoGoConfig?.debuggerHost?.includes("exp.direct")) {
|
|
|
45
104
|
);
|
|
46
105
|
}
|
|
47
106
|
|
|
48
|
-
export let baseUrl: string;
|
|
49
|
-
export let baseWebsocketsUrl: string;
|
|
50
|
-
export let baseTasksUrl: string;
|
|
51
|
-
|
|
52
107
|
const isDev = typeof __DEV__ !== "undefined" && __DEV__;
|
|
53
108
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
109
|
+
const resolved = resolveBaseUrls({
|
|
110
|
+
envApiUrl: process.env.EXPO_PUBLIC_API_URL,
|
|
111
|
+
expoConstants: Constants as ExpoConstantsShape,
|
|
112
|
+
isDev,
|
|
113
|
+
});
|
|
59
114
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}, websocket to ${baseWebsocketsUrl}, tasks to ${baseTasksUrl}`
|
|
64
|
-
);
|
|
65
|
-
} else if (isDev && Constants.expoConfig?.hostUri) {
|
|
66
|
-
// Dev simulator/device
|
|
67
|
-
baseUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}`;
|
|
68
|
-
baseWebsocketsUrl = `ws://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/`;
|
|
69
|
-
baseTasksUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/tasks`;
|
|
70
|
-
console.debug(
|
|
71
|
-
`Base URL set to hostUri ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
|
|
72
|
-
Constants.expoConfig?.hostUri
|
|
73
|
-
);
|
|
74
|
-
} else if (isDev && Constants.experienceUrl) {
|
|
75
|
-
// Dev web (experienceUrl)
|
|
76
|
-
baseUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}`;
|
|
77
|
-
baseWebsocketsUrl = `ws:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/`;
|
|
78
|
-
baseTasksUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/tasks`;
|
|
79
|
-
console.debug(
|
|
80
|
-
`Base URL set to experienceUrl ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
|
|
81
|
-
Constants.expoConfig?.hostUri
|
|
82
|
-
);
|
|
83
|
-
} else if (isDev) {
|
|
84
|
-
// Dev web fallback
|
|
85
|
-
baseUrl = `http://localhost:4000`;
|
|
86
|
-
baseWebsocketsUrl = `ws://localhost:4000/`;
|
|
87
|
-
baseTasksUrl = `http://localhost:4000/tasks`;
|
|
88
|
-
console.debug(`Base URL set to localhost ${baseUrl}, websocket to ${baseWebsocketsUrl}`);
|
|
89
|
-
} else if (Constants.expoConfig?.extra?.BASE_URL) {
|
|
90
|
-
// Prod/staging
|
|
91
|
-
baseUrl = Constants.expoConfig?.extra?.BASE_URL;
|
|
92
|
-
baseWebsocketsUrl = `${baseUrl.replace("api.", "ws.")}/`;
|
|
93
|
-
baseTasksUrl = `${baseUrl.replace("api.", "tasks.")}/tasks`;
|
|
115
|
+
export const baseUrl = resolved.baseUrl;
|
|
116
|
+
export const baseWebsocketsUrl = resolved.baseWebsocketsUrl;
|
|
117
|
+
export const baseTasksUrl = resolved.baseTasksUrl;
|
|
94
118
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
} else if (Constants.expoConfig?.hostUri) {
|
|
101
|
-
baseUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}`;
|
|
102
|
-
baseWebsocketsUrl = `ws://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/`;
|
|
103
|
-
baseTasksUrl = `http://${Constants.expoConfig?.hostUri?.split(`:`).shift()?.concat(":4000")}/tasks`;
|
|
104
|
-
console.debug(
|
|
105
|
-
`Base URL set to hostUri ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
|
|
106
|
-
Constants.expoConfig?.hostUri
|
|
107
|
-
);
|
|
108
|
-
} else if (Constants.experienceUrl) {
|
|
109
|
-
baseUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}`;
|
|
110
|
-
baseWebsocketsUrl = `ws:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/`;
|
|
111
|
-
baseTasksUrl = `http:${Constants.experienceUrl?.split(`:`)[1]?.concat(":4000")}/tasks`;
|
|
112
|
-
console.debug(
|
|
113
|
-
`Base URL set to experienceUrl ${baseUrl}, websocket to ${baseWebsocketsUrl}`,
|
|
114
|
-
Constants.expoConfig?.hostUri
|
|
115
|
-
);
|
|
116
|
-
} else {
|
|
117
|
-
baseUrl = `http://localhost:4000`;
|
|
118
|
-
baseWebsocketsUrl = `ws://localhost:4000/`;
|
|
119
|
-
baseTasksUrl = `http://localhost:4000/tasks`;
|
|
120
|
-
console.debug(`Base URL set to localhost ${baseUrl}, websocket to ${baseWebsocketsUrl}`);
|
|
121
|
-
}
|
|
119
|
+
console.debug(
|
|
120
|
+
`Base URL set to ${baseUrl} for env ${
|
|
121
|
+
Constants.expoConfig?.extra?.APP_ENV ?? "unknown"
|
|
122
|
+
}, websocket to ${baseWebsocketsUrl}, tasks to ${baseTasksUrl}`
|
|
123
|
+
);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {type ListResponse, populateId} from "./mongooseSlice";
|
|
4
|
+
|
|
5
|
+
describe("populateId", () => {
|
|
6
|
+
const items = [
|
|
7
|
+
{_id: "a", name: "alpha"},
|
|
8
|
+
{_id: "b", name: "beta"},
|
|
9
|
+
{_id: "c", name: "gamma"},
|
|
10
|
+
];
|
|
11
|
+
const response: ListResponse<(typeof items)[number]> = {
|
|
12
|
+
data: items,
|
|
13
|
+
limit: 10,
|
|
14
|
+
more: false,
|
|
15
|
+
page: 1,
|
|
16
|
+
total: items.length,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
it("returns the matching object by id", () => {
|
|
20
|
+
expect(populateId("b", response)).toEqual({_id: "b", name: "beta"});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("returns undefined when id is missing", () => {
|
|
24
|
+
expect(populateId(undefined, response)).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("returns undefined when objs is missing", () => {
|
|
28
|
+
expect(populateId("a")).toBeUndefined();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("returns undefined when objs has no data", () => {
|
|
32
|
+
expect(populateId("a", {})).toBeUndefined();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("returns undefined when no item matches", () => {
|
|
36
|
+
expect(populateId("z", response)).toBeUndefined();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("handles sparse arrays without throwing", () => {
|
|
40
|
+
const sparse: ListResponse<{_id: string}> = {
|
|
41
|
+
// biome-ignore lint/suspicious/noExplicitAny: Simulating malformed API payloads.
|
|
42
|
+
data: [undefined as any, {_id: "found"}],
|
|
43
|
+
};
|
|
44
|
+
expect(populateId("found", sparse)).toEqual({_id: "found"});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {generateTags} from "./tagGenerator";
|
|
4
|
+
|
|
5
|
+
describe("generateTags", () => {
|
|
6
|
+
const tagTypes = ["users", "posts", "conversations", "messages"];
|
|
7
|
+
|
|
8
|
+
it("assigns invalidatesTags for getConversations endpoint", () => {
|
|
9
|
+
const api = {
|
|
10
|
+
endpoints: {
|
|
11
|
+
getConversations: {},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
const tags = generateTags(api, tagTypes);
|
|
15
|
+
// getConversations matches both the special case AND the list-endpoint branch,
|
|
16
|
+
// so the list-endpoint branch wins as the final assignment. The special case
|
|
17
|
+
// still executes for coverage, but the final value is the list providesTags.
|
|
18
|
+
expect(tags.getConversations).toBeDefined();
|
|
19
|
+
expect(typeof tags.getConversations.providesTags).toBe("function");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("assigns providesTags on list get endpoints", () => {
|
|
23
|
+
const api = {
|
|
24
|
+
endpoints: {
|
|
25
|
+
getUsers: {},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const tags = generateTags(api, tagTypes);
|
|
29
|
+
expect(typeof tags.getUsers.providesTags).toBe("function");
|
|
30
|
+
|
|
31
|
+
// Exercise the returned provides function with and without data
|
|
32
|
+
const providesFn = tags.getUsers.providesTags;
|
|
33
|
+
expect(providesFn(null)).toEqual(["users"]);
|
|
34
|
+
expect(providesFn({data: [{_id: "1"}, {_id: "2"}]})).toEqual([
|
|
35
|
+
{id: "1", type: "users"},
|
|
36
|
+
{id: "2", type: "users"},
|
|
37
|
+
"users",
|
|
38
|
+
]);
|
|
39
|
+
expect(providesFn({})).toEqual(["users"]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("assigns providesTags on read (ById) get endpoints", () => {
|
|
43
|
+
const api = {
|
|
44
|
+
endpoints: {
|
|
45
|
+
getUserById: {},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
const tags = generateTags(api, tagTypes);
|
|
49
|
+
expect(typeof tags.getUserById.providesTags).toBe("function");
|
|
50
|
+
|
|
51
|
+
const providesFn = tags.getUserById.providesTags;
|
|
52
|
+
expect(providesFn(null)).toEqual(["users"]);
|
|
53
|
+
expect(providesFn({_id: "abc"})).toEqual([{id: "abc", type: "users"}]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("assigns invalidatesTags on patch endpoints", () => {
|
|
57
|
+
const api = {
|
|
58
|
+
endpoints: {
|
|
59
|
+
patchUserById: {},
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
const tags = generateTags(api, tagTypes);
|
|
63
|
+
expect(typeof tags.patchUserById.invalidatesTags).toBe("function");
|
|
64
|
+
|
|
65
|
+
const invalidateFn = tags.patchUserById.invalidatesTags;
|
|
66
|
+
expect(invalidateFn(null)).toEqual(["users"]);
|
|
67
|
+
expect(invalidateFn({data: [{_id: "x"}]})).toEqual([{id: "x", type: "users"}, "users"]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("assigns invalidatesTags on delete endpoints", () => {
|
|
71
|
+
const api = {
|
|
72
|
+
endpoints: {
|
|
73
|
+
deletePostById: {},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
const tags = generateTags(api, tagTypes);
|
|
77
|
+
expect(typeof tags.deletePostById.invalidatesTags).toBe("function");
|
|
78
|
+
|
|
79
|
+
const invalidateFn = tags.deletePostById.invalidatesTags;
|
|
80
|
+
expect(invalidateFn(null)).toEqual(["posts"]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("skips endpoints with no matching tag", () => {
|
|
84
|
+
const api = {
|
|
85
|
+
endpoints: {
|
|
86
|
+
getUnknownThing: {},
|
|
87
|
+
patchOrphan: {},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
const tags = generateTags(api, tagTypes);
|
|
91
|
+
expect(tags.getUnknownThing).toBeUndefined();
|
|
92
|
+
expect(tags.patchOrphan).toBeUndefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("returns an empty object for an empty endpoint list", () => {
|
|
96
|
+
const tags = generateTags({endpoints: {}}, tagTypes);
|
|
97
|
+
expect(tags).toEqual({});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("ignores endpoints that are not get/patch/delete", () => {
|
|
101
|
+
const api = {
|
|
102
|
+
endpoints: {
|
|
103
|
+
postUser: {},
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
const tags = generateTags(api, tagTypes);
|
|
107
|
+
expect(tags.postUser).toBeUndefined();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
// Invoke the mocked module factories registered in test-preload.ts so
|
|
4
|
+
// their bodies (and returned functions) are recognised as covered.
|
|
5
|
+
describe("test-preload default mocks", () => {
|
|
6
|
+
it("expo-secure-store mock returns a no-op store", async () => {
|
|
7
|
+
const SecureStore = await import("expo-secure-store");
|
|
8
|
+
await SecureStore.setItemAsync("k", "v");
|
|
9
|
+
await SecureStore.deleteItemAsync("k");
|
|
10
|
+
expect(await SecureStore.getItemAsync("k")).toBeNull();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("AsyncStorage mock returns a no-op store", async () => {
|
|
14
|
+
const AsyncStorage = (await import("@react-native-async-storage/async-storage")).default;
|
|
15
|
+
await AsyncStorage.setItem("k", "v");
|
|
16
|
+
await AsyncStorage.removeItem("k");
|
|
17
|
+
expect(await AsyncStorage.getItem("k")).toBeNull();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("expo-network mock reports a connected network", async () => {
|
|
21
|
+
const network = await import("expo-network");
|
|
22
|
+
const state = await network.getNetworkStateAsync();
|
|
23
|
+
expect(state.isConnected).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("expo-constants mock exposes an empty config", async () => {
|
|
27
|
+
const Constants = (await import("expo-constants")).default;
|
|
28
|
+
expect(Constants.expoConfig?.extra).toBeDefined();
|
|
29
|
+
});
|
|
30
|
+
});
|