Files

68 lines
1.6 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
//go:build linux || freebsd
package storage
import (
"bufio"
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestArchiveStoreRoundTrip(t *testing.T) {
path := filepath.Join(t.TempDir(), "nested", "archive-test.jsonl")
store := NewArchiveStore(path)
first := Submission{
Form: "contact",
Name: "Jane Doe",
Email: "jane@example.com",
Service: "architecture",
Message: "Hello, I would like to discuss an engagement.",
IP: "192.0.2.1",
}
if err := store.Append(first); err != nil {
t.Fatalf("append: %v", err)
}
second := Submission{Form: "feedback", Email: "other@example.com", Message: "Short"}
if err := store.Append(second); err != nil {
t.Fatalf("append: %v", err)
}
f, err := os.Open(path)
if err != nil {
t.Fatalf("open: %v", err)
}
defer f.Close()
var got []Submission
scanner := bufio.NewScanner(f)
for scanner.Scan() {
var sub Submission
if err := json.Unmarshal(scanner.Bytes(), &sub); err != nil {
t.Fatalf("unmarshal line: %v", err)
}
got = append(got, sub)
}
if err := scanner.Err(); err != nil {
t.Fatalf("scan: %v", err)
}
if len(got) != 2 {
t.Fatalf("lines = %d, want 2", len(got))
}
if got[0].CreatedAt.IsZero() {
t.Errorf("first created_at is zero, want a stamped time")
}
first.CreatedAt = got[0].CreatedAt
if got[0] != first {
t.Errorf("first = %+v, want %+v", got[0], first)
}
if got[1].CreatedAt.IsZero() {
t.Errorf("second created_at is zero, want a stamped time")
}
}