fix: anima-extract 支援 wiki URL 與自然語言參數,並依序逐章處理
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
1ce475024d
commit
74a5d4949c
@@ -1,5 +1,6 @@
|
||||
import { expect, mock, test } from 'claude-code/testing'
|
||||
|
||||
import { parseWikiPages, parseWikiSource } from './extract'
|
||||
import type { Extracted } from './extract'
|
||||
|
||||
const char = (over: Record<string, unknown> = {}, tone = '溫柔'): Extracted['characters'][number] =>
|
||||
@@ -21,6 +22,7 @@ let cloneMissing = false
|
||||
let registered: string[] = []
|
||||
const probed = new Set<string>()
|
||||
let sessionCwd = '/repo'
|
||||
const web = new Map<string, string>()
|
||||
const setup = (on: never, answers: string[], files = new Map<string, string>()): Setup => {
|
||||
const s: Setup = { files, cmds: [], prompts: [] }
|
||||
const o = on as unknown as (n: string, h: (...a: never[]) => unknown) => void
|
||||
@@ -39,6 +41,10 @@ const setup = (on: never, answers: string[], files = new Map<string, string>()):
|
||||
o('process.run', ((_$: unknown, e: { argv: string[]; }) => {
|
||||
s.cmds.push(e.argv)
|
||||
const j = e.argv.join(' ')
|
||||
if (e.argv[0] === 'curl') {
|
||||
const body = web.get(e.argv[e.argv.length - 1]!)
|
||||
return { value: { exitCode: body === undefined ? 22 : 0, stdout: body ?? '', stderr: '' } }
|
||||
}
|
||||
const out = j.includes('--abbrev-ref') ? 'issue-4' : j.includes('symbolic-ref') ? 'origin/main' : j.includes('get-url') ? 'https://gitea.jsc.idv.tw/jiantw83/jsc-anima.git' : j.startsWith('tea logins') ? 'admin https://gitea.jsc.idv.tw u' : j.startsWith('tea pulls') ? 'PR#1' : ''
|
||||
return { value: { exitCode: 0, stdout: out, stderr: '' } }
|
||||
}) as never)
|
||||
@@ -182,3 +188,55 @@ test('合併並更新外掛後:啟動時從外掛目錄的 index.json 註冊 {
|
||||
await $.session.start({ source: 'startup', cwd: '/anywhere' } as never)
|
||||
expect(registered).toContain('ts-aoi')
|
||||
})
|
||||
|
||||
const WIKI = 'https://gitea.jsc.idv.tw/jiantw83/sao/wiki'
|
||||
const listHtml = ['Home', '第10章', '第2章', '第1章', '_pages']
|
||||
.map(n => `<a href="/jiantw83/sao/wiki/${encodeURIComponent(n)}">${n}</a>`)
|
||||
.join('') + '<a href="/jiantw83/sao">repo</a><a href="/jiantw83/sao/wiki/第1章/_revision/abc">rev</a>'
|
||||
|
||||
test('parseWikiSource:從自然語言中取出 wiki URL,頁面清單與單頁分開判斷', () => {
|
||||
const src = parseWikiSource(`請幫我依照順序逐章處理 ${WIKI}/?action=_pages 謝謝`)
|
||||
expect(src).toMatchObject({ kind: 'list', base: WIKI })
|
||||
expect(parseWikiSource(`處理 ${WIKI}/第2章。`)).toMatchObject({ kind: 'page', page: '第2章', base: WIKI })
|
||||
expect(parseWikiSource('ch1.txt')).toBeUndefined()
|
||||
expect(parseWikiSource('請處理 ch 1.txt')).toBeUndefined()
|
||||
})
|
||||
|
||||
test('parseWikiPages:略過 Home/_pages/子路徑,並依章節數字自然排序', () => {
|
||||
expect(parseWikiPages(listHtml, WIKI)).toEqual(['第1章', '第2章', '第10章'])
|
||||
})
|
||||
|
||||
test('anima-extract 帶 URL 與自然語言:不當檔案路徑,依序逐章處理', async ($, on) => {
|
||||
web.clear()
|
||||
web.set(`${WIKI}/?action=_pages`, listHtml)
|
||||
for (const n of ['第1章', '第2章', '第10章']) web.set(`${WIKI}/raw/${encodeURIComponent(n)}`, `${n}內容`)
|
||||
const s = setup(on as never, [reply(char()), reply(char()), reply(char())])
|
||||
await $.session.start({ source: 'startup', cwd: '/repo' } as never)
|
||||
const t1 = await run($ as never, `請幫我依照順序逐章處理 ${WIKI}/?action=_pages`)
|
||||
expect(t1).not.toContain('找不到檔案')
|
||||
expect(t1).toContain('共 3 章:第1章、第2章、第10章')
|
||||
expect(t1).toContain('章節「第1章」')
|
||||
expect(s.prompts[0]).toContain('第1章內容')
|
||||
await run($ as never, 'confirm')
|
||||
const t3 = await run($ as never, 'confirm')
|
||||
expect(t3).toContain('已建立 PR')
|
||||
expect(t3).toContain('剩餘 1 章')
|
||||
expect(t3).toContain('章節「第2章」')
|
||||
expect(s.prompts[1]).toContain('第2章內容')
|
||||
await run($ as never, 'confirm')
|
||||
const t5 = await run($ as never, 'confirm')
|
||||
expect(t5).toContain('章節「第10章」')
|
||||
await run($ as never, 'confirm')
|
||||
const t7 = await run($ as never, 'confirm')
|
||||
expect(t7).toContain('已建立 PR')
|
||||
expect(t7).not.toContain('接著處理')
|
||||
})
|
||||
|
||||
test('wiki 章節讀不到時回報原因與剩餘章數;cancel 清除佇列', async ($, on) => {
|
||||
web.clear()
|
||||
web.set(`${WIKI}/?action=_pages`, listHtml)
|
||||
setup(on as never, [])
|
||||
await $.session.start({ source: 'startup', cwd: '/repo' } as never)
|
||||
expect(await run($ as never, `${WIKI}/?action=_pages`)).toContain('無法讀取 wiki 章節「第1章」')
|
||||
expect(await run($ as never, 'cancel')).toContain('已取消')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user