Fix archive reindex and add target dedup
This commit is contained in:
parent
e612896e06
commit
c83225f5ad
9 changed files with 796 additions and 6 deletions
|
|
@ -118,3 +118,100 @@ func TestZstdMboxRoundTripAndIndexRead(t *testing.T) {
|
|||
t.Fatalf("unexpected message body: %q", raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlainMboxPartialIndexIsRebuiltBeforeList(t *testing.T) {
|
||||
oldCfg, oldDB := Cfg, DB
|
||||
root := t.TempDir()
|
||||
t.Cleanup(func() {
|
||||
if DB != nil {
|
||||
_ = DB.Close()
|
||||
}
|
||||
Cfg, DB = oldCfg, oldDB
|
||||
})
|
||||
|
||||
Cfg = Config{
|
||||
DBPath: filepath.Join(root, "mail-graveyard.db"),
|
||||
MboxRoot: filepath.Join(root, "backup"),
|
||||
MboxCompression: "none",
|
||||
}
|
||||
if err := ConnectDB(true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SaveArchiveMailbox("archive"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SaveAccount(Account{
|
||||
Name: "test-account",
|
||||
SrcHost: "source.example",
|
||||
SrcPort: 993,
|
||||
SrcSecurity: "tls",
|
||||
SrcUser: "source@example.com",
|
||||
SrcPass: "x",
|
||||
DstHost: "target.example",
|
||||
DstPort: 993,
|
||||
DstSecurity: "tls",
|
||||
DstUser: "target@example.com",
|
||||
DstPass: "x",
|
||||
MboxDir: "archive",
|
||||
Active: true,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account, err := GetAccount("test-account")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writer, err := NewMboxWriter(filepath.Join(Cfg.MboxRoot, "archive"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first := testRawMessage("first@example.com", "First")
|
||||
firstInfo, err := writer.Append("INBOX", first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SaveMboxIndex(MboxIndexEntry{
|
||||
AccountID: account.ID,
|
||||
Folder: "INBOX",
|
||||
MessageID: first.MessageID,
|
||||
Subject: firstInfo.Subject,
|
||||
From: firstInfo.From,
|
||||
Date: firstInfo.Date,
|
||||
FileOffset: firstInfo.FileOffset,
|
||||
FrameLen: firstInfo.FrameLen,
|
||||
InnerOffset: firstInfo.InnerOffset,
|
||||
InnerLen: firstInfo.InnerLen,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := UpdateMboxIndexState(account.ID, "INBOX", firstInfo.FileOffset+firstInfo.FrameLen); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := writer.Append("INBOX", testRawMessage("second@example.com", "Second")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entries, err := ReadMboxList(firstInfo.Path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("expected rebuilt full list with 2 entries, got %#v", entries)
|
||||
}
|
||||
if entries[0].Subject != "First" || entries[1].Subject != "Second" {
|
||||
t.Fatalf("unexpected entries after reindex: %#v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func testRawMessage(id, subject string) RawMessage {
|
||||
return RawMessage{
|
||||
MessageID: id,
|
||||
InternalDate: time.Date(2026, 7, 14, 10, 11, 12, 0, time.UTC),
|
||||
Body: []byte("Message-ID: <" + id + ">\r\n" +
|
||||
"From: Sender <sender@example.com>\r\n" +
|
||||
"Subject: " + subject + "\r\n" +
|
||||
"Date: Tue, 14 Jul 2026 10:11:12 +0000\r\n" +
|
||||
"\r\n" +
|
||||
"Hello\r\n"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue