Skip to content

Commit accfd2a

Browse files
mailpraksundarok
andauthored
feat: human friendly duration formatting (#59)
* feat: updates to fix time * feat: updates to fix time for all the tests * feat: add duration util file * feat: cleanup html files * feat: revert the deployment image * feat: removal of md file * feat: fixing test case coverages * feat: fixing compiling issues * fix: fixing route register issue * fix: fixing the test execution issue * fix: application startup issue fix * feat: updated to use the ginko framework and removed commented codes: issue-37 * feat: test execution duplicates resolved: issue-37 * Remove legacy handler test file * Apply automated lint fixes * updating the correct values in deployment file * removal of legacy code * removal of test files * feature: removing files with only space changes: #37 * fix: fix for the test run: #37 --------- Co-authored-by: Sundar OK <[email protected]>
1 parent aac8386 commit accfd2a

File tree

9 files changed

+616
-27
lines changed

9 files changed

+616
-27
lines changed

deployments/fern-platform-kubevela.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ spec:
604604
env:
605605
- name: LOG_LEVEL
606606
value: debug
607+
- name: FERN_USE_SPLIT_HANDLERS
608+
value: "true"
607609
- name: REDIS_HOST
608610
value: redis
609611
- name: REDIS_PORT

internal/api/domain_handler.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ func (h *DomainHandler) RegisterRoutes(router *gin.Engine) {
134134

135135
// Static file serving
136136
router.Static("/web", "./web")
137-
router.GET("/", func(c *gin.Context) {
138-
c.Redirect(http.StatusMovedPermanently, "/web/")
139-
})
140137
}
141138

142139
// Helper function to check if user is authenticated

internal/domains/testing/application/complete_test_run.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ func (h *CompleteTestRunHandler) Handle(ctx context.Context, cmd CompleteTestRun
4848

4949
// Complete the test run
5050
now := time.Now()
51+
// Always set EndTime and Status
5152
testRun.EndTime = &now
5253
testRun.Status = "completed"
53-
if testRun.StartTime.After(time.Time{}) {
54-
testRun.Duration = now.Sub(testRun.StartTime)
54+
55+
// If StartTime is zero, set it to now (fallback)
56+
if testRun.StartTime.IsZero() {
57+
testRun.StartTime = now
58+
}
59+
60+
// Calculate duration if both times are set
61+
if testRun.EndTime != nil && !testRun.StartTime.IsZero() {
62+
testRun.Duration = testRun.EndTime.Sub(testRun.StartTime)
5563
}
5664

5765
// Update flaky test statistics
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package application_test
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"github.com/stretchr/testify/mock"
10+
11+
"github.com/guidewire-oss/fern-platform/internal/domains/testing/application"
12+
"github.com/guidewire-oss/fern-platform/internal/domains/testing/domain"
13+
)
14+
15+
type mockTestRunRepo struct{ mock.Mock }
16+
17+
func (m *mockTestRunRepo) Create(ctx context.Context, testRun *domain.TestRun) error { return nil }
18+
func (m *mockTestRunRepo) GetByID(ctx context.Context, id uint) (*domain.TestRun, error) {
19+
return nil, nil
20+
}
21+
func (m *mockTestRunRepo) GetWithDetails(ctx context.Context, id uint) (*domain.TestRun, error) {
22+
return nil, nil
23+
}
24+
func (m *mockTestRunRepo) GetLatestByProjectID(ctx context.Context, projectID string, limit int) ([]*domain.TestRun, error) {
25+
return nil, nil
26+
}
27+
func (m *mockTestRunRepo) GetTestRunSummary(ctx context.Context, projectID string) (*domain.TestRunSummary, error) {
28+
return nil, nil
29+
}
30+
func (m *mockTestRunRepo) Delete(ctx context.Context, id uint) error { return nil }
31+
func (m *mockTestRunRepo) CountByProjectID(ctx context.Context, projectID string) (int64, error) {
32+
return 0, nil
33+
}
34+
func (m *mockTestRunRepo) GetRecent(ctx context.Context, limit int) ([]*domain.TestRun, error) {
35+
return nil, nil
36+
}
37+
func (m *mockTestRunRepo) GetByRunID(ctx context.Context, runID string) (*domain.TestRun, error) {
38+
args := m.Called(ctx, runID)
39+
return args.Get(0).(*domain.TestRun), args.Error(1)
40+
}
41+
func (m *mockTestRunRepo) Update(ctx context.Context, tr *domain.TestRun) error {
42+
args := m.Called(ctx, tr)
43+
return args.Error(0)
44+
}
45+
46+
type mockFlakyRepo struct{ mock.Mock }
47+
48+
func (m *mockFlakyRepo) Save(ctx context.Context, flakyTest *domain.FlakyTest) error { return nil }
49+
func (m *mockFlakyRepo) FindByProject(ctx context.Context, projectID string) ([]*domain.FlakyTest, error) {
50+
return nil, nil
51+
}
52+
func (m *mockFlakyRepo) FindByTestName(ctx context.Context, projectID, testName string) (*domain.FlakyTest, error) {
53+
return nil, nil
54+
}
55+
func (m *mockFlakyRepo) Update(ctx context.Context, flakyTest *domain.FlakyTest) error { return nil }
56+
57+
// Note: Suite entry point defined in test_run_service_test.go (TestApplication)
58+
59+
var _ = Describe("CompleteTestRunHandler", Label("unit", "application"), func() {
60+
var (
61+
mockRepo *mockTestRunRepo
62+
mockFlaky *mockFlakyRepo
63+
handler *application.CompleteTestRunHandler
64+
)
65+
66+
BeforeEach(func() {
67+
mockRepo = new(mockTestRunRepo)
68+
mockFlaky = new(mockFlakyRepo)
69+
handler = application.NewCompleteTestRunHandler(mockRepo, mockFlaky)
70+
})
71+
72+
Describe("Handle", func() {
73+
Context("when test run exists", func() {
74+
It("should complete the test run successfully", func() {
75+
tr := &domain.TestRun{RunID: "run-1", StartTime: time.Now().Add(-time.Hour)}
76+
mockRepo.On("GetByRunID", mock.Anything, "run-1").Return(tr, nil)
77+
mockRepo.On("Update", mock.Anything, tr).Return(nil)
78+
79+
err := handler.Handle(context.Background(), application.CompleteTestRunCommand{RunID: "run-1"})
80+
81+
Expect(err).ToNot(HaveOccurred())
82+
Expect(tr.Status).To(Equal("completed"))
83+
Expect(tr.EndTime).ToNot(BeNil())
84+
Expect(tr.Duration).To(BeNumerically(">", 0))
85+
})
86+
})
87+
88+
Context("when test run is not found", func() {
89+
It("should return an error", func() {
90+
mockRepo.On("GetByRunID", mock.Anything, "run-x").Return((*domain.TestRun)(nil), nil)
91+
92+
err := handler.Handle(context.Background(), application.CompleteTestRunCommand{RunID: "run-x"})
93+
94+
Expect(err).To(HaveOccurred())
95+
})
96+
})
97+
98+
Context("when run ID is empty", func() {
99+
It("should return an error", func() {
100+
err := handler.Handle(context.Background(), application.CompleteTestRunCommand{RunID: ""})
101+
102+
Expect(err).To(HaveOccurred())
103+
})
104+
})
105+
})
106+
})

internal/domains/testing/application/test_run_service.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ func (s *TestRunService) updateSuiteStatistics(ctx context.Context, suiteRunID u
180180

181181
for _, spec := range specRuns {
182182
totalTests++
183+
// Fallback: If EndTime is nil, set to now
184+
if spec.EndTime == nil {
185+
now := time.Now()
186+
spec.EndTime = &now
187+
}
188+
// Fallback: If StartTime is zero, set to EndTime
189+
if spec.StartTime.IsZero() && spec.EndTime != nil {
190+
spec.StartTime = *spec.EndTime
191+
}
192+
// Calculate duration if missing
193+
if spec.Duration == 0 && spec.EndTime != nil && !spec.StartTime.IsZero() {
194+
spec.Duration = spec.EndTime.Sub(spec.StartTime)
195+
}
183196
totalDuration += spec.Duration
184197

185198
switch spec.Status {
@@ -260,6 +273,10 @@ func (s *TestRunService) CreateSuiteRun(ctx context.Context, suiteRun *domain.Su
260273
if suiteRun.Status == "" {
261274
suiteRun.Status = "running"
262275
}
276+
// Always set StartTime if zero
277+
if suiteRun.StartTime.IsZero() {
278+
suiteRun.StartTime = time.Now()
279+
}
263280

264281
return s.suiteRunRepo.Create(ctx, suiteRun)
265282
}
@@ -274,10 +291,17 @@ func (s *TestRunService) CreateSpecRun(ctx context.Context, specRun *domain.Spec
274291
if specRun.Status == "" {
275292
specRun.Status = "pending"
276293
}
294+
295+
// Only auto-set StartTime if both StartTime and EndTime are zero
296+
if specRun.StartTime.IsZero() && (specRun.EndTime == nil || specRun.EndTime.IsZero()) {
297+
specRun.StartTime = time.Now()
298+
}
277299

278-
// Calculate duration if not set
279-
if specRun.EndTime != nil && !specRun.StartTime.IsZero() {
280-
specRun.Duration = specRun.EndTime.Sub(specRun.StartTime)
300+
// Calculate duration only if both times are set and EndTime is after StartTime
301+
if specRun.EndTime != nil && !specRun.StartTime.IsZero() && !specRun.EndTime.IsZero() {
302+
if specRun.EndTime.After(specRun.StartTime) {
303+
specRun.Duration = specRun.EndTime.Sub(specRun.StartTime)
304+
}
281305
}
282306

283307
return s.specRunRepo.Create(ctx, specRun)

0 commit comments

Comments
 (0)