64 lines
2.4 KiB
TypeScript
64 lines
2.4 KiB
TypeScript
import { describe, expect, it } from 'bun:test'
|
|
import {
|
|
detectEarthContextJson,
|
|
parseJsonOrTextContent,
|
|
validateHttpUrl,
|
|
} from '../src/admin/pages/advisorAttachments'
|
|
|
|
describe('advisorAttachments', () => {
|
|
it('auto-detects exported planet.earth_context.v1 JSON payloads', () => {
|
|
const raw = JSON.stringify({
|
|
schema: 'planet.earth_context.v1',
|
|
domain: 'subsea_cable',
|
|
selected: { entity_type: 'cable', label: 'SEA-ME-WE 5' },
|
|
targets: [{ entity_type: 'cable', entity_id: 'smw5', label: 'SEA-ME-WE 5' }],
|
|
turns: [{ role: 'user', content: 'Inspect outage' }],
|
|
})
|
|
const result = parseJsonOrTextContent(raw, { forceJson: true })
|
|
expect(result.ok).toBe(true)
|
|
expect(result.kind).toBe('json')
|
|
expect(result.earthContext.isEarthContext).toBe(true)
|
|
expect(result.earthContext.title).toBe('SEA-ME-WE 5')
|
|
expect(result.earthContext.summary).toContain('subsea_cable')
|
|
expect(result.earthContext.summary).toContain('1 targets')
|
|
})
|
|
|
|
it('auto-detects Earth IndexedDB / multi-signal objects without schema header', () => {
|
|
const detected = detectEarthContextJson({
|
|
title: '红海异常研判会话',
|
|
visible_layers: { cables: true, vessels: true },
|
|
memory_notes: ['关注曼德海峡 AIS 异常'],
|
|
})
|
|
expect(detected.isEarthContext).toBe(true)
|
|
expect(detected.title).toBe('红海异常研判会话')
|
|
})
|
|
|
|
it('treats generic JSON objects and arrays as normal JSON attachments', () => {
|
|
const normalObj = parseJsonOrTextContent('{"service":"api","status":"healthy","latency_ms":14}', {
|
|
forceJson: true,
|
|
})
|
|
expect(normalObj.ok).toBe(true)
|
|
expect(normalObj.kind).toBe('json')
|
|
expect(normalObj.earthContext.isEarthContext).toBe(false)
|
|
|
|
const normalArray = detectEarthContextJson([{ id: 1 }, { id: 2 }])
|
|
expect(normalArray.isEarthContext).toBe(false)
|
|
|
|
const invalidJson = parseJsonOrTextContent('{not valid json', { forceJson: true })
|
|
expect(invalidJson.ok).toBe(false)
|
|
expect(invalidJson.error).toBe('JSON syntax error')
|
|
})
|
|
|
|
it('validates HTTP/HTTPS URLs cleanly and rejects unsafe schemes', () => {
|
|
const bare = validateHttpUrl('example.com/report')
|
|
expect(bare.ok).toBe(true)
|
|
if (bare.ok) {
|
|
expect(bare.url).toBe('https://example.com/report')
|
|
expect(bare.host).toBe('example.com')
|
|
}
|
|
|
|
expect(validateHttpUrl('javascript:alert(1)').ok).toBe(false)
|
|
expect(validateHttpUrl(' ').ok).toBe(false)
|
|
})
|
|
})
|