Files
gitea/models/actions/run_job_list_test.go
Nicolas 187daac598 fix: Sort action run jobs by JobID and Name with matrix examples (#37046)
Fix the sorting of jobs out of a matrix

## Before
<img width="415" height="487" alt="grafik"
src="https://github.com/user-attachments/assets/b628adb9-9158-4106-89f1-d8ecaa98f17d"
/>


## After

<img width="423" height="365" alt="grafik"
src="https://github.com/user-attachments/assets/d26223d5-96da-4bdc-bbfe-389101d28cc8"
/>

---------

Signed-off-by: Nicolas <bircni@icloud.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2026-05-13 07:30:22 +00:00

62 lines
1.5 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestActionJobList_SortMatrixGroupsByName(t *testing.T) {
mk := func(jobID, name string) *ActionRunJob {
return &ActionRunJob{JobID: jobID, Name: name}
}
names := func(jobs ActionJobList) []string {
out := make([]string, len(jobs))
for i, j := range jobs {
out[i] = j.Name
}
return out
}
t.Run("matrix group sorted naturally", func(t *testing.T) {
jobs := ActionJobList{
mk("build", "build"),
mk("test", "test (10)"),
mk("test", "test (2)"),
mk("test", "test (1)"),
mk("deploy", "deploy"),
}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"build", "test (1)", "test (2)", "test (10)", "deploy"}, names(jobs))
})
t.Run("non-adjacent same JobID stays in input order", func(t *testing.T) {
jobs := ActionJobList{
mk("test", "test (10)"),
mk("build", "build"),
mk("test", "test (1)"),
}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"test (10)", "build", "test (1)"}, names(jobs))
})
t.Run("groups stay in input order", func(t *testing.T) {
jobs := ActionJobList{
mk("z", "z"),
mk("a", "a"),
}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"z", "a"}, names(jobs))
})
t.Run("empty and singleton", func(t *testing.T) {
ActionJobList(nil).SortMatrixGroupsByName()
jobs := ActionJobList{mk("only", "only")}
jobs.SortMatrixGroupsByName()
assert.Equal(t, []string{"only"}, names(jobs))
})
}