user_platform_quota_adapter_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //go:build unit
  2. package repository
  3. import (
  4. "context"
  5. "errors"
  6. "testing"
  7. "time"
  8. )
  9. type fakeRepoForAdapter struct {
  10. upsertCalledWith []UserPlatformQuotaRecord
  11. upsertCalledUserID int64
  12. upsertErr error
  13. resetCalledWith [4]any // userID, platform, window, newStart
  14. resetErr error
  15. }
  16. func (f *fakeRepoForAdapter) BulkInsertInitial(_ context.Context, _ []UserPlatformQuotaRecord) error {
  17. return nil
  18. }
  19. func (f *fakeRepoForAdapter) GetByUserPlatform(_ context.Context, _ int64, _ string) (*UserPlatformQuotaRecord, error) {
  20. return nil, nil
  21. }
  22. func (f *fakeRepoForAdapter) ListByUser(_ context.Context, _ int64) ([]UserPlatformQuotaRecord, error) {
  23. return nil, nil
  24. }
  25. func (f *fakeRepoForAdapter) IncrementUsageWithReset(_ context.Context, _ int64, _ string, _ float64, _ time.Time) error {
  26. return nil
  27. }
  28. func (f *fakeRepoForAdapter) ResetExpiredWindow(_ context.Context, userID int64, platform string, window string, newStart time.Time) error {
  29. f.resetCalledWith = [4]any{userID, platform, window, newStart}
  30. return f.resetErr
  31. }
  32. func (f *fakeRepoForAdapter) UpsertForUser(_ context.Context, userID int64, records []UserPlatformQuotaRecord) error {
  33. f.upsertCalledUserID = userID
  34. f.upsertCalledWith = records
  35. return f.upsertErr
  36. }
  37. func (f *fakeRepoForAdapter) BatchSnapshotUsage(_ context.Context, _ []UserPlatformQuotaSnapshot, _ time.Time) error {
  38. return nil
  39. }
  40. func TestGenericAdapter_UpsertForUser_ForwardsRecords(t *testing.T) {
  41. fake := &fakeRepoForAdapter{}
  42. adapter := NewUserPlatformQuotaServiceAdapter(fake)
  43. err := adapter.UpsertForUser(context.Background(), 42, nil)
  44. if err != nil {
  45. t.Fatalf("unexpected err: %v", err)
  46. }
  47. if fake.upsertCalledUserID != 42 {
  48. t.Errorf("forwarded userID = %d, want 42", fake.upsertCalledUserID)
  49. }
  50. }
  51. func TestGenericAdapter_UpsertForUser_PropagatesError(t *testing.T) {
  52. wantErr := errors.New("boom")
  53. fake := &fakeRepoForAdapter{upsertErr: wantErr}
  54. adapter := NewUserPlatformQuotaServiceAdapter(fake)
  55. err := adapter.UpsertForUser(context.Background(), 1, nil)
  56. if !errors.Is(err, wantErr) {
  57. t.Errorf("expected %v, got %v", wantErr, err)
  58. }
  59. }
  60. func TestGenericAdapter_ResetExpiredWindow_ForwardsAllParams(t *testing.T) {
  61. fake := &fakeRepoForAdapter{}
  62. adapter := NewUserPlatformQuotaServiceAdapter(fake)
  63. now := time.Date(2026, 5, 23, 10, 0, 0, 0, time.UTC)
  64. if err := adapter.ResetExpiredWindow(context.Background(), 7, "openai", "weekly", now); err != nil {
  65. t.Fatalf("unexpected err: %v", err)
  66. }
  67. if fake.resetCalledWith[0].(int64) != 7 ||
  68. fake.resetCalledWith[1].(string) != "openai" ||
  69. fake.resetCalledWith[2].(string) != "weekly" ||
  70. !fake.resetCalledWith[3].(time.Time).Equal(now) {
  71. t.Errorf("forwarded params mismatch: %+v", fake.resetCalledWith)
  72. }
  73. }
  74. func TestGenericAdapter_ResetExpiredWindow_PropagatesError(t *testing.T) {
  75. wantErr := errors.New("not found")
  76. fake := &fakeRepoForAdapter{resetErr: wantErr}
  77. adapter := NewUserPlatformQuotaServiceAdapter(fake)
  78. err := adapter.ResetExpiredWindow(context.Background(), 1, "a", "daily", time.Now())
  79. if !errors.Is(err, wantErr) {
  80. t.Errorf("expected %v, got %v", wantErr, err)
  81. }
  82. }