73 lines
2.9 KiB
TypeScript
73 lines
2.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { headLines } from './pathAskContext'
|
|
import { PATH_ASK_HEAD_LINES, pathAskPrompt } from './pathAskPrompt'
|
|
|
|
const FILE = 'app/[[...section]]/page.tsx'
|
|
const FOLDER = 'app/[[...section]]'
|
|
|
|
describe('问路径时发给模型的话', () => {
|
|
it('文件:写明是文件,带相对路径和开头', () => {
|
|
const prompt = pathAskPrompt({
|
|
kind: 'file',
|
|
relativePath: FILE,
|
|
head: { lines: ['export default function Page() {', ' return null', '}'], truncated: false },
|
|
}, '这个名字是什么意思')
|
|
expect(prompt).toContain(`这是一个文件,相对路径:${FILE}`)
|
|
expect(prompt).toContain('1 | export default function Page() {')
|
|
expect(prompt).toContain('3 | }')
|
|
expect(prompt).toContain('问题:这个名字是什么意思')
|
|
expect(prompt).toContain('只读,不要改任何东西')
|
|
expect(prompt).not.toContain('剩下的需要时自己读')
|
|
})
|
|
|
|
it('文件开头超过上限时说明只贴了开头,相对路径仍在', () => {
|
|
const lines = Array.from({ length: PATH_ASK_HEAD_LINES }, (_, index) => `line ${index}`)
|
|
const prompt = pathAskPrompt({
|
|
kind: 'file',
|
|
relativePath: FILE,
|
|
head: { lines, truncated: true },
|
|
}, '?')
|
|
expect(prompt).toContain(`这是一个文件,相对路径:${FILE}`)
|
|
expect(prompt).toContain(`上面只贴了开头 ${PATH_ASK_HEAD_LINES} 行`)
|
|
expect(prompt).toContain(`80 | line ${PATH_ASK_HEAD_LINES - 1}`)
|
|
expect(prompt).not.toContain('line 80')
|
|
})
|
|
|
|
it('文件夹:写明是文件夹,带相对路径,只列这一层的名字', () => {
|
|
const prompt = pathAskPrompt({
|
|
kind: 'folder',
|
|
relativePath: FOLDER,
|
|
children: [
|
|
{ name: 'components', kind: 'folder' },
|
|
{ name: 'page.tsx', kind: 'file' },
|
|
],
|
|
}, '[[...section]] 是啥意思')
|
|
expect(prompt).toContain(`这是一个文件夹,相对路径:${FOLDER}`)
|
|
expect(prompt).toContain('- 文件夹 components\n- 文件 page.tsx')
|
|
expect(prompt).toContain('只列了这一层的名字')
|
|
expect(prompt).toContain('问题:[[...section]] 是啥意思')
|
|
expect(prompt).not.toContain('export default')
|
|
})
|
|
})
|
|
|
|
describe('文件开头截取', () => {
|
|
it('超过行数上限时只留前 80 行,并标明没贴完', () => {
|
|
const text = Array.from({ length: 100 }, (_, index) => `L${index}`).join('\n')
|
|
expect(headLines(text, false, PATH_ASK_HEAD_LINES)).toEqual({
|
|
lines: Array.from({ length: PATH_ASK_HEAD_LINES }, (_, index) => `L${index}`),
|
|
truncated: true,
|
|
})
|
|
})
|
|
|
|
it('字节没读完时丢掉被截断的最后一行', () => {
|
|
expect(headLines('alpha\nbeta-cut', true, PATH_ASK_HEAD_LINES)).toEqual({
|
|
lines: ['alpha'],
|
|
truncated: true,
|
|
})
|
|
})
|
|
|
|
it('空文件没有行', () => {
|
|
expect(headLines('', false, PATH_ASK_HEAD_LINES)).toEqual({ lines: [], truncated: false })
|
|
})
|
|
})
|