test: add preview lifecycle integration test
Covers full Create→List→Extend→Get→Delete cycle and CleanupExpired behaviour via fakeStore, exercising the Service end-to-end.
- commit
- d9532c91012577f757623524bcc7dfc4c935c4c4
- author
- brice · · 2026-03-19 05:12 UTC
- committer
- brice · · 2026-03-19 05:13 UTC
- parent
- ad3801f
- signature
- unsigned
1 file · +88 -0
A internal/usecase/auto/preview/integration_test.go +88 -0
| @@ -0,0 +1,88 @@ | |||
| 1 | + | package preview | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "context" | |
| 5 | + | "testing" | |
| 6 | + | "time" | |
| 7 | + | ||
| 8 | + | "github.com/bnema/gordon/internal/domain" | |
| 9 | + | "github.com/stretchr/testify/assert" | |
| 10 | + | "github.com/stretchr/testify/require" | |
| 11 | + | ) | |
| 12 | + | ||
| 13 | + | func TestPreviewLifecycle_Integration(t *testing.T) { | |
| 14 | + | store := &fakeStore{} | |
| 15 | + | svc := NewService(store, 2*time.Second) | |
| 16 | + | ||
| 17 | + | ctx := context.Background() | |
| 18 | + | ||
| 19 | + | // Create | |
| 20 | + | svc.CreatePreview(ctx, CreatePreviewRequest{ | |
| 21 | + | Name: "test-feat", | |
| 22 | + | Domain: "myapp--test-feat.example.com", | |
| 23 | + | BaseRoute: "myapp.example.com", | |
| 24 | + | Image: "myapp:preview-test-feat", | |
| 25 | + | HTTPS: true, | |
| 26 | + | PreviewConfig: domain.PreviewConfig{ | |
| 27 | + | TTL: 2 * time.Second, | |
| 28 | + | Separator: "--", | |
| 29 | + | DataCopy: false, | |
| 30 | + | }, | |
| 31 | + | }) | |
| 32 | + | ||
| 33 | + | // Verify created | |
| 34 | + | all, err := svc.List(ctx) | |
| 35 | + | require.NoError(t, err) | |
| 36 | + | require.Len(t, all, 1) | |
| 37 | + | assert.Equal(t, "test-feat", all[0].Name) | |
| 38 | + | assert.Equal(t, "myapp--test-feat.example.com", all[0].Domain) | |
| 39 | + | assert.Equal(t, "myapp.example.com", all[0].BaseRoute) | |
| 40 | + | ||
| 41 | + | // Extend | |
| 42 | + | err = svc.Extend(ctx, "test-feat", 1*time.Hour) | |
| 43 | + | require.NoError(t, err) | |
| 44 | + | ||
| 45 | + | p, err := svc.Get(ctx, "test-feat") | |
| 46 | + | require.NoError(t, err) | |
| 47 | + | assert.True(t, p.ExpiresAt.After(time.Now().Add(55*time.Minute))) | |
| 48 | + | ||
| 49 | + | // Delete | |
| 50 | + | err = svc.Delete(ctx, "test-feat") | |
| 51 | + | require.NoError(t, err) | |
| 52 | + | ||
| 53 | + | all, _ = svc.List(ctx) | |
| 54 | + | assert.Empty(t, all) | |
| 55 | + | } | |
| 56 | + | ||
| 57 | + | func TestPreviewLifecycle_CleanupExpired(t *testing.T) { | |
| 58 | + | store := &fakeStore{} | |
| 59 | + | svc := NewService(store, 1*time.Millisecond) | |
| 60 | + | ||
| 61 | + | ctx := context.Background() | |
| 62 | + | ||
| 63 | + | // Create with very short TTL | |
| 64 | + | svc.CreatePreview(ctx, CreatePreviewRequest{ | |
| 65 | + | Name: "short-lived", | |
| 66 | + | Domain: "myapp--short-lived.example.com", | |
| 67 | + | BaseRoute: "myapp.example.com", | |
| 68 | + | Image: "myapp:preview-short-lived", | |
| 69 | + | HTTPS: true, | |
| 70 | + | PreviewConfig: domain.PreviewConfig{ | |
| 71 | + | TTL: 1 * time.Millisecond, | |
| 72 | + | Separator: "--", | |
| 73 | + | DataCopy: false, | |
| 74 | + | }, | |
| 75 | + | }) | |
| 76 | + | ||
| 77 | + | // Wait for expiry | |
| 78 | + | time.Sleep(10 * time.Millisecond) | |
| 79 | + | ||
| 80 | + | // Cleanup should find it | |
| 81 | + | expired := svc.CleanupExpired(ctx) | |
| 82 | + | assert.Len(t, expired, 1) | |
| 83 | + | assert.Equal(t, "short-lived", expired[0].Name) | |
| 84 | + | ||
| 85 | + | // Should be gone | |
| 86 | + | all, _ := svc.List(ctx) | |
| 87 | + | assert.Empty(t, all) | |
| 88 | + | } | |
| @@ -0,0 +1,88 @@ | |||||
| 1 | + | package preview | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "context" | |||
| 5 | + | "testing" | |||
| 6 | + | "time" | |||
| 7 | + | ||||
| 8 | + | "github.com/bnema/gordon/internal/domain" | |||
| 9 | + | "github.com/stretchr/testify/assert" | |||
| 10 | + | "github.com/stretchr/testify/require" | |||
| 11 | + | ) | |||
| 12 | + | ||||
| 13 | + | func TestPreviewLifecycle_Integration(t *testing.T) { | |||
| 14 | + | store := &fakeStore{} | |||
| 15 | + | svc := NewService(store, 2*time.Second) | |||
| 16 | + | ||||
| 17 | + | ctx := context.Background() | |||
| 18 | + | ||||
| 19 | + | // Create | |||
| 20 | + | svc.CreatePreview(ctx, CreatePreviewRequest{ | |||
| 21 | + | Name: "test-feat", | |||
| 22 | + | Domain: "myapp--test-feat.example.com", | |||
| 23 | + | BaseRoute: "myapp.example.com", | |||
| 24 | + | Image: "myapp:preview-test-feat", | |||
| 25 | + | HTTPS: true, | |||
| 26 | + | PreviewConfig: domain.PreviewConfig{ | |||
| 27 | + | TTL: 2 * time.Second, | |||
| 28 | + | Separator: "--", | |||
| 29 | + | DataCopy: false, | |||
| 30 | + | }, | |||
| 31 | + | }) | |||
| 32 | + | ||||
| 33 | + | // Verify created | |||
| 34 | + | all, err := svc.List(ctx) | |||
| 35 | + | require.NoError(t, err) | |||
| 36 | + | require.Len(t, all, 1) | |||
| 37 | + | assert.Equal(t, "test-feat", all[0].Name) | |||
| 38 | + | assert.Equal(t, "myapp--test-feat.example.com", all[0].Domain) | |||
| 39 | + | assert.Equal(t, "myapp.example.com", all[0].BaseRoute) | |||
| 40 | + | ||||
| 41 | + | // Extend | |||
| 42 | + | err = svc.Extend(ctx, "test-feat", 1*time.Hour) | |||
| 43 | + | require.NoError(t, err) | |||
| 44 | + | ||||
| 45 | + | p, err := svc.Get(ctx, "test-feat") | |||
| 46 | + | require.NoError(t, err) | |||
| 47 | + | assert.True(t, p.ExpiresAt.After(time.Now().Add(55*time.Minute))) | |||
| 48 | + | ||||
| 49 | + | // Delete | |||
| 50 | + | err = svc.Delete(ctx, "test-feat") | |||
| 51 | + | require.NoError(t, err) | |||
| 52 | + | ||||
| 53 | + | all, _ = svc.List(ctx) | |||
| 54 | + | assert.Empty(t, all) | |||
| 55 | + | } | |||
| 56 | + | ||||
| 57 | + | func TestPreviewLifecycle_CleanupExpired(t *testing.T) { | |||
| 58 | + | store := &fakeStore{} | |||
| 59 | + | svc := NewService(store, 1*time.Millisecond) | |||
| 60 | + | ||||
| 61 | + | ctx := context.Background() | |||
| 62 | + | ||||
| 63 | + | // Create with very short TTL | |||
| 64 | + | svc.CreatePreview(ctx, CreatePreviewRequest{ | |||
| 65 | + | Name: "short-lived", | |||
| 66 | + | Domain: "myapp--short-lived.example.com", | |||
| 67 | + | BaseRoute: "myapp.example.com", | |||
| 68 | + | Image: "myapp:preview-short-lived", | |||
| 69 | + | HTTPS: true, | |||
| 70 | + | PreviewConfig: domain.PreviewConfig{ | |||
| 71 | + | TTL: 1 * time.Millisecond, | |||
| 72 | + | Separator: "--", | |||
| 73 | + | DataCopy: false, | |||
| 74 | + | }, | |||
| 75 | + | }) | |||
| 76 | + | ||||
| 77 | + | // Wait for expiry | |||
| 78 | + | time.Sleep(10 * time.Millisecond) | |||
| 79 | + | ||||
| 80 | + | // Cleanup should find it | |||
| 81 | + | expired := svc.CleanupExpired(ctx) | |||
| 82 | + | assert.Len(t, expired, 1) | |||
| 83 | + | assert.Equal(t, "short-lived", expired[0].Name) | |||
| 84 | + | ||||
| 85 | + | // Should be gone | |||
| 86 | + | all, _ := svc.List(ctx) | |||
| 87 | + | assert.Empty(t, all) | |||
| 88 | + | } | |||