Files
Agent-Native/runtime/session/tree_test.go
T
weichen 6ddd3da8df Enable canonical versioned Go module consumption
Why: Allow consumers to download an immutable module revision without nested source checkouts or local path replacements.

Evidence: SELF package tests and the prior canonical-path rejection are recorded in the Dev dependency-workspace task card; remote consumption is replayed against the committed candidate.

Impact: Change the module identity and its Go imports while preserving API behavior. Existing historical submodule revisions remain buildable.

Refs: Versioned dependencies U1/U3; feature/dependency-workspace
2026-10-13 15:36:10 +00:00

129 lines
3.4 KiB
Go

package session
import (
"os"
"path/filepath"
"strings"
"testing"
"git-ssh.u1680832.nyat.app/weichen/Agent-Native.git/runtime/llm"
)
func TestTreeForkCloneAndLeaf(t *testing.T) {
dir := t.TempDir()
s, err := Open(filepath.Join(dir, "a.jsonl"))
if err != nil {
t.Fatal(err)
}
if err := s.Append(llm.Message{Role: llm.RoleUser, Content: "one"}); err != nil {
t.Fatal(err)
}
if err := s.Append(llm.Message{Role: llm.RoleAssistant, Content: "two"}); err != nil {
t.Fatal(err)
}
entries, err := s.Entries()
if err != nil || len(entries) != 2 || entries[1].ParentID != entries[0].ID {
t.Fatalf("tree links: %+v %v", entries, err)
}
fork, err := s.Fork(filepath.Join(dir, "fork.jsonl"), entries[0].ID)
if err != nil {
t.Fatal(err)
}
if err := fork.Append(llm.Message{Role: llm.RoleUser, Content: "branch"}); err != nil {
t.Fatal(err)
}
msgs, err := fork.Load()
if err != nil || len(msgs) != 2 || msgs[1].Content != "branch" {
t.Fatalf("fork path: %+v %v", msgs, err)
}
orig, _ := s.Load()
if len(orig) != 2 || orig[1].Content != "two" {
t.Fatalf("source branch changed: %+v", orig)
}
clone, err := s.Clone(filepath.Join(dir, "clone.jsonl"))
if err != nil {
t.Fatal(err)
}
cmsgs, _ := clone.Load()
if len(cmsgs) != 2 || cmsgs[1].Content != "two" {
t.Fatalf("clone: %+v", cmsgs)
}
if err := s.SetLeaf(entries[0].ID); err != nil {
t.Fatal(err)
}
switched, _ := s.Load()
if len(switched) != 1 || switched[0].Content != "one" {
t.Fatalf("set leaf: %+v", switched)
}
}
func TestLegacyTranscriptStillLoads(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "old.jsonl")
body := "{\"role\":\"system\",\"content\":\"sys\"}\n{\"role\":\"user\",\"content\":\"hi\"}\n"
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
s, err := Open(path)
if err != nil {
t.Fatal(err)
}
msgs, err := s.Load()
if err != nil || len(msgs) != 2 || msgs[1].Content != "hi" {
t.Fatalf("legacy: %+v %v", msgs, err)
}
if err := s.Append(llm.Message{Role: llm.RoleAssistant, Content: "next"}); err != nil {
t.Fatal(err)
}
msgs, err = s.Load()
if err != nil || len(msgs) != 3 || msgs[2].Content != "next" {
t.Fatalf("legacy append: %+v %v", msgs, err)
}
}
func TestCatalogContinueResumeAndHTML(t *testing.T) {
dir := t.TempDir()
s, err := Open(filepath.Join(dir, "sess.jsonl"))
if err != nil {
t.Fatal(err)
}
if err := s.SetCWD("/work"); err != nil {
t.Fatal(err)
}
if err := s.SetName("notes"); err != nil {
t.Fatal(err)
}
if err := s.Append(llm.Message{Role: llm.RoleUser, Content: "hello"}); err != nil {
t.Fatal(err)
}
got, ok, err := Continue(dir, "/work")
if err != nil || !ok || got.Path() != s.Path() {
t.Fatalf("continue: %v %v %v", got, ok, err)
}
resumed, err := Resume(dir, "notes")
if err != nil || resumed.Name() != "notes" {
t.Fatalf("resume: %v %v", resumed, err)
}
htmlPath := filepath.Join(dir, "out.html")
if err := s.ExportHTML(htmlPath); err != nil {
t.Fatal(err)
}
raw, _ := os.ReadFile(htmlPath)
if !strings.Contains(string(raw), "hello") || !strings.Contains(string(raw), "notes") {
t.Fatalf("html: %s", raw)
}
if _, err := s.AppendEntry(Entry{Type: TypeLabel, Label: "checkpoint"}); err != nil {
t.Fatal(err)
}
entries, _ := s.Entries()
var labels int
for _, e := range entries {
if e.Type == TypeLabel && e.Label == "checkpoint" {
labels++
}
}
if labels != 1 {
t.Fatalf("label missing: %+v", entries)
}
}