aboutsummaryrefslogtreecommitdiff
path: root/internal/ingest/client_test.go
blob: ff8022568c86c1593e14443ce2b6d7245001ede8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ingest

import (
	"strings"
	"testing"
)

func TestDecodeExportBody_IgnoresTrailingPartialNDJSONLine(t *testing.T) {
	body := strings.Join([]string{
		`{"seq":1,"did":"did:plc:alice","cid":"cid1","operation":{"x":1}}`,
		`{"seq":2,"did":"did:plc:bob","cid":"cid2","operation":{"x":2}}`,
		`{"seq":3,"did":"did:plc:carol","cid":"cid3","operation":{"x":3}`,
	}, "\n")

	records, err := decodeExportBody(strings.NewReader(body), 0)
	if err != nil {
		t.Fatalf("decode export body: %v", err)
	}
	if len(records) != 2 {
		t.Fatalf("record count mismatch: got %d want 2", len(records))
	}
	if records[0].Seq != 1 || records[1].Seq != 2 {
		t.Fatalf("unexpected sequences: got [%d %d], want [1 2]", records[0].Seq, records[1].Seq)
	}
}

func TestDecodeExportBody_FailsOnMalformedNonTrailingNDJSONLine(t *testing.T) {
	body := strings.Join([]string{
		`{"seq":1,"did":"did:plc:alice","cid":"cid1","operation":{"x":1}}`,
		`{"seq":2,"did":"did:plc:bob","cid":"cid2","operation":{"x":2}`,
		`{"seq":3,"did":"did:plc:carol","cid":"cid3","operation":{"x":3}}`,
	}, "\n")

	_, err := decodeExportBody(strings.NewReader(body), 0)
	if err == nil {
		t.Fatalf("expected malformed middle line to fail")
	}
}