const assert = require('assert'); const GenealogyApi = require('../public/js/api-client.js'); function createStore(initial) { const values = Object.assign({}, initial); return { getItem(key) { return Object.prototype.hasOwnProperty.call(values, key) ? values[key] : null; }, setItem(key, value) { values[key] = String(value); }, removeItem(key) { delete values[key]; }, values }; } async function run() { const calls = []; const store = createStore({ 'token-key': 'abc-token' }); assert.strictEqual(GenealogyApi.DEFAULT_BASE_URL, 'http://test-genealogy-api.ddxcjp.cn'); const client = GenealogyApi.createClient({ baseUrl: 'https://test-genealogy-api.ddxcjp.cn', clientId: 'client-x', tenantId: 'tenant-x', tokenKey: 'token-key', tokenHeaderName: 'Authorization', tokenStore: store, fetchImpl: async (url, options) => { calls.push({ url, options }); return { ok: true, status: 200, json: async () => ({ code: 200, msg: '操作成功', data: { token: 'server-token', url } }) }; } }); await client.login({ phone: '13800000000', password: 'md5' }); assert.strictEqual(calls[0].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/login'); assert.strictEqual(calls[0].options.headers.clientid, 'client-x'); assert.strictEqual(calls[0].options.headers.Authorization, undefined); assert.strictEqual(JSON.parse(calls[0].options.body).tenantId, 'tenant-x'); assert.strictEqual(store.values['token-key'], 'server-token'); await client.getProfile(); assert.strictEqual(calls[1].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/profile'); assert.strictEqual(calls[1].options.headers.Authorization, 'Bearer server-token'); await client.updateProfile({ nickName: '张三', provinceCode: '510000', cityCode: '510100', districtCode: '510104' }); assert.strictEqual(calls[2].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/profile'); assert.strictEqual(calls[2].options.method, 'PUT'); await client.uploadFile('file-content'); assert.strictEqual(calls[3].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/files/upload'); assert.strictEqual(calls[3].options.headers['Content-Type'], undefined); await client.initResumableUpload({ fileName: 'cover.jpg', fileSize: 9532, fileMd5: 'md5', chunkSize: 4194304, totalChunks: 1 }); assert.strictEqual(calls[4].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/files/resumable/init'); await client.getRegionChildren('51'); assert.strictEqual(calls[5].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/region/children?parentCode=51'); await client.captchaRequire({ sceneCode: 'WEB_H5_LOGIN', subject: '13800000000' }); assert.strictEqual( calls[6].url, 'https://test-genealogy-api.ddxcjp.cn/captcha/require?tenantId=tenant-x&clientId=client-x&sceneCode=WEB_H5_LOGIN&subject=13800000000' ); assert.strictEqual(calls[6].options.headers.Authorization, undefined); await client.loginBySms({ phone: '13800000000', smsCode: '6666', validToken: 'sms-ticket' }); assert.strictEqual(calls[7].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/login/sms'); assert.strictEqual(calls[7].options.headers.Authorization, undefined); assert.strictEqual(JSON.parse(calls[7].options.body).grantType, 'sms'); assert.strictEqual(JSON.parse(calls[7].options.body).tenantId, 'tenant-x'); assert.strictEqual(JSON.parse(calls[7].options.body).clientId, 'client-x'); await client.sendSmsCode({ sceneCode: 'WEB_H5_LOGIN', phone: '13800000000', validToken: 'captcha-ticket' }); assert.strictEqual(calls[8].url, 'https://test-genealogy-api.ddxcjp.cn/genealogy/pc/auth/sms/code'); assert.strictEqual(calls[8].options.headers.Authorization, undefined); assert.strictEqual(JSON.parse(calls[8].options.body).grantType, 'sms'); assert.strictEqual(JSON.parse(calls[8].options.body).sceneCode, 'WEB_H5_LOGIN'); assert.strictEqual(JSON.parse(calls[8].options.body).validToken, 'captcha-ticket'); assert.throws( () => client.listGenealogies(), (error) => error.code === 'PC_API_NOT_AVAILABLE' ); assert.throws( () => client.publicGenealogies(), (error) => error.code === 'PC_API_NOT_AVAILABLE' ); assert.strictEqual(calls.length, 9); console.log('api-client tests passed'); } run();