|
| 1 | +import type { OverviewService } from "@dokploy/server/services/overview"; |
| 2 | +import { sortOverviewServices } from "@dokploy/server/services/overview"; |
| 3 | +import { describe, expect, test } from "vitest"; |
| 4 | + |
| 5 | +const makeService = (overrides: Partial<OverviewService>): OverviewService => ({ |
| 6 | + id: "id", |
| 7 | + type: "application", |
| 8 | + name: "name", |
| 9 | + appName: "app-name", |
| 10 | + status: "running", |
| 11 | + createdAt: "2024-01-01T00:00:00.000Z", |
| 12 | + serverId: null, |
| 13 | + serverName: null, |
| 14 | + icon: null, |
| 15 | + projectId: "project", |
| 16 | + projectName: "Project", |
| 17 | + environmentId: "environment", |
| 18 | + environmentName: "Environment", |
| 19 | + lastDeployAt: null, |
| 20 | + ...overrides, |
| 21 | +}); |
| 22 | + |
| 23 | +describe("sortOverviewServices", () => { |
| 24 | + test("sorts by name asc/desc", () => { |
| 25 | + const services = [ |
| 26 | + makeService({ id: "b", name: "Bravo" }), |
| 27 | + makeService({ id: "a", name: "Alpha" }), |
| 28 | + makeService({ id: "c", name: "Charlie" }), |
| 29 | + ]; |
| 30 | + |
| 31 | + expect(sortOverviewServices(services, "name-asc").map((s) => s.id)).toEqual( |
| 32 | + ["a", "b", "c"], |
| 33 | + ); |
| 34 | + expect( |
| 35 | + sortOverviewServices(services, "name-desc").map((s) => s.id), |
| 36 | + ).toEqual(["c", "b", "a"]); |
| 37 | + }); |
| 38 | + |
| 39 | + test("sorts by type asc/desc", () => { |
| 40 | + const services = [ |
| 41 | + makeService({ id: "app", type: "application" }), |
| 42 | + makeService({ id: "pg", type: "postgres" }), |
| 43 | + makeService({ id: "comp", type: "compose" }), |
| 44 | + ]; |
| 45 | + |
| 46 | + expect(sortOverviewServices(services, "type-asc").map((s) => s.id)).toEqual( |
| 47 | + ["app", "comp", "pg"], |
| 48 | + ); |
| 49 | + expect( |
| 50 | + sortOverviewServices(services, "type-desc").map((s) => s.id), |
| 51 | + ).toEqual(["pg", "comp", "app"]); |
| 52 | + }); |
| 53 | + |
| 54 | + test("sorts by createdAt asc/desc", () => { |
| 55 | + const services = [ |
| 56 | + makeService({ id: "old", createdAt: "2023-01-01T00:00:00.000Z" }), |
| 57 | + makeService({ id: "new", createdAt: "2024-06-01T00:00:00.000Z" }), |
| 58 | + makeService({ id: "mid", createdAt: "2023-12-01T00:00:00.000Z" }), |
| 59 | + ]; |
| 60 | + |
| 61 | + expect( |
| 62 | + sortOverviewServices(services, "createdAt-asc").map((s) => s.id), |
| 63 | + ).toEqual(["old", "mid", "new"]); |
| 64 | + expect( |
| 65 | + sortOverviewServices(services, "createdAt-desc").map((s) => s.id), |
| 66 | + ).toEqual(["new", "mid", "old"]); |
| 67 | + }); |
| 68 | + |
| 69 | + test("sorts by lastDeploy asc/desc, with never-deployed services always last", () => { |
| 70 | + const services = [ |
| 71 | + makeService({ id: "never", lastDeployAt: null }), |
| 72 | + makeService({ id: "old", lastDeployAt: "2023-01-01T00:00:00.000Z" }), |
| 73 | + makeService({ id: "new", lastDeployAt: "2024-06-01T00:00:00.000Z" }), |
| 74 | + ]; |
| 75 | + |
| 76 | + expect( |
| 77 | + sortOverviewServices(services, "lastDeploy-desc").map((s) => s.id), |
| 78 | + ).toEqual(["new", "old", "never"]); |
| 79 | + expect( |
| 80 | + sortOverviewServices(services, "lastDeploy-asc").map((s) => s.id), |
| 81 | + ).toEqual(["old", "new", "never"]); |
| 82 | + }); |
| 83 | + |
| 84 | + test("lastDeploy sort with all services never deployed is a no-op", () => { |
| 85 | + const services = [ |
| 86 | + makeService({ id: "a", lastDeployAt: null }), |
| 87 | + makeService({ id: "b", lastDeployAt: null }), |
| 88 | + ]; |
| 89 | + |
| 90 | + expect( |
| 91 | + sortOverviewServices(services, "lastDeploy-desc").map((s) => s.id), |
| 92 | + ).toEqual(["a", "b"]); |
| 93 | + }); |
| 94 | + |
| 95 | + test("does not mutate the input array", () => { |
| 96 | + const services = [ |
| 97 | + makeService({ id: "b", name: "Bravo" }), |
| 98 | + makeService({ id: "a", name: "Alpha" }), |
| 99 | + ]; |
| 100 | + const original = [...services]; |
| 101 | + |
| 102 | + sortOverviewServices(services, "name-asc"); |
| 103 | + |
| 104 | + expect(services).toEqual(original); |
| 105 | + }); |
| 106 | +}); |
0 commit comments