package reports_test import ( "context" "encoding/json" "testing" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgxpool" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "ruben/inventory2/consts" "ruben/inventory2/domains/accounts" "ruben/inventory2/domains/reports" "ruben/inventory2/internal/testdb" ) // setupAmazonMockShop creates a fresh account and an Amazon mock shop for // it, and registers cleanup for every row it creates, in FK-safe order // (shop_amazon[_events] -> mock.accounts -> accounts; oauth_users cleanup // is handled by testdb.SeedOAuthUser itself). func setupAmazonMockShop(t *testing.T, pool *pgxpool.Pool, acctStore *accounts.Store) (acctID int64, shopID string) { t.Helper() ctx := context.Background() userID := testdb.NewUserID(t) testdb.SeedOAuthUser(t, pool, userID) acct, err := acctStore.CreateAccount(ctx, userID, userID+"@example.com") require.NoError(t, err, "CreateAccount()") t.Cleanup(func() { pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID) }) id, err := acctStore.CreateMockShop(ctx, acct.AccountID, accounts.Amazon, "Test Shop") require.NoError(t, err, "CreateMockShop()") shopID = id.String() t.Cleanup(func() { ctx := context.Background() // a raw_shop_events insert for platform='amazon' fires a trigger // that also populates shop_amazon_events - clean that up too even // though these tests don't touch the amazon event processor. pool.Exec(ctx, "DELETE FROM mock.shop_amazon_events WHERE shop_id = $1", shopID) pool.Exec(ctx, "DELETE FROM mock.raw_shop_events WHERE shop_id = $1", shopID) pool.Exec(ctx, "DELETE FROM mock.shop_amazon WHERE account_id = $1", acct.AccountID) pool.Exec(ctx, "DELETE FROM mock.accounts WHERE account_id = $1", acct.AccountID) }) return acct.AccountID, shopID } func insertRawShopEvent(t *testing.T, pool *pgxpool.Pool, shopID, eventID string, ts time.Time, payload string) { t.Helper() _, err := pool.Exec(context.Background(), ` INSERT INTO mock.raw_shop_events (platform, shop_id, event_timestamp, event_id, raw_payload) VALUES ('amazon', $1, $2, $3, $4::jsonb) `, shopID, ts, eventID, payload) require.NoError(t, err, "insert raw shop event") } func TestGetRawShopEvents(t *testing.T) { pool := testdb.Pool(t) acctStore := accounts.NewStore(testdb.Logger(), pool) reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore) ctx := context.Background() acctID, shopID := setupAmazonMockShop(t, pool, acctStore) older := time.Now().Add(-time.Hour).UTC() newer := time.Now().UTC() insertRawShopEvent(t, pool, shopID, "evt-1", older, `{"n":1}`) insertRawShopEvent(t, pool, shopID, "evt-2", newer, `{"n":2}`) got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Amazon, shopID) require.NoError(t, err, "GetRawShopEvents()") require.Len(t, got, 2, "GetRawShopEvents()") // ordered event_timestamp DESC, event_id ASC - newest first. assert.Equal(t, "evt-2", got[0].EventID, "GetRawShopEvents()[0]") assert.Equal(t, "evt-1", got[1].EventID, "GetRawShopEvents()[1]") assert.Equal(t, accounts.Amazon, got[0].Platform, "got[0].Platform") assert.Equal(t, shopID, got[0].ShopID, "got[0].ShopID") var payload struct{ N int } require.NoError(t, json.Unmarshal(got[0].RawPayload, &payload), "unmarshal got[0].RawPayload") assert.Equal(t, 2, payload.N, "got[0].RawPayload n") } func TestGetRawShopEvents_NoEvents(t *testing.T) { pool := testdb.Pool(t) acctStore := accounts.NewStore(testdb.Logger(), pool) reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore) ctx := context.Background() acctID, shopID := setupAmazonMockShop(t, pool, acctStore) got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Amazon, shopID) require.NoError(t, err, "GetRawShopEvents()") assert.Empty(t, got, "GetRawShopEvents()") } func TestGetRawShopEvents_UnknownShop(t *testing.T) { pool := testdb.Pool(t) acctStore := accounts.NewStore(testdb.Logger(), pool) reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore) ctx := context.Background() userID := testdb.NewUserID(t) testdb.SeedOAuthUser(t, pool, userID) acct, err := acctStore.CreateAccount(ctx, userID, userID+"@example.com") require.NoError(t, err, "CreateAccount()") t.Cleanup(func() { pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID) }) _, err = reportsStore.GetRawShopEvents(ctx, acct.AccountID, accounts.Amazon, "no-such-shop-"+uuid.NewString()) require.ErrorIs(t, err, consts.ErrNotFound, "GetRawShopEvents()") }