package noaadapter import ( "encoding/json" "os" "path/filepath" "strings" "testing" "github.com/Autumn-27/norma/llm" "github.com/Autumn-27/norma/noa" "github.com/Autumn-27/norma/tool" ) // compressedSession returns a session with one real block, plus the history it // was built from and the options needed to reopen it. func compressedSession(t *testing.T) (Options, []llm.Message, *Session) { t.Helper() dir := t.TempDir() o := Options{ArchiveBaseDir: dir, SessionID: "sess"} opts := baseOptions() sess, err := EnableWithSession(&opts, o) if err != nil { t.Fatalf("EnableWithSession: %v", err) } body := strings.Repeat("compressible content. ", 300) tail := strings.Repeat("recent tail content. ", 250) msgs := []llm.Message{userText("the task")} for range 4 { msgs = append(msgs, llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{llm.TextBlock(body)}}) } for range 6 { msgs = append(msgs, userText(tail)) } sess.View(msgs) args, _ := json.Marshal(map[string]any{ "content": []map[string]any{{"startId": "m00002", "endId": "m00005", "summary": longSummary}}, }) res, err := runCompress(sess, args, &tool.ToolContext{ToolUseID: "toolu_1"}) if err != nil || noa.PanelBlockCount(res.Content[0].Text) != 1 { t.Fatalf("setup compression failed: %v / %s", err, res.Content[0].Text) } return o, msgs, sess } // With noa off, the full history would be sent and overflow at once; applying // the state keeps it the size it was compressed to. func TestMaterializeAppliesCompression(t *testing.T) { o, msgs, _ := compressedSession(t) out, err := Materialize(msgs, o) if err != nil { t.Fatalf("Materialize: %v", err) } if len(out) >= len(msgs) { t.Fatalf("materialized to %d messages, want fewer than the %d original", len(out), len(msgs)) } found := false for _, m := range out { if strings.HasPrefix(m.Text(), noa.SummaryHeader) { found = true // The archive pointer must survive: following a compression back to // its originals is a plain file read and does not need noa running. if !strings.Contains(m.Text(), ".md", rel) } raw, err := os.ReadFile(abs) if err != nil { t.Fatalf("read back: %v", err) } if string(raw) != "archive body" { t.Fatalf("content = %q", raw) } // No temp files may be left behind. entries, _ := os.ReadDir(filepath.Join(root, "tier2")) for _, e := range entries { if strings.HasPrefix(e.Name(), ".noa-") { t.Fatalf("a temp file survived: %s", e.Name()) } } if err := a.Remove(abs); err != nil { t.Fatalf("Remove: %v", err) } if _, err := os.Stat(abs); !os.IsNotExist(err) { t.Fatal("Remove did not delete the archive") } // Removing a missing archive is fine: rollback runs over files that may // never have landed. if err := a.Remove(abs); err != nil { t.Fatalf("Remove of a missing file = %v, want nil", err) } }