package oltp
import (
"path/filepath"
"testing"
)
// A chat file is found by its line and name, dropped with its line,
// pruned with the channel's window, and counted as shared while any
// other line still names its bytes.
func TestChatFilesFollowTheirLines(t *testing.T) {
db, err := Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
add := func(seq uint64, name, sha string) {
if err := db.AddChatFile(ChatFile{Repo: "r", Seq: seq, Name: name, Size: 3, Type: "image/png", SHA256: sha, Who: "lou"}); err != nil {
t.Fatal(err)
}
}
add(5, "a.png", "aa")
add(5, "b.png", "bb")
add(9, "a.png", "aa")
add(12, "c.png", "cc")
if f, ok, err := db.ChatFile("r", 5, "b.png"); err != nil || !ok || f.SHA256 != "bb" {
t.Errorf("FAIL: ChatFile(5, b.png) = %+v %v %v", f, ok, err)
}
if _, ok, _ := db.ChatFile("r", 9, "b.png"); ok {
t.Error("FAIL: line 9 answered for a file only line 5 carries")
}
gone, err := db.DropChatFiles("r", 5)
if err != nil || len(gone) != 2 {
t.Fatalf("FAIL: DropChatFiles = %v %v", gone, err)
}
if !db.ChatFileShared("aa") || db.ChatFileShared("bb") {
t.Error("FAIL: shared bytes miscounted after the drop")
}
gone, err = db.PruneChatFiles("r", 12)
if err != nil || len(gone) != 1 || gone[0].Seq != 9 {
t.Fatalf("FAIL: PruneChatFiles(12) = %v %v", gone, err)
}
if _, ok, _ := db.ChatFile("r", 12, "c.png"); !ok {
t.Error("FAIL: the window's own line lost its file")
}
t.Log("ok: chat files follow their lines")
}