// The terminal and the keyboard: the text a terminal keeps, the codes keys // type, and both running in the real editor, a button strobing them. import * as vue from 'vue' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { compile } from '../app/lib/logic/compile' import { DATA_KEYS, keyCode, putChar } from '../app/lib/logic/devices' import { createProject, lookup } from '../app/lib/logic/ops' import { Simulator } from '../app/lib/logic/sim' import { saveProject, setCurrentId } from '../app/lib/logic/storage' import type { ComponentDef, Project } from '../app/lib/logic/types' const type = (s: string, cols = 32) => [...s].reduce((lines, c) => putChar(lines, c.charCodeAt(0), cols), ['']) describe('terminal text', () => { it('prints, breaks lines, goes back, clears', () => { expect(type('Hi\nyou')).toEqual(['Hi', 'you']) expect(type('abc\b\bX')).toEqual(['aX']) expect(type('abcdef', 4)).toEqual(['abcd', 'ef']) expect(type('a\tb')).toEqual(['a b']) expect(type('one\rtwo')).toEqual(['two']) expect(type('junk\fok')).toEqual(['ok']) expect(type('\x01\x07x')).toEqual(['x']) expect(putChar(['é'], 0xe9)).toEqual(['éé']) }) it('keys type their codes', () => { expect(keyCode({ key: 'a' })).toBe(97) expect(keyCode({ key: 'Enter' })).toBe(10) expect(keyCode({ key: 'Backspace' })).toBe(8) expect(keyCode({ key: 'c', ctrlKey: true })).toBe(3) expect(keyCode({ key: 'ArrowUp' })).toBeNull() expect(keyCode({ key: '😀' })).toBeNull() }) }) /** A keyboard wired straight to a terminal; button W writes the waiting key and takes it, C clears. */ function echoProject(): Project { const p = createProject({ examples: false }) const def: ComponentDef = { id: 'echo', name: 'Echo', symbol: 'box', nodes: [ { id: 'kb', kind: 'keyboard', x: 0, y: 0 }, { id: 'tm', kind: 'terminal', x: 12, y: 0 }, { id: 'w', kind: 'button', x: 0, y: 14, label: 'W' }, { id: 'c', kind: 'button', x: 0, y: 16, label: 'C' }, ], wires: [ ...DATA_KEYS.map(k => ({ id: `d-${k}`, from: { node: 'kb', pin: k }, to: { node: 'tm', pin: k } })), { id: 'wr', from: { node: 'w', pin: 'q' }, to: { node: 'tm', pin: 'wr' } }, { id: 'nx', from: { node: 'w', pin: 'q' }, to: { node: 'kb', pin: 'next' } }, { id: 'cl', from: { node: 'c', pin: 'q' }, to: { node: 'tm', pin: 'clr' } }, ], } p.components.push(def) p.openId = def.id return p } let store: Map beforeEach(() => { store = new Map() vi.stubGlobal('localStorage', { getItem: (k: string) => store.get(k) ?? null, setItem: (k: string, v: string) => void store.set(k, String(v)), removeItem: (k: string) => void store.delete(k), }) vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date'] }) }) afterEach(() => { vi.useRealTimers() vi.unstubAllGlobals() }) async function openEditor(p: Project) { saveProject({ id: 'p1', name: p.name, updated: Date.now() }, p) setCurrentId('p1') const on = () => {} vi.stubGlobal('window', { addEventListener: on }) vi.stubGlobal('document', { addEventListener: on, visibilityState: 'visible' }) vi.stubGlobal('location', { pathname: '/', origin: 'http://localhost' }) vi.stubGlobal('history', { state: null, replaceState() {} }) for (const k of ['ref', 'computed', 'reactive', 'shallowRef', 'toRaw', 'watch', 'nextTick'] as const) vi.stubGlobal(k, vue[k]) vi.resetModules() const { useEditor } = await import('../app/composables/useEditor') return useEditor() } describe('keyboard and terminal in the editor', () => { it('a rising WR writes the waiting key; NEXT takes it; CLR clears', async () => { const ed = await openEditor(echoProject()) vi.advanceTimersByTime(100) const click = (label: string) => { const n = ed.def.value.nodes.find(x => x.label === label)! ed.press(n, true) ed.step() ed.press(n, false) ed.step() } ed.typeInto('kb') for (const c of 'Hi!') ed.typeKey(c.charCodeAt(0)) ed.typeKey(10) ed.typeKey('o'.charCodeAt(0)) ed.sim.value!.run() expect(ed.val('kb', 'rdy')).toBe(1) for (let i = 0; i < 5; i++) click('W') expect(ed.screens.value.get('tm')).toEqual(['Hi!', 'o']) expect(ed.keyQueues.value.get('kb')).toEqual([]) expect(ed.val('kb', 'rdy')).toBe(0) // Nothing waiting: a strobe writes code 0, which shows nothing. click('W') expect(ed.screens.value.get('tm')).toEqual(['Hi!', 'o']) click('C') expect(ed.screens.value.get('tm')).toEqual(['']) }) it('inside a component a keyboard gives 0 and a terminal does nothing', () => { const p = echoProject() p.components.push({ id: 'outer', name: 'Outer', symbol: 'box', nodes: [{ id: 'e', kind: 'chip', chip: 'echo', x: 0, y: 0 }], wires: [] }) const s = new Simulator(compile(lookup(p), 'outer')) s.run() expect(s.nl.sources.size).toBe(0) }) })