Files
gastown/internal/cmd/costs_test.go
slit 89785378bf feat(costs): Wire up gt costs record to Stop hook
Add Claude Code Stop hook that automatically records session costs when
sessions end. The hook calls `gt costs record` which now can derive the
session name from GT_* environment variables (GT_RIG, GT_POLECAT, GT_CREW,
GT_ROLE).

Changes:
- Add deriveSessionName() to infer tmux session name from environment
- Add Stop hook to settings-autonomous.json and settings-interactive.json
- Add unit tests for deriveSessionName function

When a Gas Town session ends, the Stop hook fires and records the session
cost as a bead event. Costs then appear in `gt costs --today` output.

Closes: gt-ntzhc

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-02 18:29:25 -08:00

99 lines
1.8 KiB
Go

package cmd
import (
"os"
"testing"
)
func TestDeriveSessionName(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
expected string
}{
{
name: "polecat session",
envVars: map[string]string{
"GT_ROLE": "polecat",
"GT_RIG": "gastown",
"GT_POLECAT": "toast",
},
expected: "gt-gastown-toast",
},
{
name: "crew session",
envVars: map[string]string{
"GT_ROLE": "crew",
"GT_RIG": "gastown",
"GT_CREW": "max",
},
expected: "gt-gastown-crew-max",
},
{
name: "witness session",
envVars: map[string]string{
"GT_ROLE": "witness",
"GT_RIG": "gastown",
},
expected: "gt-gastown-witness",
},
{
name: "refinery session",
envVars: map[string]string{
"GT_ROLE": "refinery",
"GT_RIG": "gastown",
},
expected: "gt-gastown-refinery",
},
{
name: "mayor session",
envVars: map[string]string{
"GT_ROLE": "mayor",
},
expected: "gt-mayor",
},
{
name: "deacon session",
envVars: map[string]string{
"GT_ROLE": "deacon",
},
expected: "gt-deacon",
},
{
name: "no env vars",
envVars: map[string]string{},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save and clear relevant env vars
saved := make(map[string]string)
envKeys := []string{"GT_ROLE", "GT_RIG", "GT_POLECAT", "GT_CREW"}
for _, key := range envKeys {
saved[key] = os.Getenv(key)
os.Unsetenv(key)
}
defer func() {
// Restore env vars
for key, val := range saved {
if val != "" {
os.Setenv(key, val)
}
}
}()
// Set test env vars
for key, val := range tt.envVars {
os.Setenv(key, val)
}
result := deriveSessionName()
if result != tt.expected {
t.Errorf("deriveSessionName() = %q, want %q", result, tt.expected)
}
})
}
}