mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
fix(mobile): Send on Enter second review fixes
- Never send a held answer as text + CR in one write: Codex folds an Enter in the same burst into a paste. A single character is typed, then CR ~120 ms later to the session captured at Enter; anything longer goes through _sendComposedPrompt. Drops HELD_DIRECT_MAX. No session keeps the text in the field. - Cap #cjkInput at 68px on every device, not only touch. - _autoGrow adds the border height and skips a hidden field. - setHoldUntilEnter returns early when the mode is unchanged. - Per-session held drafts: stashed on switch and tile focus, restored on return, dropped when the session is removed. - Toolbar Enter presses Enter in the visible CJK field (commits an active composition first). - Blank-only field sends a bare Enter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
95fb147e9c
commit
64ee504247
10 files changed
+418
-92
No files matched your search
@@ -53,8 +53,9 @@ describe('a held draft survives a reload of the same session', () => {
|
||||
// The REAL _cleanupPreviousSession from app.js. selectSession() nulls
|
||||
// activeSessionId before calling it on a same-session forceReload (tapping the
|
||||
// tab already shown, the geometry retry), so it is told through keepCjkDraft.
|
||||
function cleanup(hold: boolean, keepCjkDraft: boolean) {
|
||||
function cleanup(hold: boolean, keepCjkDraft: boolean, outgoing: string | null = null) {
|
||||
const clear = vi.fn();
|
||||
const stashDraft = vi.fn();
|
||||
const context = vm.createContext({
|
||||
console,
|
||||
setTimeout,
|
||||
@@ -70,20 +71,21 @@ describe('a held draft survives a reload of the same session', () => {
|
||||
localStorage: { length: 0, key: vi.fn(), getItem: vi.fn(), setItem: vi.fn(), removeItem: vi.fn() },
|
||||
window: { addEventListener: vi.fn(), removeEventListener: vi.fn() },
|
||||
MobileDetection: { isTouchDevice: () => true, isHandheldDevice: () => true, getDeviceType: () => 'mobile' },
|
||||
CjkInput: { holdUntilEnter: hold, clear },
|
||||
CjkInput: { holdUntilEnter: hold, clear, stashDraft },
|
||||
});
|
||||
vm.runInContext(`${read('constants.js')}\n${read('app.js')}\nglobalThis.__CodemanApp = CodemanApp;`, context);
|
||||
const CodemanApp = (context as unknown as { __CodemanApp: { prototype: object } }).__CodemanApp;
|
||||
const app = Object.create(CodemanApp.prototype) as Record<string, unknown> & {
|
||||
_cleanupPreviousSession(id: string, opts?: { keepCjkDraft?: boolean }): void;
|
||||
};
|
||||
app.activeSessionId = null;
|
||||
app.activeSessionId = outgoing;
|
||||
app.sessions = new Map();
|
||||
app._clearTimer = vi.fn();
|
||||
app._disconnectWs = vi.fn();
|
||||
app._xtermSnapshots = new Map();
|
||||
app.pendingWrites = [];
|
||||
app._cleanupPreviousSession('s1', { keepCjkDraft });
|
||||
return clear;
|
||||
return Object.assign(clear, { stashDraft });
|
||||
}
|
||||
|
||||
it('keeps the draft on a same-session reload in Send on Enter mode', () => {
|
||||
@@ -98,6 +100,41 @@ describe('a held draft survives a reload of the same session', () => {
|
||||
expect(cleanup(false, true)).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('stashes the held draft of the session being left before clearing', () => {
|
||||
const clear = cleanup(true, false, 's0');
|
||||
expect(clear.stashDraft).toHaveBeenCalledWith('s0');
|
||||
expect(clear.stashDraft.mock.invocationCallOrder[0]).toBeLessThan(clear.mock.invocationCallOrder[0]);
|
||||
});
|
||||
|
||||
it('selectSession restores the draft of the session it lands on', () => {
|
||||
expect(read('app.js')).toContain(
|
||||
"this.activeSessionId = sessionId;\n if (typeof CjkInput !== 'undefined') CjkInput.restoreDraft?.(sessionId);"
|
||||
);
|
||||
});
|
||||
|
||||
it('leaving the tile grid for another session stashes the focused tile draft', () => {
|
||||
// selectSession nulls activeSessionId there, which hides the tile from
|
||||
// _cleanupPreviousSession.
|
||||
expect(read('app.js')).toMatch(
|
||||
/CjkInput\.stashDraft\?\.\(this\.activeSessionId\);\n(?:\s*\/\/.*\n)*\s*this\.activeSessionId = null;/
|
||||
);
|
||||
});
|
||||
|
||||
it('tile focus stashes the draft of the tile left and restores the focused one', () => {
|
||||
const tiles = read('tile-grid.js');
|
||||
const start = tiles.indexOf(' _selectTiledSession(sessionId, options = {}) {');
|
||||
const body = tiles.slice(start, tiles.indexOf('grid.focusedId = sessionId;', start));
|
||||
expect(body).toMatch(
|
||||
/CjkInput\.stashDraft\?\.\(this\.activeSessionId\);\s*CjkInput\.clear\(\);\s*\}\s*CjkInput\.restoreDraft\?\.\(sessionId\);\s*\}\s*this\.activeSessionId = sessionId;/
|
||||
);
|
||||
});
|
||||
|
||||
it('a removed session drops its stashed draft', () => {
|
||||
expect(read('app.js')).toMatch(
|
||||
/discardComposerDraft\?\.\(sessionId\);\n\s*if \(typeof CjkInput !== 'undefined'\) CjkInput\.discardDraft\?\.\(sessionId\);/
|
||||
);
|
||||
});
|
||||
|
||||
it('selectSession passes the same-session flag it captured before nulling activeSessionId', () => {
|
||||
const appJs = read('app.js');
|
||||
expect(appJs).toMatch(
|
||||
@@ -115,6 +152,24 @@ describe('Send on Enter field layout and strings', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('caps the field on every device, not only in the .touch-device rule', () => {
|
||||
// The setting is not limited to phones; on desktop an uncapped field grew to
|
||||
// 502px at 25 lines and resized the PTY on every step.
|
||||
const css = read('styles.css');
|
||||
const base = css.slice(css.indexOf('#cjkInput {'), css.indexOf('}', css.indexOf('#cjkInput {')));
|
||||
expect(base).toContain('max-height: 68px;');
|
||||
expect(base).toContain('overflow-y: auto;');
|
||||
});
|
||||
|
||||
it('the phone toolbar Enter presses Enter in the visible CJK field', () => {
|
||||
// onData drops everything while cjkActive, so the xterm path does nothing.
|
||||
const sessionUi = read('session-ui.js');
|
||||
const start = sessionUi.indexOf(' sendEnterKey() {');
|
||||
const body = sessionUi.slice(start, sessionUi.indexOf('triggerDataEvent', start));
|
||||
expect(body).toContain("classList.contains('cjk-input-visible')");
|
||||
expect(body).toContain('CjkInput.submit?.();');
|
||||
});
|
||||
|
||||
it('has zh-CN strings for both field placeholders', () => {
|
||||
const i18n = read('i18n.js');
|
||||
const cjk = read('input-cjk.js');
|
||||
|
||||
+153
-32
@@ -21,14 +21,18 @@ function makeTextarea() {
|
||||
placeholder: '',
|
||||
style: { height: '' } as Record<string, string>,
|
||||
scrollHeight: 34,
|
||||
// border-box with a 1px border: offsetHeight - clientHeight is the borders.
|
||||
offsetHeight: 36,
|
||||
clientHeight: 34,
|
||||
attrs,
|
||||
blur: () => {},
|
||||
focus: () => {},
|
||||
blur: vi.fn(),
|
||||
focus: vi.fn(),
|
||||
getClientRects: (): unknown[] => [{}],
|
||||
classList: { contains: (c: string) => c === 'cjk-input-visible' },
|
||||
setSelectionRange: () => {},
|
||||
setAttribute: (k: string, v: string) => {
|
||||
setAttribute: vi.fn((k: string, v: string) => {
|
||||
attrs[k] = v;
|
||||
},
|
||||
}),
|
||||
addEventListener: (ev: string, fn: (e?: unknown) => void) => {
|
||||
listeners.set(ev, [...(listeners.get(ev) ?? []), fn]);
|
||||
},
|
||||
@@ -136,43 +140,38 @@ describe('CJK input: Send on Enter', () => {
|
||||
expect(textarea.value).toBe(PHANTOM + 'the');
|
||||
});
|
||||
|
||||
it('sends one line as text + CR on Enter, then clears the field', () => {
|
||||
const { textarea, sent } = load();
|
||||
it('sends one line on Enter through the prompt path, then clears the field', () => {
|
||||
const { textarea, sent, prompts } = load();
|
||||
type(textarea, 'yes, use Postgres');
|
||||
const preventDefault = vi.fn();
|
||||
textarea.fire('keydown', { key: 'Enter', keyCode: 13, shiftKey: false, preventDefault });
|
||||
expect(preventDefault).toHaveBeenCalled();
|
||||
expect(sent).toEqual(['yes, use Postgres\r']);
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
});
|
||||
|
||||
it('sends a digit as typed so CLI menus still answer to it', () => {
|
||||
const { textarea, sent } = load();
|
||||
type(textarea, '2');
|
||||
textarea.fire('keydown', { key: 'Enter', keyCode: 13, shiftKey: false });
|
||||
expect(sent).toEqual(['2\r']);
|
||||
});
|
||||
|
||||
it('sends a long single line as a prompt, never as one <text>\\r burst', () => {
|
||||
// Claude Code takes a burst of ~100+ characters as a paste and its \r as a
|
||||
// newline, so the answer would sit unsent (src/web/route-helpers.ts).
|
||||
const { textarea, sent, prompts } = load();
|
||||
const long = 'x'.repeat(150);
|
||||
type(textarea, long);
|
||||
enter(textarea);
|
||||
expect(prompts).toEqual(['yes, use Postgres']);
|
||||
expect(sent).toEqual([]);
|
||||
expect(prompts).toEqual([long]);
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
});
|
||||
|
||||
it('keeps a line at the direct-write limit as one write, one over it as a prompt', () => {
|
||||
it('never writes text and Enter together, whatever the length', () => {
|
||||
// Codex takes characters arriving in one burst as a paste and folds an Enter
|
||||
// inside it into the paste, so even `1\r` in one write stays unsent; Claude
|
||||
// Code does the same for a long burst. terminal-ui.js _sendCjkPrompt types a
|
||||
// single character and pastes anything longer, Enter always on its own.
|
||||
const { textarea, sent, prompts } = load();
|
||||
type(textarea, 'a'.repeat(64));
|
||||
for (const answer of ['2', 'yes', 'a'.repeat(64), 'b'.repeat(150)]) {
|
||||
type(textarea, answer);
|
||||
enter(textarea);
|
||||
}
|
||||
expect(sent).toEqual([]);
|
||||
expect(prompts).toEqual(['2', 'yes', 'a'.repeat(64), 'b'.repeat(150)]);
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
});
|
||||
|
||||
it('keeps a short answer in the field when it is refused (no session)', () => {
|
||||
const { textarea, prompts } = load({ promptResult: false });
|
||||
type(textarea, 'yes');
|
||||
enter(textarea);
|
||||
type(textarea, 'b'.repeat(65));
|
||||
enter(textarea);
|
||||
expect(sent).toEqual(['a'.repeat(64) + '\r']);
|
||||
expect(prompts).toEqual(['b'.repeat(65)]);
|
||||
expect(prompts).toEqual(['yes']);
|
||||
expect(textarea.value).toBe(PHANTOM + 'yes');
|
||||
});
|
||||
|
||||
it('sends several lines as a prompt (bracketed paste + session-bound Enter)', () => {
|
||||
@@ -322,10 +321,132 @@ describe('CJK input: Send on Enter', () => {
|
||||
textarea.scrollHeight = 60;
|
||||
type(textarea, 'a\nb\nc');
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(textarea.style.height).toBe('60px');
|
||||
// scrollHeight + the 2px of border, so a two-line draft does not scroll inside.
|
||||
expect(textarea.style.height).toBe('62px');
|
||||
expect(layout).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('leaves the field alone when the mode is set again unchanged', () => {
|
||||
// Runs on every session select and SSE reconnect, possibly mid-composition.
|
||||
const { CjkInput, textarea } = load();
|
||||
textarea.setAttribute.mockClear();
|
||||
textarea.placeholder = 'untouched';
|
||||
CjkInput.setHoldUntilEnter(true);
|
||||
expect(textarea.setAttribute).not.toHaveBeenCalled();
|
||||
expect(textarea.placeholder).toBe('untouched');
|
||||
CjkInput.setHoldUntilEnter(false);
|
||||
expect(textarea.setAttribute).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('submit() from outside the field (toolbar Enter) sends like Enter', () => {
|
||||
const { CjkInput, textarea, prompts, sent } = load();
|
||||
type(textarea, 'go ahead');
|
||||
expect(CjkInput.submit()).toBe(true);
|
||||
expect(prompts).toEqual(['go ahead']);
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
expect(CjkInput.submit()).toBe(true);
|
||||
expect(sent).toEqual(['\r']);
|
||||
});
|
||||
|
||||
it('submit() keeps a refused answer and reports it', () => {
|
||||
const { CjkInput, textarea } = load({ promptResult: false });
|
||||
type(textarea, 'go ahead');
|
||||
expect(CjkInput.submit()).toBe(false);
|
||||
expect(textarea.value).toBe(PHANTOM + 'go ahead');
|
||||
});
|
||||
|
||||
it('a draft stashed on a switch comes back for its own session only', () => {
|
||||
const { CjkInput, textarea, sent, prompts } = load();
|
||||
type(textarea, 'half an answer');
|
||||
CjkInput.stashDraft('session-a');
|
||||
CjkInput.clear();
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
CjkInput.restoreDraft('session-b');
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
CjkInput.restoreDraft('session-a');
|
||||
expect(textarea.value).toBe(PHANTOM + 'half an answer');
|
||||
// Restored once: a second visit does not bring it back again.
|
||||
CjkInput.clear();
|
||||
CjkInput.restoreDraft('session-a');
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
expect(sent).toEqual([]);
|
||||
expect(prompts).toEqual([]);
|
||||
});
|
||||
|
||||
it('a restored draft is held text: Backspace edits it without a DEL', () => {
|
||||
const { CjkInput, textarea, sent } = load();
|
||||
type(textarea, 'ok');
|
||||
CjkInput.stashDraft('session-a');
|
||||
CjkInput.clear();
|
||||
CjkInput.restoreDraft('session-a');
|
||||
backspace(textarea, PHANTOM + 'o');
|
||||
backspace(textarea, PHANTOM);
|
||||
expect(sent).toEqual([]);
|
||||
});
|
||||
|
||||
it('an empty field stashes nothing, and a discarded draft is gone', () => {
|
||||
const { CjkInput, textarea } = load();
|
||||
type(textarea, 'draft');
|
||||
CjkInput.stashDraft('session-a');
|
||||
CjkInput.discardDraft('session-a');
|
||||
CjkInput.clear();
|
||||
CjkInput.restoreDraft('session-a');
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
CjkInput.stashDraft('session-b');
|
||||
CjkInput.restoreDraft('session-b');
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
});
|
||||
|
||||
it('a switch with an empty field keeps the draft already stashed for that session', () => {
|
||||
// The tile grid stashes the session it parks, then may stash it again from
|
||||
// tile focus with the field already cleared.
|
||||
const { CjkInput, textarea } = load();
|
||||
type(textarea, 'keep me');
|
||||
CjkInput.stashDraft('session-a');
|
||||
CjkInput.clear();
|
||||
CjkInput.stashDraft('session-a');
|
||||
CjkInput.restoreDraft('session-a');
|
||||
expect(textarea.value).toBe(PHANTOM + 'keep me');
|
||||
});
|
||||
|
||||
it('sends a bare Enter when the field holds only blank lines', () => {
|
||||
const { textarea, sent, prompts } = load();
|
||||
type(textarea, '\n');
|
||||
enter(textarea);
|
||||
expect(prompts).toEqual([]);
|
||||
expect(sent).toEqual(['\r']);
|
||||
});
|
||||
|
||||
it('submit() mid-composition commits it first (blur, focus) and sends what landed', () => {
|
||||
const { CjkInput, textarea, prompts } = load();
|
||||
textarea.fire('compositionstart');
|
||||
textarea.value = PHANTOM + 'yes';
|
||||
textarea.fire('input', { isComposing: true, inputType: 'insertCompositionText' });
|
||||
expect(CjkInput.submit()).toBe(true);
|
||||
expect(textarea.blur).toHaveBeenCalled();
|
||||
expect(textarea.focus).toHaveBeenCalled();
|
||||
expect(prompts).toEqual(['yes']);
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
});
|
||||
|
||||
it('a hidden field does not get a 0px height (no session shown yet)', () => {
|
||||
const { CjkInput, textarea } = load({ hold: false });
|
||||
textarea.getClientRects = () => [];
|
||||
textarea.scrollHeight = 0;
|
||||
textarea.style.height = '40px';
|
||||
CjkInput.setHoldUntilEnter(true);
|
||||
expect(textarea.style.height).toBe('');
|
||||
});
|
||||
|
||||
it('live mode stashes no drafts', () => {
|
||||
const { CjkInput, textarea } = load({ hold: false });
|
||||
textarea.value = PHANTOM + 'abc';
|
||||
CjkInput.stashDraft('session-a');
|
||||
CjkInput.clear();
|
||||
CjkInput.restoreDraft('session-a');
|
||||
expect(textarea.value).toBe(PHANTOM);
|
||||
});
|
||||
|
||||
it('live mode is unchanged: text streams after the debounce', () => {
|
||||
const { textarea, sent } = load({ hold: false });
|
||||
type(textarea, 'hi');
|
||||
|
||||
@@ -41,8 +41,9 @@ function loadTerminalModule(): TerminalModule {
|
||||
console,
|
||||
setInterval: vi.fn(),
|
||||
clearInterval: vi.fn(),
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
// Looked up per call, so a test's vi.useFakeTimers() reaches the cached module.
|
||||
setTimeout: (fn: () => void, ms?: number) => setTimeout(fn, ms),
|
||||
clearTimeout: (t: ReturnType<typeof setTimeout>) => clearTimeout(t),
|
||||
requestAnimationFrame: vi.fn(),
|
||||
HTMLCanvasElement: class HTMLCanvasElement {},
|
||||
WebSocket: { OPEN: 1 },
|
||||
@@ -547,10 +548,10 @@ describe('one-shot Ctrl through the CJK input field', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Send on Enter: a long or multi-line held answer goes out as a composed prompt', () => {
|
||||
// _sendCjkPrompt is CjkInput's sendPrompt callback. It hands the text to the
|
||||
// composer's _sendComposedPrompt with the session captured at Enter, so the
|
||||
// delayed Enter cannot follow a tab switch.
|
||||
describe('Send on Enter: a held answer goes out with Enter as its own write', () => {
|
||||
// _sendCjkPrompt is CjkInput's sendPrompt callback. Anything longer than one
|
||||
// character goes to the composer's _sendComposedPrompt with the session
|
||||
// captured at Enter, so the delayed Enter cannot follow a tab switch.
|
||||
function promptApp() {
|
||||
const { CodemanApp, bar } = loadTerminalModule();
|
||||
bar.clearCtrl();
|
||||
@@ -581,12 +582,44 @@ describe('Send on Enter: a long or multi-line held answer goes out as a composed
|
||||
composed.mockRestore();
|
||||
});
|
||||
|
||||
it('types a single character, then Enter ~120 ms later to the same session', () => {
|
||||
// Codex folds an Enter that arrives in the same burst as the text into a
|
||||
// paste, even for `1\r`; a selection dialog ignores a pasted digit. Same
|
||||
// split as TmuxManager.sendInput (submitsAsKeys).
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const { app, composed } = promptApp();
|
||||
const writes: Array<[string, string, unknown]> = [];
|
||||
(app as unknown as { _sendInputAsync: unknown })._sendInputAsync = (id: string, input: string, opts?: unknown) =>
|
||||
writes.push([id, input, opts]);
|
||||
expect(app._sendCjkPrompt('1')).toBe(true);
|
||||
expect(writes).toEqual([['session-a', '1', undefined]]);
|
||||
app.activeSessionId = 'session-b';
|
||||
vi.advanceTimersByTime(119);
|
||||
expect(writes).toHaveLength(1);
|
||||
vi.advanceTimersByTime(1);
|
||||
expect(writes[1]).toEqual(['session-a', '\r', { useMux: true }]);
|
||||
expect(composed).not.toHaveBeenCalled();
|
||||
composed.mockRestore();
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('pastes even a short word: only one character is typed', () => {
|
||||
const { app, composed } = promptApp();
|
||||
expect(app._sendCjkPrompt('yes')).toBe(true);
|
||||
expect(composed).toHaveBeenCalledWith('session-a', 'yes');
|
||||
composed.mockRestore();
|
||||
});
|
||||
|
||||
it('reports a refusal so the field keeps the text', () => {
|
||||
const { app, composed } = promptApp();
|
||||
composed.mockReturnValue(false);
|
||||
expect(app._sendCjkPrompt('x'.repeat(100))).toBe(false);
|
||||
app.activeSessionId = null;
|
||||
expect(app._sendCjkPrompt('x'.repeat(100))).toBe(false);
|
||||
expect(app._sendCjkPrompt('1')).toBe(false);
|
||||
expect(composed).toHaveBeenCalledTimes(1);
|
||||
composed.mockRestore();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user