You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.1 KiB
Go

package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/cloudwego/eino/schema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testTime = time.Date(2026, time.May, 23, 12, 0, 0, 0, time.UTC)
// testFile creates an session jsonl file with the appropriate SessionHeader starting
// line.
func testFile(t *testing.T, dir string) (string, string) {
t.Helper()
f, err := os.CreateTemp(dir, "test-session-*.jsonl")
if err != nil {
// Panic risk in call to f.Name() if in error because f would be nil
// t.Fatalf("failed to create temp session file %s", f.Name())
t.Fatal("failed to create temp session file")
}
fullPath := f.Name()
// Ensure files are cleaned up
t.Cleanup(func() {
_ = os.Remove(fullPath)
})
// os.File name can contain either the full path or the base file name
// depending on what the Open method arguments are. The use of filepath.Base
// ensures we only take the file name.
id := strings.TrimSuffix(filepath.Base(fullPath), ".jsonl")
s := SessionHeader{Type: "session", ID: id, CreatedAt: testTime}
hData, err := json.Marshal(s)
if err != nil {
t.Fatalf("failed to marshal header data for %s", fullPath)
}
hData = append(hData, '\n')
_, err = f.Write(hData)
if err != nil {
f.Close()
t.Fatalf("failed to write session header to %s", fullPath)
}
if err = f.Close(); err != nil {
t.Fatalf("failed to close test file %s", fullPath)
}
return fullPath, id
}
func TestSessionAppend(t *testing.T) {
td := t.TempDir()
fp, id := testFile(t, td)
want := "Tell me about eino"
s := Session{ID: id, CreatedAt: testTime, FilePath: fp}
s.Append(&schema.Message{Role: schema.User, Content: want})
var msg *schema.Message
data, err := os.ReadFile(fp)
assert.NoError(t, err)
content := bytes.Split(data, []byte("\n"))
rawMsg := bytes.TrimSpace(content[1]) // possible out of index if Append failed
json.Unmarshal(rawMsg, &msg) // didn't check parse error
// we should convert to require which better documents the intention of the
// code and the test
assert.Equal(t, msg.Content, s.Messages[0].Content)
}
func TestSessionAppendBetter(t *testing.T) {
td := os.TempDir()
fp, id := testFile(t, td)
want := "Tell me about eino"
s := Session{ID: id, CreatedAt: testTime, FilePath: fp}
err := s.Append(&schema.Message{Role: schema.User, Content: want})
require.NoError(t, err, "s.Append should not return an error")
data, err := os.ReadFile(s.FilePath)
require.NoError(t, err, "failed to read from session file")
// remove possible trailing newline in file contents
data = bytes.TrimSpace(data)
content := bytes.Split(data, []byte("\n"))
require.Len(t, content, 2, "files should contain two lines")
var msg *schema.Message
err = json.Unmarshal(content[1], &msg)
require.NoError(t, err, "content should json parse as eino Message")
require.Equal(t, want, msg.Content, "retrieved message content should match test content")
require.Len(t, s.Messages, 1, "session message should have 1 item")
require.Equal(t, want, s.Messages[0].Content, "in memory content should match test content")
}