chore: 并入 iotlcm-onelink-mock 历史到 onelink-mock/(原提交 f398595)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRuntimeLogStoreRetainsLatestLines(t *testing.T) {
|
||||
now := func() time.Time { return time.Date(2026, 8, 13, 12, 30, 0, 0, time.Local) }
|
||||
store := newRuntimeLogStore(2, now)
|
||||
if _, err := store.Write([]byte("first\nsecond")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := store.Write([]byte(" line\nERROR third\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result := store.Latest(10)
|
||||
if result.Total != 2 || len(result.Items) != 2 {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
if result.Items[0].Message != "ERROR third" || result.Items[0].Level != "ERROR" {
|
||||
t.Fatalf("newest = %#v", result.Items[0])
|
||||
}
|
||||
if result.Items[1].Message != "second line" {
|
||||
t.Fatalf("second = %#v", result.Items[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeLogsEndpointIsNotRecordedAsOperation(t *testing.T) {
|
||||
runtimeLogs := NewRuntimeLogStore()
|
||||
service, err := NewService("sqlite::@file(:memory:)", runtimeLogs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = runtimeLogs.Write([]byte("service ready\n"))
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/runtime-logs?limit=10", nil)
|
||||
res := httptest.NewRecorder()
|
||||
service.Router().ServeHTTP(res, req)
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body = %s", res.Code, res.Body.String())
|
||||
}
|
||||
var result RuntimeLogResult
|
||||
if err := json.Unmarshal(res.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Items) != 1 || result.Items[0].Message != "service ready" {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
if page := service.operationLogs.Query("", "", "", 1, 20); page.Total != 0 {
|
||||
t.Fatalf("runtime log polling was recorded: %#v", page)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user