62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package export
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func sample(n int) Batch {
|
|
b := Batch{ID: "batch-abc", MintURL: "https://mint.example/w", MintPubkey: "02ab", MintName: "fake", Memo: "party", CreatedAt: time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC)}
|
|
for i := 1; i <= n; i++ {
|
|
id := "note-" + strings.Repeat("x", i)
|
|
lnurl := "LNURL1DP68GURN8GHJ7MRWW4EXCTNXD9SHG6NPVCHXXMMD9AKXUATJDSKHQCTE8AEK2UMND9HKU0TL" + strings.Repeat("A", i)
|
|
lnurlw := "lnurlw://mint.example/w?k1=" + strings.Repeat("a", 64)
|
|
b.Notes = append(b.Notes, Note{Index: i, ID: id, ShortID: "xxxxxx", AmountMsat: 21_000, LNURL: lnurl, LNURLW: lnurlw, Mint: "fake", Memo: "party", CreatedAt: b.CreatedAt})
|
|
}
|
|
return b
|
|
}
|
|
|
|
func TestCSVAndJSON(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
if err := CSV(&buf, sample(2)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
|
|
if len(lines) != 3 || lines[0] != "index,id,amount_sat,lnurl,lnurlw_url,mint,memo,created_at" || !strings.HasPrefix(lines[1], "1,note-x,21,LNURL1") {
|
|
t.Fatalf("csv:\n%s", buf.String())
|
|
}
|
|
buf.Reset()
|
|
if err := JSON(&buf, sample(2)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got map[string]interface{}
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got["batch_id"] != "batch-abc" || got["mint_pubkey"] != "02ab" || len(got["notes"].([]interface{})) != 2 {
|
|
t.Fatalf("json: %v", got)
|
|
}
|
|
buf.Reset()
|
|
if err := JSON(&buf, sample(0)); err != nil || !strings.Contains(buf.String(), `"notes": []`) {
|
|
t.Fatalf("empty batch must have an empty array: %s %v", buf.String(), err)
|
|
}
|
|
}
|
|
|
|
func TestPDFPages(t *testing.T) {
|
|
for notes, pages := range map[int]int{0: 1, 1: 1, 8: 1, 9: 2, 20: 3} {
|
|
var buf bytes.Buffer
|
|
if err := PDF(&buf, sample(notes)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.HasPrefix(buf.Bytes(), []byte("%PDF")) {
|
|
t.Fatal("not a pdf")
|
|
}
|
|
if got := bytes.Count(buf.Bytes(), []byte("/Type /Page\n")); got != pages {
|
|
t.Fatalf("%d notes: %d pages, want %d", notes, got, pages)
|
|
}
|
|
}
|
|
}
|