38 lines
1.7 KiB
JavaScript
38 lines
1.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import test from 'node:test';
|
|
import ts from 'typescript';
|
|
|
|
const source = readFileSync(new URL('../src/utils/request.ts', import.meta.url), 'utf8');
|
|
const ast = ts.createSourceFile('request.ts', source, ts.ScriptTarget.Latest, true);
|
|
let api;
|
|
function visit(node) {
|
|
if (ts.isVariableDeclaration(node) && node.name.getText(ast) === 'api') api = node.initializer;
|
|
ts.forEachChild(node, visit);
|
|
}
|
|
visit(ast);
|
|
assert.ok(api && ts.isObjectLiteralExpression(api));
|
|
function call(name, ...args) {
|
|
const property = api.properties.find(item => item.name?.getText(ast) === name);
|
|
const javascript = ts.transpileModule(`const call = ${property.initializer.getText(ast)};`, {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2020 },
|
|
}).outputText;
|
|
return new Function('request', 'getDeviceId', `${javascript}; return call;`)(
|
|
(url, options) => ({ url, ...options }), () => 'test-device',
|
|
)(...args);
|
|
}
|
|
|
|
test('H5 login requests stay compatible with the deployed strict JSON API', () => {
|
|
assert.deepEqual(call('oauthStart', 'github', 'h5').data, { provider: 'github' });
|
|
assert.deepEqual(call('oauthExchange', 'code', '').data, { code: 'code', deviceId: 'test-device' });
|
|
assert.deepEqual(call('oauthLink', 'code', 'phone', '123456', '').data, {
|
|
code: 'code', deviceId: 'test-device', phone: 'phone', smsCode: '123456',
|
|
});
|
|
});
|
|
|
|
test('App-specific platform and proof are retained when supplied', () => {
|
|
assert.deepEqual(call('oauthStart', 'github', 'app').data, { provider: 'github', platform: 'app' });
|
|
assert.equal(call('oauthExchange', 'code', 'proof').data.appProof, 'proof');
|
|
assert.equal(call('oauthLink', 'code', 'phone', '123456', 'proof').data.appProof, 'proof');
|
|
});
|