- the composer shows a context ring and percentage from the last token-bearing assistant message, toned at 75% and 90%; tapping opens a sheet with the model, used tokens, limits, output reserve and turn cost - images a tool call names (read attachments as data URLs, input paths, and image paths in output text) get a link under the tool row; tapping previews it from the data URL or /api/fs/raw, allowing paths outside the workspace - directory-scoped requests now send x-opencode-directory-encoding: uri, which scoped routes such as /api/fs/raw need to decode the header - ocprobe gains a raw file command for checking previews Bump version to 0.2.2 (build 16).
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package appui
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/egoist/asuka/signal"
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
|
)
|
|
|
|
func usageTestChat(limits map[string]modelLimit) *chat {
|
|
return &chat{
|
|
modelLimits: signal.NewFunc(limits, nil),
|
|
modelNames: signal.NewFunc(map[string]string{}, nil),
|
|
}
|
|
}
|
|
|
|
func TestUsageReadingCapsOutputReserve(t *testing.T) {
|
|
ch := usageTestChat(map[string]modelLimit{
|
|
"p/m": {Context: 200_000, Output: 64_000},
|
|
})
|
|
msgs := []ocapi.Message{{
|
|
Type: "assistant",
|
|
Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
|
Tokens: &ocapi.TokenUsage{Total: 50_000},
|
|
Cost: 0.02,
|
|
}}
|
|
r := ch.usageReading(msgs)
|
|
if !r.Known {
|
|
t.Fatal("usage should be known")
|
|
}
|
|
if r.Limit != 200_000 || r.Output != 64_000 || r.Used != 50_000 {
|
|
t.Errorf("reading = %+v", r)
|
|
}
|
|
// 200k - min(64k, 32k) = 168k; 50k / 168k = 29.76%
|
|
want := 50_000.0 / 168_000.0 * 100
|
|
if math.Abs(r.Pct-want) > 1e-9 {
|
|
t.Errorf("pct = %v, want %v", r.Pct, want)
|
|
}
|
|
if r.Cost != 0.02 {
|
|
t.Errorf("cost = %v", r.Cost)
|
|
}
|
|
}
|
|
|
|
func TestUsageReadingFallsBackToFieldSum(t *testing.T) {
|
|
ch := usageTestChat(map[string]modelLimit{
|
|
"p/m": {Context: 100_000, Output: 8_000},
|
|
})
|
|
tokens := &ocapi.TokenUsage{Input: 10, Output: 20, Reasoning: 30}
|
|
tokens.Cache.Read = 40
|
|
tokens.Cache.Write = 50
|
|
r := ch.usageReading([]ocapi.Message{{
|
|
Type: "assistant",
|
|
Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
|
Tokens: tokens,
|
|
}})
|
|
if r.Used != 150 {
|
|
t.Errorf("used = %d, want 150", r.Used)
|
|
}
|
|
if want := 150.0 / 92_000.0 * 100; math.Abs(r.Pct-want) > 1e-9 {
|
|
t.Errorf("pct = %v, want %v", r.Pct, want)
|
|
}
|
|
}
|
|
|
|
func TestUsageReadingUnknownWithoutLimit(t *testing.T) {
|
|
ch := usageTestChat(nil)
|
|
r := ch.usageReading([]ocapi.Message{{
|
|
Type: "assistant",
|
|
Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
|
Tokens: &ocapi.TokenUsage{Total: 500},
|
|
}})
|
|
if r.Known || r.Pct != 0 {
|
|
t.Errorf("reading = %+v, want unknown", r)
|
|
}
|
|
}
|
|
|
|
func TestUsageReadingBorrowsModelFromEarlierMessage(t *testing.T) {
|
|
ch := usageTestChat(map[string]modelLimit{
|
|
"p/m": {Context: 100_000, Output: 8_000},
|
|
})
|
|
msgs := []ocapi.Message{
|
|
{Type: "assistant", Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
|
Tokens: &ocapi.TokenUsage{Total: 1_000}},
|
|
// Added by the event stream: tokens, but no model yet.
|
|
{Type: "assistant", Tokens: &ocapi.TokenUsage{Total: 2_000}},
|
|
}
|
|
r := ch.usageReading(msgs)
|
|
if !r.Known || r.Used != 2_000 || r.Limit != 100_000 {
|
|
t.Errorf("reading = %+v", r)
|
|
}
|
|
}
|