package cache

import (
    "testing"

    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

func TestDefaultKeyGenerator_DefaultPrefix(t *testing.T) {
    g := NewDefaultKeyGenerator("")
    require.NotNil(t, g)
    // Table-driven tests for key generation
    tests := []struct {
        name      string
        entity    string
        id        uint
        page      int
        pageSize  int
        queryName string
        params    []interface{}
        wantEntity string
        wantList   string
        wantQuery  string
    }{
        {
            name:      "basic",
            entity:    "user",
            id:        42,
            page:      1,
            pageSize:  20,
            queryName: "byEmail",
            params:    []interface{}{"foo@bar.com"},
            wantEntity: "tercul:user:id:42",
            wantList:   "tercul:user:list:1:20",
            wantQuery:  "tercul:user:byEmail:foo@bar.com",
        },
        {
            name:      "different entity and multiple params",
            entity:    "work",
            id:        7,
            page:      3,
            pageSize:  15,
            queryName: "search",
            params:    []interface{}{"abc", 2020, true},
            wantEntity: "tercul:work:id:7",
            wantList:   "tercul:work:list:3:15",
            wantQuery:  "tercul:work:search:abc:2020:true",
        },
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            assert.Equal(t, tt.wantEntity, g.EntityKey(tt.entity, tt.id))
            assert.Equal(t, tt.wantList, g.ListKey(tt.entity, tt.page, tt.pageSize))
            assert.Equal(t, tt.wantQuery, g.QueryKey(tt.entity, tt.queryName, tt.params...))
        })
    }
}

func TestDefaultKeyGenerator_CustomPrefix(t *testing.T) {
    g := NewDefaultKeyGenerator("mypfx:")
    require.NotNil(t, g)

    assert.Equal(t, "mypfx:book:id:1", g.EntityKey("book", 1))
    assert.Equal(t, "mypfx:book:list:2:10", g.ListKey("book", 2, 10))
    assert.Equal(t, "mypfx:book:find:tag:99", g.QueryKey("book", "find", "tag", 99))
}
