package runtime
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sausheong/harness/llm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// --- providerSupportsMidLoopCompaction ---
func TestProviderSupportsMidLoopCompactionMatrix(t *testing.T) {
cases := map[string]bool{
"anthropic": true,
"openai": true,
"gemini": true,
"local": false,
"ollama": false,
"": false, // empty/unknown defaults to off (safety)
"deepseek": false, // not yet classified — opt-in via the switch
}
for provider, want := range cases {
r := &Runtime{Provider: provider}
assert.Equalf(t, want, r.providerSupportsMidLoopCompaction(),
"provider=%q", provider)
}
}
// --- recordFileTouch ordering + dedupe ---
func TestRecordFileTouchAppendsAndDedupesByMoveToBack(t *testing.T) {
r := &Runtime{}
r.recordFileTouch("a.go")
r.recordFileTouch("b.go")
r.recordFileTouch("c.go")
r.recordFileTouch("a.go") // re-touch — should move to back, not duplicate
got := r.snapshotTouchedFiles()
assert.Equal(t, []string{"b.go", "c.go", "a.go"}, got)
}
func TestRecordFileTouchIgnoresEmptyPath(t *testing.T) {
r := &Runtime{}
r.recordFileTouch("")
r.recordFileTouch("x.go")
r.recordFileTouch("")
assert.Equal(t, []string{"x.go"}, r.snapshotTouchedFiles())
}
func TestSnapshotTouchedFilesReturnsCopy(t *testing.T) {
r := &Runtime{}
r.recordFileTouch("a.go")
snap := r.snapshotTouchedFiles()
snap[0] = "mutated"
// Mutation of the snapshot must not bleed back into the live slice;
// otherwise concurrent post-compact restore reads could observe
// torn paths under load.
assert.Equal(t, []string{"a.go"}, r.snapshotTouchedFiles())
}
// --- extractPathFromInput ---
func TestExtractPathFromInputHappyPath(t *testing.T) {
in, _ := json.Marshal(map[string]string{"path": "/tmp/foo.go"})
assert.Equal(t, "/tmp/foo.go", extractPathFromInput(in))
}
func TestExtractPathFromInputMissingFieldReturnsEmpty(t *testing.T) {
in, _ := json.Marshal(map[string]string{"command": "ls"})
assert.Equal(t, "", extractPathFromInput(in))
}
func TestExtractPathFromInputBadJSONReturnsEmpty(t *testing.T) {
assert.Equal(t, "", extractPathFromInput([]byte("not-json{")))
assert.Equal(t, "", extractPathFromInput(nil))
}
// --- buildPostCompactRestore ---
func TestBuildPostCompactRestoreFormatsLatestKFiles(t *testing.T) {
dir := t.TempDir()
a := filepath.Join(dir, "a.go")
b := filepath.Join(dir, "b.go")
c := filepath.Join(dir, "c.go")
require.NoError(t, os.WriteFile(a, []byte("package a\n"), 0o644))
require.NoError(t, os.WriteFile(b, []byte("package b\n"), 0o644))
require.NoError(t, os.WriteFile(c, []byte("package c\n"), 0o644))
// Touch order: a, b, c — newest is c. K=2 should pick c then b
// (newest-first walk), and skip a entirely.
msg := buildPostCompactRestore([]string{a, b, c}, 2, 1024)
require.Equal(t, "user", msg.Role)
assert.Contains(t, msg.Content, "")
assert.Contains(t, msg.Content, "")
assert.Contains(t, msg.Content, c, "newest file must be present")
assert.Contains(t, msg.Content, b, "second-newest must be present")
assert.NotContains(t, msg.Content, a, "K=2 cap must drop the oldest")
}
func TestBuildPostCompactRestoreSkipsMissingAndDir(t *testing.T) {
dir := t.TempDir()
good := filepath.Join(dir, "good.go")
require.NoError(t, os.WriteFile(good, []byte("ok\n"), 0o644))
missing := filepath.Join(dir, "missing.go")
subdir := filepath.Join(dir, "sub")
require.NoError(t, os.Mkdir(subdir, 0o755))
msg := buildPostCompactRestore([]string{missing, subdir, good}, 5, 1024)
assert.Contains(t, msg.Content, good)
assert.NotContains(t, msg.Content, missing)
assert.NotContains(t, msg.Content, "")
assert.Contains(t, got[0].Content, f)
assert.Equal(t, original[0], got[1], "original messages must follow restore intact")
}
func TestPrependPostCompactRestoreReturnsUnchangedWhenNoFiles(t *testing.T) {
original := []llm.Message{{Role: "user", Content: "hi"}}
got := prependPostCompactRestore(original, nil)
assert.Equal(t, original, got)
}
func TestPrependPostCompactRestoreReturnsUnchangedWhenAllUnreadable(t *testing.T) {
original := []llm.Message{{Role: "user", Content: "hi"}}
got := prependPostCompactRestore(original, []string{"/nonexistent/ghost.go"})
assert.Equal(t, original, got, "unreadable touched files must not produce an empty restore wrapper")
}