Files
ai/object/chat_search_test.go
zeekayandHanzo Dev 82353b9873 chat: search by message text never returned a row on any driver
Both halves of the ?field=messages branch were broken, and neither could have
worked in production:

  getPaginationChatsByMessages passed "DISTINCT chat.*" as a column name.
  DISTINCT belongs to the SELECT clause, so the builder quoted the whole thing
  as an identifier — `DISTINCT chat`.* — and every call died with
  "no such table: DISTINCT chat" before the join was evaluated.

  getChatCountByMessages counted COUNT(DISTINCT chat.owner, chat.name).
  Two-argument COUNT(DISTINCT …) is MySQL-only; SQLite answers "wrong number of
  arguments to function COUNT()" and Postgres rejects it too. This one runs
  first, so on our own default driver the endpoint failed before it ever reached
  the page query.

Both de-duplications exist only because the join multiplies a chat by each of
its matching messages. EXISTS does not, so neither caller has to undo anything:
one predicate, shared by the count and the page, identical on every driver, and
no DISTINCT left to spell wrong. The LIKE pattern stays bound; "%"+value+"%" is
exactly what dbx.Like builds, which escapes nothing, so matching is unchanged.

The sort column still goes through the sortColumn whitelist — the shape of the
query changed, not who supplies that value.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-02 21:32:02 -07:00

57 lines
1.9 KiB
Go

package object
import (
"testing"
)
// Chat search by message text (?field=messages) has to actually return rows.
// The DISTINCT belongs to the SELECT clause, not to a column name — passed as
// one it is quoted as an identifier, `DISTINCT chat`.*, and every call fails
// with "no such table: DISTINCT chat" before the join is ever evaluated.
func TestGetPaginationChatsByMessages_ReturnsMatchingChats(t *testing.T) {
restore, err := UseMemoryDB("file:chatsearch?mode=memory&cache=shared", &Chat{}, &Message{})
if err != nil {
t.Fatalf("memory db: %v", err)
}
defer restore()
for i, name := range []string{"alpha", "bravo", "charlie"} {
if err := insertRow(adapter.db, &Chat{Owner: "acme", Name: name, CreatedTime: string(rune('a' + i))}); err != nil {
t.Fatalf("seed chat %s: %v", name, err)
}
}
// alpha matches twice, so DISTINCT is what keeps it from appearing twice.
for _, m := range []Message{
{Owner: "acme", Name: "m1", Chat: "alpha", Text: "the needle is here"},
{Owner: "acme", Name: "m2", Chat: "alpha", Text: "needle again"},
{Owner: "acme", Name: "m3", Chat: "bravo", Text: "needle too"},
{Owner: "acme", Name: "m4", Chat: "charlie", Text: "nothing to see"},
} {
msg := m
if err := insertRow(adapter.db, &msg); err != nil {
t.Fatalf("seed message %s: %v", m.Name, err)
}
}
chats, err := getPaginationChatsByMessages("acme", 0, 10, "needle", "name", "ascend", "")
if err != nil {
t.Fatalf("search: %v", err)
}
got := []string{}
for _, c := range chats {
got = append(got, c.Name)
}
if len(got) != 2 || got[0] != "alpha" || got[1] != "bravo" {
t.Fatalf("search for %q returned %v, want [alpha bravo]", "needle", got)
}
// The count beside it feeds the paginator and must agree.
count, err := getChatCountByMessages("acme", "needle", "")
if err != nil {
t.Fatalf("count: %v", err)
}
if count != 2 {
t.Fatalf("count = %d, want 2", count)
}
}