// Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package integration import ( "fmt" "testing" "time" actions_model "gitea.dev/models/actions" "gitea.dev/models/db" "gitea.dev/modules/timeutil" "gitea.dev/tests" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TestFindRunnersSortByStatus verifies that sorting by status ranks runners by // their computed status (active, then idle, then offline) instead of interleaving // idle runners with active ones by raw last_online, and that a disabled runner // sinks to the bottom of its status group rather than mixing with enabled runners. // // It lives in tests/integration rather than a unit test because the status rank is // a database-evaluated CASE expression; unit tests only run against SQLite, so the // expression must be exercised against MySQL/PostgreSQL/MSSQL in CI to catch dialect // differences. func TestFindRunnersSortByStatus(t *testing.T) { defer tests.PrepareTestEnv(t)() ctx := t.Context() const ownerID = 1001 now := time.Now() // An idle runner that went online most recently would sort before an active // runner when ordering by last_online alone; the status rank must override that. insert := func(name string, lastOnline, lastActive time.Time, disabled bool) { require.NoError(t, db.Insert(ctx, &actions_model.ActionRunner{ Name: name, UUID: "STATUS-SORT-" + name, TokenHash: "status-sort-token-" + name, OwnerID: ownerID, LastOnline: timeutil.TimeStamp(lastOnline.Unix()), LastActive: timeutil.TimeStamp(lastActive.Unix()), IsDisabled: disabled, })) } // Each disabled runner has the most recent last_online within its status group, // so it would sort first in that group if the disabled flag were ignored; the // last_active value keeps it in the intended group (offline