sidebar-context.test.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /** @jsxImportSource @opentui/solid */
  2. import { expect, test } from "bun:test"
  3. import { RGBA } from "@opentui/core"
  4. import { testRender } from "@opentui/solid"
  5. import type { Context } from "@opencode-ai/plugin/tui/context"
  6. import { SidebarContext } from "../../src/feature-plugins/sidebar/context"
  7. function context(options?: { cost?: number; tokens?: number }) {
  8. const color = RGBA.fromInts(200, 200, 200)
  9. return {
  10. theme: { text: { default: color, subdued: color } },
  11. data: {
  12. session: {
  13. get: () => ({ location: { directory: "/workspace" } }),
  14. cost: () => options?.cost ?? 0,
  15. message: {
  16. list: () =>
  17. options?.tokens
  18. ? [
  19. {
  20. id: "message",
  21. type: "assistant",
  22. model: { providerID: "provider", id: "model" },
  23. tokens: {
  24. input: options.tokens,
  25. output: 0,
  26. reasoning: 0,
  27. cache: { read: 0, write: 0 },
  28. },
  29. },
  30. ]
  31. : [],
  32. },
  33. },
  34. location: {
  35. model: { list: () => [] },
  36. },
  37. },
  38. } as unknown as Context
  39. }
  40. test("sidebar omits context before usage is available", async () => {
  41. const app = await testRender(() => <SidebarContext context={context()} sessionID="session" />, {
  42. width: 42,
  43. height: 8,
  44. })
  45. try {
  46. await app.renderOnce()
  47. expect(app.captureCharFrame()).not.toContain("Context")
  48. expect(app.captureCharFrame()).not.toContain("Not measured")
  49. } finally {
  50. app.renderer.destroy()
  51. }
  52. })
  53. test("sidebar shows available context usage", async () => {
  54. const app = await testRender(() => <SidebarContext context={context({ tokens: 1234 })} sessionID="session" />, {
  55. width: 42,
  56. height: 8,
  57. })
  58. try {
  59. await app.renderOnce()
  60. expect(app.captureCharFrame()).toContain("Context")
  61. expect(app.captureCharFrame()).toContain("1,234 tokens")
  62. } finally {
  63. app.renderer.destroy()
  64. }
  65. })