mirror of
https://github.com/JohnRiceML/clawport-ui.git
synced 2026-08-14 08:51:58 +00:00
When OpenClaw CLI prints debug log lines (e.g. "[plugins] [debug] ...") to stdout before JSON, extractJson() failed because bracketed log lines look like JSON array starts. Now tries each candidate position until one actually parses. Also fixes the onboarding wizard to show a specific "CLI log output" error instead of the misleading "Could not reach gateway" message when /api/crons fails due to JSON parsing. Bumps to v0.8.6. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, it, expect } from 'vitest'
|
|
import { extractJson } from './cli-utils'
|
|
|
|
describe('extractJson', () => {
|
|
it('parses clean JSON array', () => {
|
|
const result = extractJson('[{"id":"main"}]')
|
|
expect(result).toEqual([{ id: 'main' }])
|
|
})
|
|
|
|
it('parses clean JSON object', () => {
|
|
const result = extractJson('{"name":"test"}')
|
|
expect(result).toEqual({ name: 'test' })
|
|
})
|
|
|
|
it('strips validation warnings before JSON array', () => {
|
|
const raw = `agents.defaults.memorySearch.query.hybrid: Unrecognized keys: "mmr", "temporalDecay"
|
|
commands: Unrecognized key: "ownerDisplay"
|
|
hooks: Unrecognized key: "allowedAgentIds"
|
|
[{"id":"main","workspace":"/home/user/.openclaw/workspace"}]`
|
|
const result = extractJson(raw)
|
|
expect(result).toEqual([{ id: 'main', workspace: '/home/user/.openclaw/workspace' }])
|
|
})
|
|
|
|
it('strips validation warnings before JSON object', () => {
|
|
const raw = `gateway: Unrecognized key: "allowRealIpFallback"
|
|
{"jobs":[{"name":"daily-report"}]}`
|
|
const result = extractJson(raw) as Record<string, unknown>
|
|
expect(result.jobs).toEqual([{ name: 'daily-report' }])
|
|
})
|
|
|
|
it('handles whitespace before JSON', () => {
|
|
const result = extractJson(' \n [1,2,3]')
|
|
expect(result).toEqual([1, 2, 3])
|
|
})
|
|
|
|
it('throws on output with no JSON', () => {
|
|
expect(() => extractJson('no json here')).toThrow('No JSON found')
|
|
})
|
|
|
|
it('throws on empty string', () => {
|
|
expect(() => extractJson('')).toThrow()
|
|
})
|
|
|
|
it('skips bracketed log lines like [plugins] before real JSON', () => {
|
|
const raw = `[plugins] [debug] Database schema initialized
|
|
[plugins] [debug] Loading context engine
|
|
{"jobs":[{"name":"daily-report","schedule":"0 8 * * *"}]}`
|
|
const result = extractJson(raw) as Record<string, unknown>
|
|
expect(result.jobs).toEqual([{ name: 'daily-report', schedule: '0 8 * * *' }])
|
|
})
|
|
|
|
it('skips bracketed log lines before JSON array', () => {
|
|
const raw = `[plugins] [debug] Database schema initialized
|
|
[plugins] [info] Ready
|
|
[{"id":"pulse","name":"daily-pulse"}]`
|
|
const result = extractJson(raw)
|
|
expect(result).toEqual([{ id: 'pulse', name: 'daily-pulse' }])
|
|
})
|
|
})
|