summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/testing/prompt.ts
blob: 5102ed825bf8387f43cf28a40970b38f9c2167da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { E2EWindow } from "./terminal"

export type PromptProbeState = {
  popover: "at" | "slash" | null
  slash: {
    active: string | null
    ids: string[]
  }
  selected: string | null
  selects: number
}

export type PromptSendState = {
  started: number
  count: number
  sessionID?: string
  directory?: string
}

export const promptEnabled = () => {
  if (typeof window === "undefined") return false
  return (window as E2EWindow).__opencode_e2e?.prompt?.enabled === true
}

const root = () => {
  if (!promptEnabled()) return
  return (window as E2EWindow).__opencode_e2e?.prompt
}

export const promptProbe = {
  set(input: Omit<PromptProbeState, "selected" | "selects">) {
    const state = root()
    if (!state) return
    state.current = {
      popover: input.popover,
      slash: {
        active: input.slash.active,
        ids: [...input.slash.ids],
      },
      selected: state.current?.selected ?? null,
      selects: state.current?.selects ?? 0,
    }
  },
  select(id: string) {
    const state = root()
    if (!state) return
    const prev = state.current
    state.current = {
      popover: prev?.popover ?? null,
      slash: {
        active: prev?.slash.active ?? null,
        ids: [...(prev?.slash.ids ?? [])],
      },
      selected: id,
      selects: (prev?.selects ?? 0) + 1,
    }
  },
  clear() {
    const state = root()
    if (!state) return
    state.current = undefined
  },
  start() {
    const state = root()
    if (!state) return
    state.sent = {
      started: (state.sent?.started ?? 0) + 1,
      count: state.sent?.count ?? 0,
      sessionID: state.sent?.sessionID,
      directory: state.sent?.directory,
    }
  },
  submit(input: { sessionID: string; directory: string }) {
    const state = root()
    if (!state) return
    state.sent = {
      started: state.sent?.started ?? 0,
      count: (state.sent?.count ?? 0) + 1,
      sessionID: input.sessionID,
      directory: input.directory,
    }
  },
}