test(35-03): add failing tests for codeeditor field

- Browser smoke: Tab leaves, Mod+S/Mod+Enter save, Esc does not cancel
- Happy-dom: loading box, read-only pre, chunk-failure textarea fallback
- Stub field/editor so imports resolve; assertions fail until GREEN

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-13 13:24:19 +01:00
parent ee0bbc27f8
commit 7e2c01172f
4 changed files with 269 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
// Stub for TDD RED (35-03 Task 4): real CodeMirror factory lands in GREEN.
export interface CodeEditorOptions {
parent: HTMLElement
doc: string
language: 'html' | 'css' | 'text'
height: number
attributes: Record<string, string>
onChange: (doc: string) => void
}
export interface CodeEditorHandle {
view: unknown
insert(text: string): void
destroy(): void
setDoc(doc: string): void
}
/** Builds the field's CodeMirror editor on options.parent. */
export function createCodeEditor(_options: CodeEditorOptions): CodeEditorHandle {
throw new Error('codeeditor not implemented')
}

View File

@@ -0,0 +1,13 @@
<script setup lang="ts">
// Stub for TDD RED (35-03 Task 4): loading, lazy CodeMirror, fallback and
// read-only rendering land in GREEN. Emits nothing and shows an empty box so
// the planned behavior assertions fail intentionally.
import type { FieldControlProps } from '../control'
defineProps<FieldControlProps>()
defineEmits<{ 'update:modelValue': [value: string] }>()
</script>
<template>
<div data-codeeditor-stub class="rounded-control border border-border bg-surface" />
</template>

View File

@@ -0,0 +1,135 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
import type { FormField } from '../../src/api/types'
import { installHotkeys } from '../../src/app/hotkeys'
import { setBundle } from '../../src/app/i18n'
import CodeEditorField from '../../src/components/form/fields/CodeEditorField.vue'
import { langFixture } from '../fixtures/typed'
import { press } from '../helpers'
// Browser smoke for the codeeditor field (Phase 35 UI-SPEC C1): Tab leaves the
// editor, Mod+S and Mod+Enter reach form save hotkeys, Esc does not Cancel.
// Needs a real engine so contenteditable focus and key routing match production.
enableAutoUnmount(afterEach)
const codeeditorBundle = {
...langFixture.data,
'backend::lang.codeeditor.load_failed': {
other: 'The code editor could not be loaded. You can still edit the text here.',
},
'backend::lang.codeeditor.toolbar': { other: 'Code editor tools' },
'backend::lang.list.empty_value': { other: '—' },
}
function field(overrides: Partial<FormField> = {}): FormField {
return {
name: 'content_html',
label: 'HTML',
type: 'codeeditor',
language: 'html',
size: 'large',
...overrides,
} as FormField
}
async function editorReady(root: HTMLElement): Promise<HTMLElement> {
return vi.waitFor(() => {
const content = root.querySelector<HTMLElement>('.cm-content')
expect(content).not.toBeNull()
return content!
})
}
beforeEach(() => {
setBundle(codeeditorBundle, 'en')
})
afterEach(() => {
document.body.innerHTML = ''
})
describe('codeeditor keyboard in a real browser engine', () => {
it('Tab leaves the editor; Mod+S and Mod+Enter save; Esc does not cancel', async () => {
const uninstall = installHotkeys()
const saveClicks: string[] = []
const cancelClicks: string[] = []
const form = document.createElement('form')
form.setAttribute('role', 'form')
document.body.appendChild(form)
const next = document.createElement('input')
next.id = 'next-control'
next.type = 'text'
const save = document.createElement('button')
save.type = 'button'
save.setAttribute('data-hotkey', 'mod+s mod+enter')
save.addEventListener('click', () => saveClicks.push('save'))
const cancel = document.createElement('button')
cancel.type = 'button'
cancel.setAttribute('data-hotkey', 'escape')
cancel.addEventListener('click', () => cancelClicks.push('cancel'))
const host = document.createElement('div')
form.append(host, next, save, cancel)
const wrapper = mount(CodeEditorField, {
props: {
field: field(),
modelValue: 'Hello {{ name }}',
controlId: 'f-html',
},
attachTo: host,
})
await flushPromises()
const content = await editorReady(wrapper.element as HTMLElement)
content.focus()
expect(document.activeElement).toBe(content)
// Tab must leave the editor (no indentWithTab / no trap).
content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true }))
// Native Tab focus movement: advance to the next control.
next.focus()
expect(document.activeElement).toBe(next)
content.focus()
expect(press(content, 's', { ctrlKey: true }).defaultPrevented).toBe(true)
expect(saveClicks).toEqual(['save'])
saveClicks.length = 0
expect(press(content, 'Enter', { ctrlKey: true }).defaultPrevented).toBe(true)
expect(saveClicks).toEqual(['save'])
expect(press(content, 'Escape').defaultPrevented).toBe(false)
expect(cancelClicks).toEqual([])
uninstall()
wrapper.unmount()
})
it('typing emits update:modelValue with the full document', async () => {
const wrapper = mount(CodeEditorField, {
props: {
field: field({ language: 'text' }),
modelValue: 'start',
controlId: 'f-text',
},
attachTo: document.body,
})
await flushPromises()
const content = await editorReady(wrapper.element as HTMLElement)
content.focus()
// Insert at end via beforeinput/input so CodeMirror updates the doc.
document.execCommand('selectAll')
document.execCommand('insertText', false, 'changed')
await flushPromises()
const emitted = wrapper.emitted('update:modelValue')
expect(emitted).toBeTruthy()
expect(emitted!.at(-1)).toEqual(['changed'])
})
})

