package sends import ( "io" "log/slog" "os" "path/filepath" "testing" "amadan.net/rastrillo/rastrillo/budget" "amadan.net/rastrillo/rastrillo/db" ) func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } func TestRequestsStayWithViewer(t *testing.T) { d, err := db.Open(filepath.Join(t.TempDir(), "requests.db"), slog.New(slog.NewTextHandler(io.Discard, nil))) if err != nil { t.Fatal(err) } defer d.Close() for _, q := range []string{ `CREATE TABLE conversation_requests(principal text,operation text,request_id text,result text)`, `INSERT INTO conversation_requests VALUES('alice','send','alice-request','{"ID":"alice-message"}'),('bob','send','bob-request','{"ID":"bob-message"}')`, } { if _, err := d.Writer().Exec(q); err != nil { t.Fatal(err) } } ids := []string{"alice-message", "bob-message"} result, err := Requests(d.G, "alice", ids, func(id string) string { return id }) if err != nil { t.Fatal(err) } if len(result) != 1 || result["alice-message"] != "alice-request" { t.Fatalf("another viewer's request exposed: %v", result) } if err = d.Close(); err != nil { t.Fatal(err) } if _, err = Requests(d.G, "alice", ids, func(id string) string { return id }); err == nil { t.Fatal("lookup failure became an empty success") } }