@@ -3,6 +3,7 @@ import { expect, test } from "bun:test"
33import { RGBA } from "@opentui/core"
44import { testRender } from "@opentui/solid"
55import type { Context } from "@opencode/plugin/tui/context"
6+ import type { ModelInfo } from "@opencode/client"
67import { ConfigProvider } from "../../src/config"
78import { createTuiResolvedConfig } from "../fixture/tui-runtime"
89import { SidebarContext } from "../../src/feature-plugins/sidebar/context"
@@ -16,8 +17,9 @@ function usage(input: number, output = 0, read = 0) {
1617function context ( options ?: {
1718 cost ?: number
1819 tokens ?: number
19- family ?: Array < { model ?: { providerID : string ; id : string } ; tokens ?: ReturnType < typeof usage > } >
20- pricing ?: typeof pricing
20+ steps ?: Array < ReturnType < typeof usage > >
21+ family ?: Array < { parentID ?: string ; model ?: { providerID : string ; id : string } ; tokens ?: ReturnType < typeof usage > } >
22+ pricing ?: ModelInfo [ "cost" ]
2123 experiment ?: boolean
2224} ) {
2325 const color = RGBA . fromInts ( 200 , 200 , 200 )
@@ -29,44 +31,38 @@ function context(options?: {
2931 get : ( id : string ) => {
3032 if ( ! family ) return { location : { directory : "/workspace" } }
3133 const index = id === "session" ? 0 : Number ( id . replace ( "child-" , "" ) )
32- return family [ index ]
34+ return { ... family [ index ] , id , location : { directory : "/workspace" } }
3335 } ,
3436 family : ( ) => family ?. map ( ( _ , index ) => ( index === 0 ? "session" : `child-${ index } ` ) ) ?? [ ] ,
3537 cost : ( ) => options ?. cost ?? 0 ,
3638 message : {
3739 list : ( ) =>
38- options ?. tokens
39- ? [
40- {
41- id : "message" ,
42- type : "assistant" ,
43- model : { providerID : "provider" , id : "model" } ,
44- tokens : {
45- input : options . tokens ,
46- output : 0 ,
47- reasoning : 0 ,
48- cache : { read : 0 , write : 0 } ,
49- } ,
50- } ,
51- ]
52- : [ ] ,
40+ ( options ?. steps ?? ( options ?. tokens ? [ usage ( options . tokens ) ] : [ ] ) ) . map ( ( tokens , index ) => ( {
41+ id : `message-${ index } ` ,
42+ type : "assistant" ,
43+ model : { providerID : "provider" , id : "model" } ,
44+ tokens,
45+ } ) ) ,
5346 } ,
5447 } ,
5548 location : {
5649 model : {
57- list : ( ) => ( options ?. pricing ? [ { providerID : "provider" , id : "model" , cost : options . pricing } ] : [ ] ) ,
50+ list : ( ) =>
51+ options ?. pricing
52+ ? [ { providerID : "provider" , id : "model" , cost : options . pricing , limit : { context : 0 } } ]
53+ : [ ] ,
5854 } ,
5955 } ,
6056 } ,
6157 experiment : options ?. experiment ,
6258 }
6359}
6460
65- async function render ( raw : ReturnType < typeof context > , width = 42 ) {
61+ async function render ( raw : ReturnType < typeof context > , width = 42 , sessionID = "session" ) {
6662 const app = await testRender (
6763 ( ) => (
6864 < ConfigProvider config = { createTuiResolvedConfig ( { experimental : { "cost-estimate" : raw . experiment === true } } ) } >
69- < SidebarContext context = { raw as unknown as Context } sessionID = "session" />
65+ < SidebarContext context = { raw as unknown as Context } sessionID = { sessionID } />
7066 </ ConfigProvider >
7167 ) ,
7268 { width, height : 8 } ,
@@ -139,6 +135,58 @@ test("sidebar keeps the reported cost over the estimate", async () => {
139135 }
140136} )
141137
138+ test . each ( [ true , false ] ) ( "sidebar uses per-call tiers only with complete history: %s" , async ( complete ) => {
139+ const app = await render (
140+ context ( {
141+ experiment : true ,
142+ family : [ { model : { providerID : "provider" , id : "model" } , tokens : usage ( 300_000 , 20_000 ) } ] ,
143+ steps : complete ? [ usage ( 150_000 , 10_000 ) , usage ( 150_000 , 10_000 ) ] : [ usage ( 150_000 , 10_000 ) ] ,
144+ pricing : [
145+ ...pricing ,
146+ { tier : { type : "context" , size : 200_000 } , input : 6 , output : 22.5 , cache : { read : 0.6 , write : 0 } } ,
147+ ] ,
148+ } ) ,
149+ )
150+
151+ try {
152+ await app . renderOnce ( )
153+ if ( ! complete ) {
154+ expect ( app . captureCharFrame ( ) ) . not . toContain ( "spent" )
155+ return
156+ }
157+ expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $1.20 spent" )
158+ } finally {
159+ app . renderer . destroy ( )
160+ }
161+ } )
162+
163+ test . each ( [ 0 , 500_000 ] ) ( "sidebar estimates only the selected child's %i input tokens" , async ( input ) => {
164+ const app = await render (
165+ context ( {
166+ experiment : true ,
167+ family : [
168+ { model : { providerID : "provider" , id : "model" } , tokens : usage ( 1_000_000 ) } ,
169+ { parentID : "session" , model : { providerID : "provider" , id : "model" } , tokens : usage ( input ) } ,
170+ { parentID : "session" , model : { providerID : "provider" , id : "model" } , tokens : usage ( 2_000_000 ) } ,
171+ ] ,
172+ pricing,
173+ } ) ,
174+ 42 ,
175+ "child-1" ,
176+ )
177+
178+ try {
179+ await app . renderOnce ( )
180+ if ( input === 0 ) {
181+ expect ( app . captureCharFrame ( ) ) . not . toContain ( "spent" )
182+ return
183+ }
184+ expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $1.50 spent" )
185+ } finally {
186+ app . renderer . destroy ( )
187+ }
188+ } )
189+
142190test ( "sidebar hides the estimate while the experiment is off" , async ( ) => {
143191 const app = await render (
144192 context ( {
0 commit comments