package inbox import ( "bytes" "encoding/json" "errors" "testing" "time" orbit "git.yuntuo.hk/k2safe/orbit/sdk/go" ) func newID(t *testing.T) string { t.Helper() id, err := orbit.NewEventMessageID() if err != nil { t.Fatal(err) } return id } func testEvent(t *testing.T, queue string) orbit.ResultEvent { t.Helper() task := newID(t) return orbit.ResultEvent{ID: newID(t), Sequence: 7, Type: "task.succeeded", AggregateKind: "task", AggregateID: task, AggregateRevision: 9007199254740993, OccurredAt: time.Now().UTC().Truncate(time.Microsecond), Details: json.RawMessage(`{"origin":"worker"}`), Snapshot: orbit.TaskResultSnapshot{SchemaVersion: 1, TaskID: task, QueueID: queue, State: orbit.TaskSucceeded, Revision: 9007199254740993, Result: json.RawMessage(`{"n":9007199254740993,"text":"<&>"}`)}} } func TestFrozenEventIdentity(t *testing.T) { s := &Store{consumer: "result-view", queue: newID(t)} e := testEvent(t, s.queue) raw, a, err := s.document(e) if err != nil || !bytes.Contains(raw, []byte("9007199254740993")) || !bytes.Contains(raw, []byte("<&>")) { t.Fatal("precision changed", err) } e.Snapshot.Result = json.RawMessage(`{ "text": "<&>", "n": 9007199254740993 }`) _, b, err := s.document(e) if err != nil || a != b { t.Fatal("equivalent formatting changed identity", err) } e.OccurredAt = e.OccurredAt.In(time.FixedZone("offset", 8*3600)) _, b, err = s.document(e) if err != nil || a != b { t.Fatal("timezone representation changed event identity", err) } e.Snapshot.Result = json.RawMessage(`{"n":9007199254740994,"text":"<&>"}`) _, b, err = s.document(e) if err != nil || a == b { t.Fatal("changed result not detected", err) } e.Snapshot.QueueID = newID(t) if _, _, err = s.document(e); !errors.Is(err, ErrInvalidEvent) { t.Fatal("wrong receiver scope accepted", err) } } func TestCanonicalRejectsDuplicateAndInvalidJSON(t *testing.T) { for _, raw := range []string{`{"a":1,"a":2}`, `{"a":null,"a":2}`, `{"a":1} {}`, `{"a":`, `{"a":"\ud800"}`, `{"a":"\udfff"}`, "\xff"} { if _, err := canonicalJSON([]byte(raw)); !errors.Is(err, ErrInvalidEvent) { t.Fatal("invalid document accepted") } } }