| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestHistoryReadersAcceptLegacyImportMetadata(t *testing.T) { |
| 12 | service, err := NewService("local", NewFilesystemPersistence(filepath.Join(t.TempDir(), "sessions"))) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | t.Cleanup(func() { _ = service.CloseAll(context.Background()) }) |
| 17 | runtime, err := service.Create(t.Context(), CreateOptions{SessionID: "imported"}) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | // Use the wire fields, not the shared decoder type: this fixture guards |
| 22 | // the actual import contract independently of its implementation. |
| 23 | payload := json.RawMessage(`{"source":{},"messages":[{"id":"imported-message","role":"user","content":"imported question"}],"goal":{"goal":"example"},"modelRef":"test","modelIdentity":"test-model"}`) |
| 24 | if _, err := runtime.Session().Append(t.Context(), Batch{OperationID: "import", Events: []Event{{Kind: "legacy/import", Payload: payload}}}); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if _, err := runtime.Session().Flush(t.Context()); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | page := historyPageReady(t, service.Query(), runtime.Ref(), "", 10) |
| 31 | if len(page.Messages) != 1 || page.Messages[0].MessageID != "imported-message" { |
| 32 | t.Fatalf("imported history: %+v", page) |
| 33 | } |
| 34 | if page := searchHistoryReady(t, service.Query(), runtime.Ref(), "imported", "", 10); len(page.Hits) != 1 { |
| 35 | t.Fatalf("imported search: %+v", page) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestReplacementEventSchemaRemainsStrict(t *testing.T) { |
| 40 | for _, kind := range []string{"history/replace", "legacy/import"} { |
| 41 | for _, payload := range []string{`{"messages":[],"typo":true}`, `{"messages":null}`, `{}`} { |
| 42 | event := Event{Kind: kind, Payload: json.RawMessage(payload)} |
| 43 | if _, err := replacementEventMessages(event, event.Payload); !errors.Is(err, ErrDamagedStore) { |
| 44 | t.Fatalf("%s accepted invalid payload %s: %v", kind, payload, err) |
| 45 | } |
| 46 | } |
| 47 | messages, err := replacementEventMessages(Event{Kind: kind}, json.RawMessage(`{"messages":[]}`)) |
| 48 | if err != nil || messages == nil || len(messages) != 0 { |
| 49 | t.Fatalf("%s rejected valid empty history: %v", kind, err) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 |