import assert from 'node:assert/strict' import test from 'node:test' import { buildDouyinMashidaClaimLinkPreview, describeClaimLinkBaseWarning, describeMessageTemplateWarning, buildDouyinMashidaMessageUrl, describeMissingConfigFields, isLocalDevBaseUrl, resolveDouyinMashidaPublicOrigin, } 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: ' ', }), ['接入开关', '兑换链接'], ) }) test('领取链接预览 = 基址去尾斜杠 + token', () => { assert.equal( buildDouyinMashidaClaimLinkPreview('https://x.ngrok-free.app/claim', 'tok123'), 'https://x.ngrok-free.app/claim/tok123', ) // 尾斜杠与前后空格要容忍 assert.equal( buildDouyinMashidaClaimLinkPreview(' https://x.example.com/claim/ ', 'tok123'), 'https://x.example.com/claim/tok123', ) // 裸域名也允许(后端统一补 /token) assert.equal( buildDouyinMashidaClaimLinkPreview('https://x.example.com', 'tok123'), 'https://x.example.com/tok123', ) // 留空 → 不生成链接(后端回退到通用兑换链接) assert.equal(buildDouyinMashidaClaimLinkPreview(''), '') assert.equal(buildDouyinMashidaClaimLinkPreview(' '), '') }) test('领取链接基址误配提示:拦 # 与缺协议', () => { assert.equal(describeClaimLinkBaseWarning('https://x.example.com/claim'), '') assert.equal(describeClaimLinkBaseWarning(''), '') // # 是打手领取页,必须拦(真实踩坑点) const hashWarning = describeClaimLinkBaseWarning('https://x.example.com/#/claim') assert.match(hashWarning, /#/) assert.match(hashWarning, /打手/) const protocolWarning = describeClaimLinkBaseWarning('x.example.com/claim') assert.match(protocolWarning, /https:\/\//) }) test('发消息模板提示:没有 {link} 就等于没给买家链接', () => { assert.equal(describeMessageTemplateWarning('您的订单已发货,请点击领取:{link}'), '') assert.equal(describeMessageTemplateWarning(''), '') assert.match(describeMessageTemplateWarning('您的订单已发货'), /\{link\}/) }) test('resolveDouyinMashidaPublicOrigin:从已配置的 claimLinkBase 或 certLink 提取公网 origin', () => { assert.equal( resolveDouyinMashidaPublicOrigin('https://nonsecretly-posttracheal-amado.ngrok-free.dev/h5'), 'https://nonsecretly-posttracheal-amado.ngrok-free.dev', ) assert.equal( resolveDouyinMashidaPublicOrigin('', 'https://my-domain.com/h5'), 'https://my-domain.com', ) assert.equal( resolveDouyinMashidaPublicOrigin('', '', 'http://localhost:5173'), 'http://localhost:5173', ) })