1
0

fix: 启动参数 duration 候选值对齐后端标准格式

前端 Select 使用 Go time.Duration.String() 标准字符串作为 value,
与后端查询/保存响应保持一致,解决保存后反显不匹配的问题。
This commit is contained in:
2026-05-08 14:18:09 +08:00
parent 6b00045f4e
commit c524e8f928
9 changed files with 197 additions and 16 deletions

View File

@@ -66,6 +66,63 @@ func TestDurationConversion(t *testing.T) {
assert.Equal(t, cfg.Database.ConnMaxLifetime, parsed)
}
func TestSaveConfigToPath_DurationFormat(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.yaml")
cfg := DefaultConfig()
cfg.Server.ReadTimeout = 30 * time.Second
cfg.Server.WriteTimeout = 1 * time.Minute
cfg.Database.ConnMaxLifetime = 1 * time.Hour
err := SaveConfigToPath(cfg, configPath)
require.NoError(t, err)
data, err := os.ReadFile(configPath)
require.NoError(t, err)
content := string(data)
assert.Contains(t, content, "conn_max_lifetime: 1h0m0s")
assert.Contains(t, content, "read_timeout: 30s")
assert.Contains(t, content, "write_timeout: 1m0s")
}
func TestSaveAndReload_DurationRoundTrip(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.yaml")
yamlContent := `
server:
port: 9826
read_timeout: 30s
write_timeout: 1m
database:
driver: sqlite
path: ` + filepath.Join(dir, "test.db") + `
max_idle_conns: 10
max_open_conns: 100
conn_max_lifetime: 30m
log:
level: info
path: ` + filepath.Join(dir, "log") + `
max_size: 100
max_backups: 10
max_age: 30
compress: true
`
require.NoError(t, os.WriteFile(configPath, []byte(yamlContent), 0o600))
cfg, err := LoadDesktopConfigAtPath(configPath)
require.NoError(t, err)
assert.Equal(t, 30*time.Minute, cfg.Database.ConnMaxLifetime)
err = SaveConfigToPath(cfg, configPath)
require.NoError(t, err)
data, err := os.ReadFile(configPath)
require.NoError(t, err)
assert.Contains(t, string(data), "conn_max_lifetime: 30m0s")
}
func configToDTO(c *Config) struct {
Server struct {
Port int `json:"port"`

View File

@@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@@ -416,3 +417,94 @@ func TestSettingsHandler_SaveStartupSettings_InvalidJSON(t *testing.T) {
assert.Equal(t, 400, w.Code)
}
func TestSettingsHandler_GetStartupSettings_DurationNormalization(t *testing.T) {
cfg, configPath := createTestConfig(t)
yamlContent := `
server:
port: 9826
read_timeout: 30s
write_timeout: 1m
database:
driver: sqlite
path: ` + cfg.Database.Path + `
max_idle_conns: 10
max_open_conns: 100
conn_max_lifetime: 30m
log:
level: info
path: ` + cfg.Log.Path + `
max_size: 100
max_backups: 10
max_age: 30
compress: true
`
require.NoError(t, os.WriteFile(configPath, []byte(yamlContent), 0o600))
h := NewSettingsHandler(cfg, "desktop", true, configPath)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/api/settings/startup", nil)
h.GetStartupSettings(c)
assert.Equal(t, 200, w.Code)
var resp startupSettingsResponse
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(t, "1m0s", resp.Config.Server.WriteTimeout)
assert.Equal(t, "30m0s", resp.Config.Database.ConnMaxLifetime)
}
func TestSettingsHandler_SaveStartupSettings_StandardDurationRoundTrip(t *testing.T) {
cfg, configPath := createTestConfig(t)
h := NewSettingsHandler(cfg, "desktop", true, configPath)
tmpDir := t.TempDir()
body, _ := json.Marshal(map[string]interface{}{
"config": map[string]interface{}{
"server": map[string]interface{}{
"port": 9826,
"read_timeout": "30s",
"write_timeout": "1m0s",
},
"database": map[string]interface{}{
"driver": "sqlite",
"path": filepath.Join(tmpDir, "test.db"),
"port": 3306,
"dbname": "nex",
"max_idle_conns": 10,
"max_open_conns": 100,
"conn_max_lifetime": "1h0m0s",
},
"log": map[string]interface{}{
"level": "info",
"path": filepath.Join(tmpDir, "log"),
"max_size": 100,
"max_backups": 10,
"max_age": 30,
"compress": true,
},
},
})
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("PUT", "/api/settings/startup", bytes.NewReader(body))
c.Request.Header.Set("Content-Type", "application/json")
h.SaveStartupSettings(c)
assert.Equal(t, 200, w.Code)
var resp startupSettingsResponse
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(t, "1m0s", resp.Config.Server.WriteTimeout)
assert.Equal(t, "1h0m0s", resp.Config.Database.ConnMaxLifetime)
savedCfg, err := config.LoadDesktopConfigAtPath(configPath)
require.NoError(t, err)
assert.Equal(t, 1*time.Hour, savedCfg.Database.ConnMaxLifetime)
}