import assert from 'node:assert/strict' import test from 'node:test' import { buildDouyinMashidaMessageUrl, describeMissingConfigFields, isLocalDevBaseUrl, } from '../src/utils/douyin-mashida.ts' /** 固定顺序:接入开关 / 商户uid / 用户秘钥 / 兑换链接 */ const ALL_MISSING = ['接入开关', '商户uid', '用户秘钥', '兑换链接'] test('isLocalDevBaseUrl:127.0.0.1 / localhost / host.docker.internal(含端口与结尾斜杠)判为 true', () => { assert.equal(isLocalDevBaseUrl('http://127.0.0.1:3000'), true) assert.equal(isLocalDevBaseUrl('http://127.0.0.1:3000/'), true) assert.equal(isLocalDevBaseUrl('http://127.0.0.1'), true) assert.equal(isLocalDevBaseUrl('http://localhost:8080/'), true) assert.equal(isLocalDevBaseUrl('http://localhost'), true) assert.equal(isLocalDevBaseUrl('http://host.docker.internal:3000/'), true) assert.equal(isLocalDevBaseUrl(' http://localhost:5173// '), true) }) test('isLocalDevBaseUrl:线上地址 / 空串 / 非本地域名判为 false', () => { assert.equal(isLocalDevBaseUrl('https://dy-openplatform-api.wanmahui.com'), false) assert.equal(isLocalDevBaseUrl('https://dy-openplatform-api.wanmahui.com/'), false) assert.equal(isLocalDevBaseUrl(''), false) assert.equal(isLocalDevBaseUrl(' '), false) assert.equal(isLocalDevBaseUrl('http://example.com'), false) assert.equal(isLocalDevBaseUrl('http://notlocalhost:3000'), false) }) test('buildDouyinMashidaMessageUrl:拼接推送地址并容忍结尾斜杠', () => { const suffix = '/api/v1/open/douyin-mashida/message' assert.equal( buildDouyinMashidaMessageUrl('https://ks.khhao.com'), `https://ks.khhao.com${suffix}`, ) assert.equal( buildDouyinMashidaMessageUrl('https://ks.khhao.com/'), `https://ks.khhao.com${suffix}`, ) assert.equal( buildDouyinMashidaMessageUrl('http://127.0.0.1:3000//'), `http://127.0.0.1:3000${suffix}`, ) }) test('describeMissingConfigFields:四项齐全返回空数组', () => { assert.deepEqual( describeMissingConfigFields({ enabled: true, uid: 'uid_123', hasSecret: true, certLink: 'https://ks.khhao.com/redeem', }), [], ) }) test('describeMissingConfigFields:缺多项按固定顺序返回中文名', () => { assert.deepEqual( describeMissingConfigFields({ enabled: false, uid: '', hasSecret: false, certLink: '' }), ALL_MISSING, ) }) test('describeMissingConfigFields:缺单项只返回对应中文名', () => { assert.deepEqual( describeMissingConfigFields({ enabled: true, uid: 'uid_123', hasSecret: false, certLink: 'https://ks.khhao.com/redeem', }), ['用户秘钥'], ) assert.deepEqual( describeMissingConfigFields({ enabled: false, uid: 'uid_123', hasSecret: true, certLink: ' ', }), ['接入开关', '兑换链接'], ) })