Compare commits
21 Commits
big_refact
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7818a3fb44 | |||
| 295c8f1589 | |||
| 739497a853 | |||
| 8da79514b8 | |||
| 872c7b356f | |||
| 6fab12e7bd | |||
| cbdc1295f2 | |||
| a7ae5d465c | |||
| 8df89d3478 | |||
| f97841b079 | |||
| 17d2f4d1f5 | |||
| 1aa6c7dac4 | |||
| da5bf43197 | |||
| 0d803f4b23 | |||
| fc4e0217b2 | |||
| d4db20be16 | |||
| 1041016210 | |||
| 99ed529600 | |||
| f02bb1e6fb | |||
| ccb70e3165 | |||
| 0a76c5feca |
22
.github/workflows/nightly.yml
vendored
Normal file
22
.github/workflows/nightly.yml
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
name: Run nightly tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: '0 20 * * *' # Every day at 20:00 UTC
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
jobs:
|
||||||
|
nightly-tests:
|
||||||
|
runs-on: [ubuntu-latest, cloud]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v4
|
||||||
|
with:
|
||||||
|
go-version: '1.23'
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
working-directory: ./src
|
||||||
|
run: go test -v --short ./...
|
||||||
7
.github/workflows/short-tests.yml
vendored
7
.github/workflows/short-tests.yml
vendored
@@ -13,8 +13,9 @@ jobs:
|
|||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: '1.23'
|
go-version: '1.24'
|
||||||
|
|
||||||
- name: Test
|
- name: Run short tests with coverage
|
||||||
working-directory: ./src
|
working-directory: ./src
|
||||||
run: go test -v --short ./...
|
run: | # TODO: at this moment only Home Assistant component is tested
|
||||||
|
go test -v --short ./components/homeassistant/... -cover -coverprofile=cover.out
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
Port 8881
|
# Home Automation Backend
|
||||||
|
|
||||||
|

