92 lines
3.7 KiB
TypeScript
92 lines
3.7 KiB
TypeScript
/**
|
||
* 「样例响应摊成可挑位置」这一层的口径检查。
|
||
*
|
||
* 最要命的两件事:摊出来的写法后端取不到(那就等于骗人),以及深响应把整栏挤爆。
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import { jsonValuePaths, linksFirst } from '@/utils/jsonPaths'
|
||
|
||
const SAMPLE = JSON.stringify({
|
||
code: 0,
|
||
message: 'ok',
|
||
data: {
|
||
url: 'https://erp.example.com/contract/edit?k=9f2c',
|
||
viewUrl: 'https://erp.example.com/contract/view?k=9f2c',
|
||
printUrl: '',
|
||
flow_number: 'ERP-2026-0001',
|
||
isReviewAllowed: true,
|
||
signer: null,
|
||
attachment: { name: '合同.pdf', size: 20480 }
|
||
}
|
||
})
|
||
|
||
describe('样例响应摊平成可挑的位置', () => {
|
||
it('按响应里的先后摊,路径用后端认的点写法', () => {
|
||
expect(jsonValuePaths(SAMPLE).map((item) => item.path)).toEqual([
|
||
'code',
|
||
'message',
|
||
'data.url',
|
||
'data.viewUrl',
|
||
'data.printUrl',
|
||
'data.flow_number',
|
||
'data.isReviewAllowed',
|
||
'data.signer',
|
||
'data.attachment.name',
|
||
'data.attachment.size'
|
||
])
|
||
})
|
||
|
||
it('示例值跟着摆在后面,认位置靠的就是它', () => {
|
||
const hit = jsonValuePaths(SAMPLE).find((item) => item.path === 'data.flow_number')
|
||
expect(hit?.preview).toBe('ERP-2026-0001')
|
||
})
|
||
|
||
it('数组用下标写', () => {
|
||
const paths = jsonValuePaths('{"data":{"rows":[{"link":"https://a.test/1"},{"link":"https://a.test/2"}]}}')
|
||
expect(paths.map((item) => item.path)).toEqual(['data.rows.0.link', 'data.rows.1.link'])
|
||
})
|
||
|
||
it('顶层就是数组的响应也摊得开', () => {
|
||
expect(jsonValuePaths('[{"url":"https://a.test/1"}]').map((item) => item.path)).toEqual(['0.url'])
|
||
})
|
||
|
||
it('样例里留空与 null 的位置照样摆出来:对方常常只在真调用时才填地址', () => {
|
||
const paths = jsonValuePaths(SAMPLE)
|
||
expect(paths.find((item) => item.path === 'data.printUrl')).toMatchObject({ preview: '', link: true })
|
||
expect(paths.find((item) => item.path === 'data.signer')).toMatchObject({ preview: 'null' })
|
||
})
|
||
|
||
it('值像地址、或者键名像地址字段的,标成链接', () => {
|
||
const paths = jsonValuePaths('{"a":"https://x.test/p","b":"/relative","c":"随便一句话","printURL":"","jump_link":"u"}')
|
||
expect(paths.filter((item) => item.link).map((item) => item.path)).toEqual(['a', 'printURL', 'jump_link'])
|
||
})
|
||
|
||
it('挑链接时把看着像地址的那几项挪到第一屏', () => {
|
||
const ordered = linksFirst(jsonValuePaths(SAMPLE)).map((item) => item.path)
|
||
expect(ordered.slice(0, 3)).toEqual(['data.url', 'data.viewUrl', 'data.printUrl'])
|
||
expect(ordered).toHaveLength(jsonValuePaths(SAMPLE).length)
|
||
})
|
||
|
||
it('非法 JSON、空文本、顶层是标量,都回空清单(调用方据此退回手输框)', () => {
|
||
expect(jsonValuePaths('')).toEqual([])
|
||
expect(jsonValuePaths(' ')).toEqual([])
|
||
expect(jsonValuePaths('{"code":0,')).toEqual([])
|
||
expect(jsonValuePaths('"https://erp.example.com/x"')).toEqual([])
|
||
})
|
||
|
||
it('深度封顶:明细行再深也不摊出十层长的路径', () => {
|
||
const deep = '{"a":{"b":{"c":{"d":{"e":{"f":"hit","g":{"h":"更深的明细"}}}}}}}'
|
||
expect(jsonValuePaths(deep).map((item) => item.path)).toEqual(['a.b.c.d.e.f'])
|
||
})
|
||
|
||
it('条数封顶:200 行流水不会挤爆那一栏', () => {
|
||
const rows = { data: Array.from({ length: 200 }, (_, index) => ({ url: `https://a.test/${index}` })) }
|
||
expect(jsonValuePaths(JSON.stringify(rows))).toHaveLength(60)
|
||
})
|
||
|
||
it('键名里带点或方括号的那一支整支跳过:后端 pick_path 切不开它', () => {
|
||
const paths = jsonValuePaths('{"a.b":1,"ok":{"c.d":2,"e":3},"fine":"y"}')
|
||
expect(paths.map((item) => item.path)).toEqual(['ok.e', 'fine'])
|
||
})
|
||
})
|