import assert from 'node:assert/strict' import test from 'node:test' import { ref } from 'vue' import { useContentMarkdownLists } from '../composables/useContentMarkdownLists.js' /** * 테스트용 목록 편집 상태를 만든다. * @param {string} contentText - Markdown 본문 * @returns {{ calls: Object, pendingFocusLine: import('vue').Ref, pendingFocusPosition: import('vue').Ref, state: Object }} */ const createListState = (contentText = '') => { const calls = { commits: [], inserts: [] } const pendingFocusLine = ref(null) const pendingFocusPosition = ref('auto') const state = useContentMarkdownLists({ content: ref(contentText), pendingFocusLine, pendingFocusPosition, commitInlineBlockLines: (block, replacementLines) => { calls.commits.push({ block, replacementLines }) }, insertBelowBlock: (block, options) => { calls.inserts.push({ block, options }) } }) return { calls, pendingFocusLine, pendingFocusPosition, state } } test('순서 목록 번호는 원본 줄 번호를 우선 사용한다', () => { const block = { type: 'list', ordered: true, meta: { startLine: 1, endLine: 2 } } const { state } = createListState(['제목', '4. 첫째', '5. 둘째'].join('\n')) assert.equal(state.getListMarkerNumber(block, 0), 4) assert.equal(state.getListMarkerNumber(block, 1), 5) }) test('목록 항목 커밋은 선택 항목만 마크다운 목록 줄로 갱신한다', () => { const block = { type: 'list', ordered: false, meta: { startLine: 0, endLine: 1 } } const { calls, state } = createListState(['- 기존', '- 다음'].join('\n')) state.onListItemInlineCommit(block, 0, '변경') assert.deepEqual(calls.commits[0], { block, replacementLines: ['- 변경', '- 다음'] }) }) test('목록 항목 중간 Enter는 현재 항목을 둘로 나누고 다음 줄에 포커스를 둔다', () => { const block = { type: 'list', ordered: false, meta: { startLine: 3, endLine: 4 } } const { calls, pendingFocusLine, pendingFocusPosition, state } = createListState([ 'a', 'b', 'c', '- 앞뒤', '- 다음' ].join('\n')) state.onListItemInsertBelow(block, 0, { before: '앞', after: '뒤', value: '앞뒤' }) assert.equal(pendingFocusLine.value, 4) assert.equal(pendingFocusPosition.value, 'start') assert.deepEqual(calls.commits[0].replacementLines, ['- 앞', '- 뒤', '- 다음']) }) test('빈 목록 마커 Enter는 해당 줄을 일반 문단 줄로 탈출시킨다', () => { const block = { type: 'list', ordered: false, meta: { startLine: 2, endLine: 2 } } const { calls, pendingFocusLine, pendingFocusPosition, state } = createListState([ 'a', 'b', '- ' ].join('\n')) state.onListItemInsertBelow(block, 0, '- ') assert.equal(pendingFocusLine.value, 2) assert.equal(pendingFocusPosition.value, 'start') assert.deepEqual(calls.commits[0].replacementLines, ['']) assert.equal(calls.inserts.length, 0) }) test('목록 항목 끝 Enter는 현재 줄 저장 후 아래 문단을 삽입한다', () => { const block = { type: 'list', ordered: true, listNumbers: [7], meta: { startLine: 0, endLine: 0 } } const { calls, pendingFocusLine, pendingFocusPosition, state } = createListState('7. 기존') state.onListItemInsertBelow(block, 0, { value: '완료', before: '완료', after: '', caretAtEnd: true }) assert.equal(pendingFocusLine.value, 1) assert.equal(pendingFocusPosition.value, 'start') assert.deepEqual(calls.commits[0].replacementLines, ['7. 완료']) assert.deepEqual(calls.inserts[0], { block, options: { lines: [''] } }) }) test('원문 모드 목록 커밋은 이미 있는 마커를 그대로 보존한다', () => { const block = { type: 'list', ordered: false, meta: { startLine: 0, endLine: 0 } } const { calls, state } = createListState('- 기존') state.onListItemInlineCommit(block, 0, { value: '* 원문', raw: true }) assert.deepEqual(calls.commits[0].replacementLines, ['* 원문']) })