|
||||||
@@ -27,7 +27,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
port string
|
port string
|
||||||
scheduler gocron.Scheduler
|
scheduler gocron.Scheduler
|
||||||
ticktick *ticktickutil.TicktickUtilImpl
|
ticktick ticktickutil.TicktickUtil
|
||||||
|
ha *homeassistant.HomeAssistant
|
||||||
)
|
)
|
||||||
|
|
||||||
// serveCmd represents the serve command
|
// serveCmd represents the serve command
|
||||||
@@ -55,7 +56,7 @@ func initComponent() {
|
|||||||
// init location recorder
|
// init location recorder
|
||||||
locationRecorder.Init()
|
locationRecorder.Init()
|
||||||
// init homeassistant
|
// init homeassistant
|
||||||
homeassistant.Init(ticktick)
|
ha = homeassistant.NewHomeAssistant(ticktick)
|
||||||
}
|
}
|
||||||
|
|
||||||
func serve(cmd *cobra.Command, args []string) {
|
func serve(cmd *cobra.Command, args []string) {
|
||||||
@@ -108,7 +109,7 @@ func serve(cmd *cobra.Command, args []string) {
|
|||||||
|
|
||||||
router.HandleFunc("/poo/latest", pooRecorder.HandleNotifyLatestPoo).Methods("GET")
|
router.HandleFunc("/poo/latest", pooRecorder.HandleNotifyLatestPoo).Methods("GET")
|
||||||
router.HandleFunc("/poo/record", pooRecorder.HandleRecordPoo).Methods("POST")
|
router.HandleFunc("/poo/record", pooRecorder.HandleRecordPoo).Methods("POST")
|
||||||
router.HandleFunc("/homeassistant/publish", homeassistant.HandleHaMessage).Methods("POST")
|
router.HandleFunc("/homeassistant/publish", ha.HandleHaMessage).Methods("POST")
|
||||||
|
|
||||||
router.HandleFunc("/location/record", locationRecorder.HandleRecordLocation).Methods("POST")
|
router.HandleFunc("/location/record", locationRecorder.HandleRecordLocation).Methods("POST")
|
||||||
|
|
||||||
|
|||||||
@@ -18,63 +18,68 @@ type haMessage struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HomeAssistant struct {
|
||||||
|
ticktickUtil ticktickutil.TicktickUtil
|
||||||
|
}
|
||||||
|
|
||||||
type actionTask struct {
|
type actionTask struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
DueHour int `json:"due_hour"`
|
DueHour int `json:"due_hour"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
func NewHomeAssistant(ticktick ticktickutil.TicktickUtil) *HomeAssistant {
|
||||||
ticktickUtil ticktickutil.TicktickUtil
|
return &HomeAssistant{
|
||||||
)
|
ticktickUtil: ticktick,
|
||||||
|
}
|
||||||
func Init(ticktick ticktickutil.TicktickUtil) {
|
|
||||||
ticktickUtil = ticktick
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
func (ha *HomeAssistant) HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||||
var message haMessage
|
var message haMessage
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
decoder.DisallowUnknownFields()
|
decoder.DisallowUnknownFields()
|
||||||
err := decoder.Decode(&message)
|
err := decoder.Decode(&message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn(fmt.Sprintln("HandleHaMessage: Error decoding request body", err))
|
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error decoding request body", err))
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, "", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch message.Target {
|
switch message.Target {
|
||||||
case "poo_recorder":
|
case "poo_recorder":
|
||||||
res := handlePooRecorderMsg(message)
|
res := ha.handlePooRecorderMsg(message)
|
||||||
if !res {
|
if !res {
|
||||||
http.Error(w, "Error handling poo recorder message", http.StatusInternalServerError)
|
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling poo recorder message"))
|
||||||
|
http.Error(w, "", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
case "location_recorder":
|
case "location_recorder":
|
||||||
res := handleLocationRecorderMsg(message)
|
res := ha.handleLocationRecorderMsg(message)
|
||||||
if !res {
|
if !res {
|
||||||
http.Error(w, "Error handling location recorder message", http.StatusInternalServerError)
|
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling location recorder message"))
|
||||||
|
http.Error(w, "", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
case "ticktick":
|
case "ticktick":
|
||||||
res := handleTicktickMsg(message)
|
res := ha.handleTicktickMsg(message)
|
||||||
if !res {
|
if !res {
|
||||||
http.Error(w, "Error handling ticktick message", http.StatusInternalServerError)
|
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling ticktick message"))
|
||||||
|
http.Error(w, "", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
slog.Warn(fmt.Sprintln("HandleHaMessage: Unknown target", message.Target))
|
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Unknown target", message.Target))
|
||||||
http.Error(w, "Unknown target", http.StatusBadRequest)
|
http.Error(w, "", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePooRecorderMsg(message haMessage) bool {
|
func (ha *HomeAssistant) handlePooRecorderMsg(message haMessage) bool {
|
||||||
switch message.Action {
|
switch message.Action {
|
||||||
case "get_latest":
|
case "get_latest":
|
||||||
return handleGetLatestPoo()
|
return ha.handleGetLatestPoo()
|
||||||
default:
|
default:
|
||||||
slog.Warn(fmt.Sprintln("handlePooRecorderMsg: Unknown action", message.Action))
|
slog.Warn(fmt.Sprintln("homeassistant.handlePooRecorderMsg: Unknown action", message.Action))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLocationRecorderMsg(message haMessage) bool {
|
func (ha *HomeAssistant) handleLocationRecorderMsg(message haMessage) bool {
|
||||||
if message.Action == "record" {
|
if message.Action == "record" {
|
||||||
port := viper.GetString("port")
|
port := viper.GetString("port")
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
@@ -82,43 +87,43 @@ func handleLocationRecorderMsg(message haMessage) bool {
|
|||||||
}
|
}
|
||||||
_, err := client.Post("http://localhost:"+port+"/location/record", "application/json", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
|
_, err := client.Post("http://localhost:"+port+"/location/record", "application/json", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Error sending request to location recorder", err))
|
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
} else {
|
} else {
|
||||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Unknown action", message.Action))
|
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Unknown action", message.Action))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleTicktickMsg(message haMessage) bool {
|
func (ha *HomeAssistant) handleTicktickMsg(message haMessage) bool {
|
||||||
switch message.Action {
|
switch message.Action {
|
||||||
case "create_action_task":
|
case "create_action_task":
|
||||||
return createActionTask(message)
|
return ha.createActionTask(message)
|
||||||
default:
|
default:
|
||||||
slog.Warn(fmt.Sprintln("handleTicktickMsg: Unknown action", message.Action))
|
slog.Warn(fmt.Sprintln("homeassistant.handleTicktickMsg: Unknown action", message.Action))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetLatestPoo() bool {
|
func (ha *HomeAssistant) handleGetLatestPoo() bool {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Second * 1,
|
Timeout: time.Second * 1,
|
||||||
}
|
}
|
||||||
port := viper.GetString("port")
|
port := viper.GetString("port")
|
||||||
_, err := client.Get("http://localhost:" + port + "/poo/latest")
|
_, err := client.Get("http://localhost:" + port + "/poo/latest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn(fmt.Sprintln("handleGetLatestPoo: Error sending request to poo recorder", err))
|
slog.Warn(fmt.Sprintln("homeassistant.handleGetLatestPoo: Error sending request to poo recorder", err))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func createActionTask(message haMessage) bool {
|
func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||||
if !viper.IsSet("homeassistant.actionTaskProjectId") {
|
if !viper.IsSet("homeassistant.actionTaskProjectId") {
|
||||||
slog.Warn("Homeassistant.createActionTask actionTaskProjectId not set")
|
slog.Warn("homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
projectId := viper.GetString("homeassistant.actionTaskProjectId")
|
projectId := viper.GetString("homeassistant.actionTaskProjectId")
|
||||||
@@ -126,7 +131,7 @@ func createActionTask(message haMessage) bool {
|
|||||||
var task actionTask
|
var task actionTask
|
||||||
err := json.Unmarshal([]byte(detail), &task)
|
err := json.Unmarshal([]byte(detail), &task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn(fmt.Sprintln("Homeassistant.createActionTask: Error unmarshalling", err))
|
slog.Warn(fmt.Sprintln("homeassistant.createActionTask: Error unmarshalling", err))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
dueHour := task.DueHour
|
dueHour := task.DueHour
|
||||||
@@ -138,9 +143,9 @@ func createActionTask(message haMessage) bool {
|
|||||||
Title: task.Action,
|
Title: task.Action,
|
||||||
DueDate: dueTicktick,
|
DueDate: dueTicktick,
|
||||||
}
|
}
|
||||||
err = ticktickUtil.CreateTask(ticktickTask)
|
err = ha.ticktickUtil.CreateTask(ticktickTask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn(fmt.Sprintf("Homeassistant.createActionTask: Error creating task %s", err))
|
slog.Warn(fmt.Sprintf("homeassistant.createActionTask: Error creating task %s", err))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -20,19 +20,38 @@ var (
|
|||||||
loggerText = new(bytes.Buffer)
|
loggerText = new(bytes.Buffer)
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type MockTicktickUtil struct {
|
||||||
MockedTicktickUtil struct {
|
mock.Mock
|
||||||
mock.Mock
|
}
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func SetupTearDown(t *testing.T) func() {
|
func (m *MockTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||||
|
m.Called(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
||||||
|
args := m.Called(projectId)
|
||||||
|
return args.Get(0).([]ticktickutil.Task)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockTicktickUtil) HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||||
|
args := m.Called(projectId, taskTitile)
|
||||||
|
return args.Bool(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
||||||
|
args := m.Called(task)
|
||||||
|
return args.Error(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetupTearDown(t *testing.T) (func(), *HomeAssistant) {
|
||||||
loggertearDown := loggerSetupTeardown()
|
loggertearDown := loggerSetupTeardown()
|
||||||
|
mockTicktick := &MockTicktickUtil{}
|
||||||
|
ha := NewHomeAssistant(mockTicktick)
|
||||||
|
|
||||||
return func() {
|
return func() {
|
||||||
loggertearDown()
|
loggertearDown()
|
||||||
viper.Reset()
|
viper.Reset()
|
||||||
}
|
}, ha
|
||||||
}
|
}
|
||||||
|
|
||||||
func loggerSetupTeardown() func() {
|
func loggerSetupTeardown() func() {
|
||||||
@@ -46,40 +65,21 @@ func loggerSetupTeardown() func() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockedTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
|
||||||
m.Called(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockedTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
|
||||||
args := m.Called(projectId)
|
|
||||||
return args.Get(0).([]ticktickutil.Task)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockedTicktickUtil) HasDuplicateTask(projectId string, taskTitile string) bool {
|
|
||||||
args := m.Called(projectId, taskTitile)
|
|
||||||
return args.Bool(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockedTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
|
||||||
args := m.Called(task)
|
|
||||||
return args.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleHaMessageJsonDecodeError(t *testing.T) {
|
func TestHandleHaMessageJsonDecodeError(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
invalidRequestBody := ` { "target": "poo_recorder", "action": "get_latest", "content": " }`
|
invalidRequestBody := ` { "target": "poo_recorder", "action": "get_latest", "content": " }`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "HandleHaMessage: Error decoding request body")
|
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Error decoding request body")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlePooRecorderMsgGetLatest(t *testing.T) {
|
func TestHandlePooRecorderMsgGetLatest(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
@@ -92,26 +92,26 @@ func TestHandlePooRecorderMsgGetLatest(t *testing.T) {
|
|||||||
port := strings.Split(server.URL, ":")[2]
|
port := strings.Split(server.URL, ":")[2]
|
||||||
viper.Set("port", port)
|
viper.Set("port", port)
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
assert.Empty(t, loggerText.String())
|
assert.Empty(t, loggerText.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlePooRecorderMsgUnknownAction(t *testing.T) {
|
func TestHandlePooRecorderMsgUnknownAction(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "poo_recorder", "action": "unknown_action", "content": ""}`
|
requestBody := `{"target": "poo_recorder", "action": "unknown_action", "content": ""}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "handlePooRecorderMsg: Unknown action")
|
assert.Contains(t, loggerText.String(), "homeassistant.handlePooRecorderMsg: Unknown action")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlePooRecorderMsgGetLatestError(t *testing.T) {
|
func TestHandlePooRecorderMsgGetLatestError(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||||
@@ -121,13 +121,13 @@ func TestHandlePooRecorderMsgGetLatestError(t *testing.T) {
|
|||||||
port := "invalid port"
|
port := "invalid port"
|
||||||
viper.Set("port", port)
|
viper.Set("port", port)
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "handleGetLatestPoo: Error sending request to poo recorder")
|
assert.Contains(t, loggerText.String(), "homeassistant.handleGetLatestPoo: Error sending request to poo recorder")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleLocationRecorderMsg(t *testing.T) {
|
func TestHandleLocationRecorderMsg(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
||||||
@@ -143,26 +143,26 @@ func TestHandleLocationRecorderMsg(t *testing.T) {
|
|||||||
port := strings.Split(server.URL, ":")[2]
|
port := strings.Split(server.URL, ":")[2]
|
||||||
viper.Set("port", port)
|
viper.Set("port", port)
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
assert.Empty(t, loggerText.String())
|
assert.Empty(t, loggerText.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleLocationRecorderMsgUnknownAction(t *testing.T) {
|
func TestHandleLocationRecorderMsgUnknownAction(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "location_recorder", "action": "unknown_action", "content": ""}`
|
requestBody := `{"target": "location_recorder", "action": "unknown_action", "content": ""}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "handleLocationRecorderMsg: Unknown action")
|
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Unknown action")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
||||||
@@ -172,13 +172,13 @@ func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
|||||||
port := "invalid port"
|
port := "invalid port"
|
||||||
viper.Set("port", port)
|
viper.Set("port", port)
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "handleLocationRecorderMsg: Error sending request to location recorder")
|
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Error sending request to location recorder")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, _ := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
const expectedProjectId = "test_project_id"
|
const expectedProjectId = "test_project_id"
|
||||||
const dueHour = 12
|
const dueHour = 12
|
||||||
@@ -189,96 +189,92 @@ func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
|||||||
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
mockedTicktickUtil := new(MockedTicktickUtil)
|
|
||||||
|
mockTicktick := &MockTicktickUtil{}
|
||||||
|
mockTicktick.On("CreateTask", mock.Anything).Return(nil)
|
||||||
|
ha := NewHomeAssistant(mockTicktick)
|
||||||
viper.Set("homeassistant.actionTaskProjectId", expectedProjectId)
|
viper.Set("homeassistant.actionTaskProjectId", expectedProjectId)
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(nil)
|
|
||||||
|
|
||||||
Init(mockedTicktickUtil)
|
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
|
||||||
|
|
||||||
expectedTask := ticktickutil.Task{
|
expectedTask := ticktickutil.Task{
|
||||||
Title: "test_action",
|
Title: "test_action",
|
||||||
ProjectId: expectedProjectId,
|
|
||||||
DueDate: dueTicktick,
|
DueDate: dueTicktick,
|
||||||
|
ProjectId: expectedProjectId,
|
||||||
}
|
}
|
||||||
|
mockTicktick.AssertCalled(t, "CreateTask", expectedTask)
|
||||||
mockedTicktickUtil.AssertCalled(t, "CreateTask", expectedTask)
|
mockTicktick.AssertNumberOfCalls(t, "CreateTask", 1)
|
||||||
mockedTicktickUtil.AssertNumberOfCalls(t, "CreateTask", 1)
|
|
||||||
assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
assert.Empty(t, loggerText.String())
|
assert.Empty(t, loggerText.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleTicktickMsgUnknownAction(t *testing.T) {
|
func TestHandleTicktickMsgUnknownAction(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "ticktick", "action": "unknown_action", "content": ""}`
|
requestBody := `{"target": "ticktick", "action": "unknown_action", "content": ""}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "handleTicktickMsg: Unknown action")
|
assert.Contains(t, loggerText.String(), "homeassistant.handleTicktickMsg: Unknown action")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleTicktickMsgProjectIdUnset(t *testing.T) {
|
func TestHandleTicktickMsgProjectIdUnset(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask actionTaskProjectId not set")
|
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleTicktickMsgJsonError(t *testing.T) {
|
func TestHandleTicktickMsgJsonError(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
invalidRequestBody := ` { "target": "ticktick", "action": "create_action_task", "content": "{'title': 'tes, 'action': 'test_action', 'due_hour': 12}"}`
|
invalidRequestBody := ` { "target": "ticktick", "action": "create_action_task", "content": "{'title': 'tes, 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask: Error unmarshalling")
|
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error unmarshalling")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleTicktickMsgTicktickUtilErr(t *testing.T) {
|
func TestHandleTicktickMsgTicktickUtilErr(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, _ := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
mockedTicktickUtil := new(MockedTicktickUtil)
|
mockedTicktickUtil := &MockTicktickUtil{}
|
||||||
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||||
|
|
||||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(errors.New("some error"))
|
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(errors.New("some error"))
|
||||||
|
|
||||||
Init(mockedTicktickUtil)
|
ha := NewHomeAssistant(mockedTicktickUtil)
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
|
|
||||||
mockedTicktickUtil.AssertCalled(t, "CreateTask", mock.Anything)
|
mockedTicktickUtil.AssertCalled(t, "CreateTask", mock.Anything)
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask: Error creating task")
|
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error creating task")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleHaMessageUnknownTarget(t *testing.T) {
|
func TestHandleHaMessageUnknownTarget(t *testing.T) {
|
||||||
teardown := SetupTearDown(t)
|
teardown, ha := SetupTearDown(t)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
requestBody := `{"target": "unknown_target", "action": "record", "content": ""}`
|
requestBody := `{"target": "unknown_target", "action": "record", "content": ""}`
|
||||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
HandleHaMessage(w, req)
|
ha.HandleHaMessage(w, req)
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "HandleHaMessage: Unknown target")
|
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Unknown target")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ require (
|
|||||||
github.com/jomei/notionapi v1.13.2
|
github.com/jomei/notionapi v1.13.2
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.10.0
|
||||||
golang.org/x/term v0.24.0
|
golang.org/x/term v0.24.0
|
||||||
modernc.org/sqlite v1.33.1
|
modernc.org/sqlite v1.33.1
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -78,8 +78,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
|||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() *TicktickUtilImpl { // TODO: Will modify Init to a proper behavior
|
func Init() TicktickUtil { // TODO: Will modify Init to a proper behavior
|
||||||
ticktickUtilImpl := &TicktickUtilImpl{}
|
ticktickUtilImpl := &TicktickUtilImpl{}
|
||||||
if !viper.InConfig("ticktick.clientId") {
|
if !viper.InConfig("ticktick.clientId") {
|
||||||
slog.Error("TickTick clientId not found in config file, exiting..")
|
slog.Error("TickTick clientId not found in config file, exiting..")
|
||||||
|
|||||||
Reference in New Issue
Block a user