View File

@@ -10,6 +10,14 @@ import { clearToasts, toasts } from '../../src/state/useToasts'
import { API, mockApi, profile, requestsTo, resetState, type Reply, wait } from '../helpers'
import { langFixture } from '../fixtures/typed'
const codeeditorBundle = {
'backend::lang.codeeditor.load_failed': {
other: 'The code editor could not be loaded. You can still edit the text here.',
},
'backend::lang.codeeditor.toolbar': { other: 'Code editor tools' },
'backend::lang.list.empty_value': { other: '—' },
}
const mailPreviewBundle = {
...langFixture.data,
'backend::lang.mail.preview.html': { other: 'HTML' },
@@ -209,3 +217,95 @@ describe('MailPreviewField', () => {
expect(toasts.value[0]?.text).toContain('dev@example.test')
})
})
function codeField(overrides: Partial<FormField> = {}): FormField {
return {
name: 'content_html',
label: 'HTML',
type: 'codeeditor',
language: 'html',
size: 'large',
...overrides,
} as FormField
}
describe('CodeEditorField loading and fallback', () => {
beforeEach(() => {
setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en')
})
it('shows the bg-skel loading box at the size height while the chunk loads', async () => {
// Hold the dynamic import open so the loading box stays visible.
let release!: () => void
const gate = new Promise<void>((resolve) => {
release = resolve
})
vi.resetModules()
vi.doMock('../../src/components/form/codeeditor/editor', async () => {
await gate
return await vi.importActual('../../src/components/form/codeeditor/editor')
})
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
const wrapper = mount(Field, {
props: { field: codeField(), modelValue: '<p>x</p>', controlId: 'f-html' },
attachTo: document.body,
})
await flushPromises()
const loading = wrapper.find('[data-codeeditor-loading]')
expect(loading.exists()).toBe(true)
expect(loading.attributes('aria-busy')).toBe('true')
expect((loading.element as HTMLElement).style.height).toBe('320px')
release()
await flushPromises()
wrapper.unmount()
vi.doUnmock('../../src/components/form/codeeditor/editor')
})
it('renders a read-only pre without loading CodeMirror', async () => {
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
const wrapper = mount(Field, {
props: {
field: codeField({ readOnly: true }),
modelValue: 'layout {{ name }}',
controlId: 'f-html',
},
attachTo: document.body,
})
await flushPromises()
const pre = wrapper.find('pre')
expect(pre.exists()).toBe(true)
expect(pre.text()).toBe('layout {{ name }}')
expect(wrapper.find('[data-codeeditor-loading]').exists()).toBe(false)
expect(wrapper.find('.cm-editor').exists()).toBe(false)
})
})
// Last in the file: it replaces the editor chunk for the rest of the run.
describe('CodeEditorField chunk failure', () => {
afterEach(() => {
vi.doUnmock('../../src/components/form/codeeditor/editor')
})
it('shows the load_failed note and a monospace textarea that still emits', async () => {
vi.resetModules()
vi.doMock('../../src/components/form/codeeditor/editor', () => {
throw new Error('chunk failed')
})
;(await import('../../src/app/i18n')).setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en')
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
const wrapper = mount(Field, {
props: { field: codeField(), modelValue: '<p>Stored</p>', controlId: 'f-html' },
attachTo: document.body,
})
await vi.waitFor(() => {
expect(wrapper.find('[data-codeeditor-load-failed]').exists()).toBe(true)
})
expect(wrapper.find('[data-codeeditor-load-failed]').text()).toContain('could not be loaded')
const textarea = wrapper.find<HTMLTextAreaElement>('textarea')
expect(textarea.exists()).toBe(true)
expect(textarea.element.value).toBe('<p>Stored</p>')
expect(textarea.attributes('id')).toBe('f-html')
await textarea.setValue('<p>Edited</p>')
expect(wrapper.emitted('update:modelValue')).toEqual([['<p>Edited</p>']])
})
})