@@ -4,6 +4,7 @@ import { RGBA } from "@opentui/core"
44import { testRender } from "@opentui/solid"
55import type { Context } from "@opencode/plugin/tui/context"
66import type { ModelInfo } from "@opencode/client"
7+ import { createSignal } from "solid-js"
78import { ConfigProvider } from "../../src/config"
89import { createTuiResolvedConfig } from "../fixture/tui-runtime"
910import { SidebarContext } from "../../src/feature-plugins/sidebar/context"
@@ -36,13 +37,16 @@ function context(options?: {
3637 family : ( ) => family ?. map ( ( _ , index ) => ( index === 0 ? "session" : `child-${ index } ` ) ) ?? [ ] ,
3738 cost : ( ) => options ?. cost ?? 0 ,
3839 message : {
39- list : ( ) =>
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- } ) ) ,
40+ list : ( id : string ) =>
41+ ( id === "session" ? ( options ?. steps ?? ( options ?. tokens ? [ usage ( options . tokens ) ] : [ ] ) ) : [ ] ) . map (
42+ ( tokens , index ) => ( {
43+ id : `message-${ index } ` ,
44+ type : "assistant" ,
45+ model : { providerID : "provider" , id : "model" } ,
46+ tokens,
47+ time : { created : 0 } ,
48+ } ) ,
49+ ) ,
4650 } ,
4751 } ,
4852 location : {
@@ -135,11 +139,19 @@ test("sidebar keeps the reported cost over the estimate", async () => {
135139 }
136140} )
137141
138- test . each ( [ true , false ] ) ( "sidebar uses per-call tiers only with complete history: %s" , async ( complete ) => {
142+ test . each ( [
143+ { name : "complete history" , complete : true , title : 0 , child : 0 , expected : "≈ $1.20 spent" } ,
144+ { name : "partial history" , complete : false , title : 0 , child : 0 , expected : "≈ $1.20 spent" } ,
145+ { name : "title usage" , complete : false , title : 10_000 , child : 0 , expected : "≈ $1.23 spent" } ,
146+ { name : "unloaded child" , complete : false , title : 10_000 , child : 100_000 , expected : "≈ $1.53 spent" } ,
147+ ] ) ( "sidebar keeps tiered estimates with $name" , async ( { complete, title, child, expected } ) => {
139148 const app = await render (
140149 context ( {
141150 experiment : true ,
142- family : [ { model : { providerID : "provider" , id : "model" } , tokens : usage ( 300_000 , 20_000 ) } ] ,
151+ family : [
152+ { model : { providerID : "provider" , id : "model" } , tokens : usage ( 300_000 + title , 20_000 ) } ,
153+ { parentID : "session" , model : { providerID : "provider" , id : "model" } , tokens : usage ( child ) } ,
154+ ] ,
143155 steps : complete ? [ usage ( 150_000 , 10_000 ) , usage ( 150_000 , 10_000 ) ] : [ usage ( 150_000 , 10_000 ) ] ,
144156 pricing : [
145157 ...pricing ,
@@ -150,11 +162,7 @@ test.each([true, false])("sidebar uses per-call tiers only with complete history
150162
151163 try {
152164 await app . renderOnce ( )
153- if ( ! complete ) {
154- expect ( app . captureCharFrame ( ) ) . not . toContain ( "spent" )
155- return
156- }
157- expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $1.20 spent" )
165+ expect ( app . captureCharFrame ( ) ) . toContain ( expected )
158166 } finally {
159167 app . renderer . destroy ( )
160168 }
@@ -220,3 +228,78 @@ test("sidebar hides the estimate without catalog pricing", async () => {
220228 app . renderer . destroy ( )
221229 }
222230} )
231+
232+ test ( "sidebar does not show parent spend inherited by a fresh fork" , async ( ) => {
233+ const raw = context ( {
234+ experiment : true ,
235+ family : [ { model : { providerID : "provider" , id : "model" } , tokens : usage ( 0 ) } ] ,
236+ steps : [ usage ( 300_000 ) ] ,
237+ pricing : [
238+ ...pricing ,
239+ { tier : { type : "context" , size : 200_000 } , input : 6 , output : 22.5 , cache : { read : 0.6 , write : 0 } } ,
240+ ] ,
241+ } )
242+ const get = raw . data . session . get
243+ raw . data . session . get = ( id ) => ( {
244+ ...get ( id ) ,
245+ fork : { sessionID : "parent" , boundary : { type : "through" , messageID : "parent-message" } } ,
246+ time : { created : 1 , updated : 1 } ,
247+ } )
248+ const app = await render ( raw )
249+
250+ try {
251+ await app . renderOnce ( )
252+ expect ( app . captureCharFrame ( ) ) . toContain ( "Context" )
253+ expect ( app . captureCharFrame ( ) ) . not . toContain ( "spent" )
254+ } finally {
255+ app . renderer . destroy ( )
256+ }
257+ } )
258+
259+ test . each ( [ 42 , 80 ] ) ( "sidebar updates estimates as history and reported cost change at %i columns" , async ( width ) => {
260+ const raw = context ( {
261+ experiment : true ,
262+ family : [ { model : { providerID : "provider" , id : "model" } , tokens : usage ( 300_000 ) } ] ,
263+ pricing : [
264+ ...pricing ,
265+ { tier : { type : "context" , size : 200_000 } , input : 6 , output : 22.5 , cache : { read : 0.6 , write : 0 } } ,
266+ ] ,
267+ } )
268+ const [ messages , setMessages ] = createSignal ( raw . data . session . message . list ( "session" ) )
269+ const [ cost , setCost ] = createSignal ( 0 )
270+ raw . data . session . message . list = ( ) => messages ( )
271+ raw . data . session . cost = cost
272+ const app = await render ( raw , width )
273+
274+ try {
275+ await app . renderOnce ( )
276+ expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $0.90 spent" )
277+
278+ setMessages ( [
279+ {
280+ id : "message" ,
281+ type : "assistant" ,
282+ model : { providerID : "provider" , id : "model" } ,
283+ tokens : usage ( 250_000 ) ,
284+ time : { created : 0 } ,
285+ } ,
286+ ] )
287+ await app . renderOnce ( )
288+ expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $1.65 spent" )
289+
290+ setCost ( 1.23 )
291+ await app . renderOnce ( )
292+ expect ( app . captureCharFrame ( ) ) . toContain ( "$1.23 spent" )
293+ expect ( app . captureCharFrame ( ) ) . not . toContain ( "≈" )
294+
295+ setCost ( 0 )
296+ await app . renderOnce ( )
297+ expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $1.65 spent" )
298+
299+ setMessages ( [ ] )
300+ await app . renderOnce ( )
301+ expect ( app . captureCharFrame ( ) ) . toContain ( "≈ $0.90 spent" )
302+ } finally {
303+ app . renderer . destroy ( )
304+ }
305+ } )
0 commit comments