package cursor import ( "encoding/base64" "errors" "strings" "testing" "time" "github.com/google/uuid" ) // TestCodec_EncodeDecode preserves the complete position through an authenticated round trip. func TestCodec_EncodeDecode(t *testing.T) { // Arrange. codec := newTestCodec(t) position := Position{CreatedAt: time.Unix(1700000000, 123456789).UTC(), ID: uuid.New()} // Act. encoded, err := codec.Encode("chats:alice", position) // Assert. if err != nil { t.Fatal(err) } if encoded == "" { t.Fatal("encoded position is empty") } decoded, err := codec.Decode(encoded, "chats:alice") if err != nil || decoded != position { t.Fatalf("decoded=%v, want=%v, error=%v", decoded, position, err) } } // TestCodec_Decode checks first-page behavior and independent rejection conditions. func TestCodec_Decode(t *testing.T) { t.Run("Positive: empty token starts first page", func(t *testing.T) { // Arrange. codec := newTestCodec(t) // Act. got, err := codec.Decode("", "chats:alice") // Assert. if err != nil || got != (Position{}) { t.Fatalf("first-page position=%v, error=%v", got, err) } }) t.Run("Negative: another resource", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := encodeFixture(t, codec, "chats:alice") // Act. got, err := codec.Decode(encoded, "chats:bob") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) t.Run("Negative: another process key", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := encodeFixture(t, codec, "chats:alice") codec = newTestCodec(t) // Act. got, err := codec.Decode(encoded, "chats:alice") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) t.Run("Negative: tampered payload", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := encodeFixture(t, codec, "chats:alice") signed, err := base64.RawURLEncoding.DecodeString(encoded) if err != nil { t.Fatal(err) } signed[0] ^= 1 encoded = base64.RawURLEncoding.EncodeToString(signed) // Act. got, err := codec.Decode(encoded, "chats:alice") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) t.Run("Negative: oversized token", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := strings.Repeat("a", 1025) // Act. got, err := codec.Decode(encoded, "chats:alice") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) t.Run("Negative: malformed base64", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := "!invalid!" // Act. got, err := codec.Decode(encoded, "chats:alice") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) t.Run("Negative: signed malformed JSON", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := signedPayload(codec, []byte("not JSON")) // Act. got, err := codec.Decode(encoded, "chats:alice") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) t.Run("Negative: signed zero timestamp", func(t *testing.T) { // Arrange. codec := newTestCodec(t) encoded := signedPayload(codec, []byte(`{"r":"chats:alice","t":"0001-01-01T00:00:00Z","i":"11111111-1111-1111-1111-111111111111"}`)) // Act. got, err := codec.Decode(encoded, "chats:alice") // Assert. if !errors.Is(err, ErrInvalid) || got != (Position{}) { t.Fatalf("position=%v, error=%v", got, err) } }) } // signedPayload creates authenticated malformed fixtures to isolate payload validation. func signedPayload(codec *Codec, payload []byte) string { return base64.RawURLEncoding.EncodeToString(append(payload, codec.signature(payload)...)) } // newTestCodec creates an independent signing key for one scenario. func newTestCodec(t *testing.T) *Codec { t.Helper() codec, err := New() if err != nil { t.Fatal(err) } return codec } // encodeFixture prepares a valid token before testing a separate decoding condition. func encodeFixture(t *testing.T, codec *Codec, resource string) string { t.Helper() token, err := codec.Encode(resource, Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()}) if err != nil { t.Fatal(err) } return token }