eclipse/lib/common/encode_test.go

68 lines
2.1 KiB
Go
Raw Normal View History

// Copyright (C) 2024 Umorpha Systems
// SPDX-License-Identifier: AGPL-3.0-or-later
package common_test
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.mothstuff.lol/lukeshu/eclipse/lib/common"
)
func TestJobJSON(t *testing.T) {
job := common.Job{
ID: 3,
TriggerExpressions: []string{`{"repo_url":"https://git.lukeshu.com/btrfs-progs-ng","rev":"HEAD"}`},
TriggerValues: []string{"76f668122f9feeb20e885be330990a750107d5dd"},
Command: `set -x; for i in {1..1000}; do echo $i; sleep 1; done`,
Status: common.StatusRunning,
CreatedAt: time.Unix(1705641678, 0),
UpdatedAt: time.Unix(1705641678, 0),
Dir: "/var/eclipse/jobfiles/3",
}
jsBytes, err := json.Marshal(job)
require.NoError(t, err)
assert.Equal(t,
`{`+strings.Join([]string{
`"ID":3`,
`"TriggerExpressions":["{\"repo_url\":\"https://git.lukeshu.com/btrfs-progs-ng\",\"rev\":\"HEAD\"}"]`,
`"TriggerValues":["76f668122f9feeb20e885be330990a750107d5dd"]`,
`"Command":"set -x; for i in {1..1000}; do echo $i; sleep 1; done"`,
`"Status":"running"`,
`"CreatedAt":"2024-01-18T22:21:18-07:00"`,
`"UpdatedAt":"2024-01-18T22:21:18-07:00"`,
}, `,`)+`}`,
string(jsBytes))
var job2 common.Job
require.NoError(t, json.Unmarshal(jsBytes, &job2))
assert.Equal(t,
common.Job{
ID: 3,
TriggerExpressions: []string{`{"repo_url":"https://git.lukeshu.com/btrfs-progs-ng","rev":"HEAD"}`},
TriggerValues: []string{"76f668122f9feeb20e885be330990a750107d5dd"},
Command: `set -x; for i in {1..1000}; do echo $i; sleep 1; done`,
Status: common.StatusRunning,
CreatedAt: time.Unix(1705641678, 0),
UpdatedAt: time.Unix(1705641678, 0),
Dir: "",
},
job2)
}
func TestUnmarshalStatus(t *testing.T) {
status := common.JobStatus(-1)
assert.Error(t, status.UnmarshalText([]byte("invalid")))
// Check that an invalid unmarshal doesn't change the value.
assert.Equal(t, common.JobStatus(-1), status)
}