package ect import ( "testing" "time" ) func TestMemoryLedger_AppendAndGet(t *testing.T) { m := NewMemoryLedger() p := &Payload{Jti: "jti-1", Iss: "iss", ExecAct: "act", Par: []string{}, Iat: time.Now().Unix(), Exp: time.Now().Add(time.Hour).Unix()} seq, err := m.Append("jws1", p) if err != nil { t.Fatal(err) } if seq != 1 { t.Errorf("seq: got %d", seq) } got := m.GetByTid("jti-1") if got == nil || got.Jti != "jti-1" { t.Errorf("GetByTid: got %v", got) } } func TestMemoryLedger_ErrTaskIDExists(t *testing.T) { m := NewMemoryLedger() p := &Payload{Jti: "jti-dup", Iss: "i", ExecAct: "e", Par: []string{}, Iat: 1, Exp: 2} _, _ = m.Append("jws1", p) _, err := m.Append("jws2", p) if err != ErrTaskIDExists { t.Errorf("expected ErrTaskIDExists, got %v", err) } } func TestMemoryLedger_ContainsWid(t *testing.T) { m := NewMemoryLedger() p := &Payload{Jti: "j1", Wid: "wf1", Iss: "i", ExecAct: "e", Par: []string{}, Iat: 1, Exp: 2} _, _ = m.Append("jws", p) if !m.Contains("j1", "") { t.Error("Contains(j1, \"\") should be true") } if !m.Contains("j1", "wf1") { t.Error("Contains(j1, wf1) should be true") } if m.Contains("j1", "wf2") { t.Error("Contains(j1, wf2) should be false") } } func TestMemoryLedger_AppendNilPayload(t *testing.T) { m := NewMemoryLedger() seq, err := m.Append("jws", nil) if err != nil { t.Fatal(err) } if seq != 0 { t.Errorf("Append(nil) should return 0, got %d", seq) } }