Files
clawport-ui/lib/cli-utils.test.ts
T
JohnRiceMLandClaude Opus 4.6 7c44a6d969 fix: onboarding misreports cron parse failure as gateway error (#23)
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>
2026-03-23 10:18:00 -05:00

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' }])
})
})