mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-01 18:26:38 +00:00
chore: refactoring and tests (#12468)
This commit is contained in:
61
packages/app/src/pages/session/helpers.test.ts
Normal file
61
packages/app/src/pages/session/helpers.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { combineCommandSections, createOpenReviewFile, focusTerminalById } from "./helpers"
|
||||
|
||||
describe("createOpenReviewFile", () => {
|
||||
test("opens and loads selected review file", () => {
|
||||
const calls: string[] = []
|
||||
const openReviewFile = createOpenReviewFile({
|
||||
showAllFiles: () => calls.push("show"),
|
||||
tabForPath: (path) => {
|
||||
calls.push(`tab:${path}`)
|
||||
return `file://${path}`
|
||||
},
|
||||
openTab: (tab) => calls.push(`open:${tab}`),
|
||||
loadFile: (path) => calls.push(`load:${path}`),
|
||||
})
|
||||
|
||||
openReviewFile("src/a.ts")
|
||||
|
||||
expect(calls).toEqual(["show", "tab:src/a.ts", "open:file://src/a.ts", "load:src/a.ts"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("focusTerminalById", () => {
|
||||
test("focuses textarea when present", () => {
|
||||
document.body.innerHTML = `<div id="terminal-wrapper-one"><div data-component="terminal"><textarea></textarea></div></div>`
|
||||
|
||||
const focused = focusTerminalById("one")
|
||||
|
||||
expect(focused).toBe(true)
|
||||
expect(document.activeElement?.tagName).toBe("TEXTAREA")
|
||||
})
|
||||
|
||||
test("falls back to terminal element focus", () => {
|
||||
document.body.innerHTML = `<div id="terminal-wrapper-two"><div data-component="terminal" tabindex="0"></div></div>`
|
||||
const terminal = document.querySelector('[data-component="terminal"]') as HTMLElement
|
||||
let pointerDown = false
|
||||
terminal.addEventListener("pointerdown", () => {
|
||||
pointerDown = true
|
||||
})
|
||||
|
||||
const focused = focusTerminalById("two")
|
||||
|
||||
expect(focused).toBe(true)
|
||||
expect(document.activeElement).toBe(terminal)
|
||||
expect(pointerDown).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("combineCommandSections", () => {
|
||||
test("keeps section order stable", () => {
|
||||
const result = combineCommandSections([
|
||||
[{ id: "a", title: "A" }],
|
||||
[
|
||||
{ id: "b", title: "B" },
|
||||
{ id: "c", title: "C" },
|
||||
],
|
||||
])
|
||||
|
||||
expect(result.map((item) => item.id)).toEqual(["a", "b", "c"])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user