resolve-api-env-key.test.cjs
5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const ts = require('typescript');
const moduleCache = new Map();
function transpileAndEvaluate(absolutePath) {
if (moduleCache.has(absolutePath)) {
return moduleCache.get(absolutePath);
}
let source = fs.readFileSync(absolutePath, 'utf8');
source = source.replace(/@\/config\//g, './');
source = source.replace(/import\.meta\.env\.DEV/g, 'false');
const compiled = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2020,
esModuleInterop: true,
},
fileName: absolutePath,
}).outputText;
const loadedModule = {exports: {}};
const localRequire = (request) => {
if (request.startsWith('.')) {
const resolved = path.resolve(path.dirname(absolutePath), request);
const candidates = [
resolved,
`${resolved}.ts`,
`${resolved}.js`,
];
for (const candidate of candidates) {
if (fs.existsSync(candidate)) {
return transpileAndEvaluate(candidate);
}
}
}
return require(request);
};
const evaluate = new Function('module', 'exports', 'require', '__filename', '__dirname', compiled);
evaluate(
loadedModule,
loadedModule.exports,
localRequire,
absolutePath,
path.dirname(absolutePath),
);
moduleCache.set(absolutePath, loadedModule.exports);
return loadedModule.exports;
}
function loadConfigModule(fileName) {
const absolutePath = path.resolve(__dirname, '..', 'src', 'config', fileName);
return transpileAndEvaluate(absolutePath);
}
const {resolveEnvKeyFromAppId, APP_ID_TO_ENV} = loadConfigModule('apiEnvironments.ts');
const {resolveApiEnvKeyFromContext} = loadConfigModule('resolveApiEnvKey.ts');
function runtime(overrides = {}) {
return {
appId: '',
envVersion: '',
platform: '',
isDevtools: false,
isWeixinMp: false,
...overrides,
};
}
// AppId mapping
assert.equal(resolveEnvKeyFromAppId('wx568d02f6352efd49'), 'test');
assert.equal(resolveEnvKeyFromAppId('wxa719209ed933d667'), 'prod');
assert.equal(resolveEnvKeyFromAppId('unknown'), undefined);
assert.equal(APP_ID_TO_ENV['wx568d02f6352efd49'], 'test');
// P0: devtools + test AppId auto-maps to test
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({isDevtools: true, isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
true,
'test',
),
{source: 'appId', envKey: 'test'},
);
// P0: devtools + local override wins over AppId
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({isDevtools: true, isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
true,
'local-125',
),
{source: 'devtools', envKey: 'local-125'},
);
// P0: devtools without AppId uses ACTIVE_DEV_API_ENV
assert.deepEqual(
resolveApiEnvKeyFromContext(runtime({isDevtools: true}), true, 'local-125'),
{source: 'devtools', envKey: 'local-125'},
);
// A known release AppId must use the same environment as trial/develop.
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'release', isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
false,
),
{source: 'appId', envKey: 'test'},
);
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'release', isWeixinMp: true, appId: 'wxa719209ed933d667'}),
false,
),
{source: 'appId', envKey: 'prod'},
);
// Unknown release AppIds retain the build-time fallback.
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'release', isWeixinMp: true, appId: 'wx-unknown'}),
false,
),
{source: 'build-env'},
);
// P2: trial + test AppId
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'trial', isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
false,
),
{source: 'appId', envKey: 'test'},
);
// P2: trial + prod AppId
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'trial', isWeixinMp: true, appId: 'wxa719209ed933d667'}),
false,
),
{source: 'appId', envKey: 'prod'},
);
// P3: develop + test AppId
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'develop', isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
false,
),
{source: 'appId', envKey: 'test'},
);
// P4: unknown AppId on trial
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'trial', isWeixinMp: true, appId: 'wx-unknown'}),
false,
),
{source: 'fallback'},
);
// P4: H5 / non-weixin mp
assert.deepEqual(
resolveApiEnvKeyFromContext(runtime({envVersion: 'trial'}), false),
{source: 'fallback'},
);
// P0: build preview in devtools + release envVersion still uses AppId (not build-env lock)
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({
isDevtools: true,
envVersion: 'release',
isWeixinMp: true,
appId: 'wx568d02f6352efd49',
}),
false,
'test',
),
{source: 'appId', envKey: 'test'},
);
// Real-device release also follows the known AppId mapping.
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'release', isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
false,
),
{source: 'appId', envKey: 'test'},
);
// A dev build on a real mini-program device still follows its known AppId.
assert.deepEqual(
resolveApiEnvKeyFromContext(
runtime({envVersion: 'develop', isWeixinMp: true, appId: 'wx568d02f6352efd49'}),
true,
),
{source: 'appId', envKey: 'test'},
);
console.log('resolve-api-env-key.test.cjs: all tests passed');