Files
iotlcm/internal/service/runtime_log_test.go
T
MiFaZhan 4e2cd43a55 chore: 初始开源发布
iotlcm-onelink-mock:OneLink 挡板服务(本地测试替身)。

- Go module 迁移至 git.5iot.com/iotlcm-public/iotlcm-onelink-mock
- 修复同毫秒内多次生成卡导致 ICCID/IMSI 唯一约束冲突
- 补 Apache-2.0 LICENSE,README 重写并新增 docs/deploy.md
- 移除内部部署资产(CI 触发器 / 镜像推送脚本 / 指定环境部署文档)
- 测试凭据改为 demo_appid / demo_password,真实手机号与内网 IP 脱敏
- 统一 LF 行尾并加 .gitattributes
2026-10-22 15:08:27 +08:00

57 lines
1.6 KiB
Go

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)
}
}