Files
KlipperAnywhere/cmd/server/printer_settings_test.go
T
2026-05-14 02:27:26 +01:00

220 lines
8.0 KiB
Go

package main
import (
"path/filepath"
"strings"
"testing"
"github.com/klipperanywhere/klipperanywhere/internal/protocol"
)
func TestPrinterSettingsStoreSeedAndUpdate(t *testing.T) {
store, err := newPrinterSettingsStore(filepath.Join(t.TempDir(), "printer-settings.json"))
if err != nil {
t.Fatal(err)
}
health := &protocol.Health{
UIURL: "http://192.168.1.60",
UIAutoDetect: true,
ControlMode: "readonly",
}
record, created, err := store.ensureFromHealth("real-printer", health, "full")
if err != nil {
t.Fatal(err)
}
if !created {
t.Fatal("expected first seed to create a record")
}
if record.ControlMode != "full" || record.UIURL != "http://192.168.1.60" || !record.UIAutoDetect {
t.Fatalf("unexpected seed record: %+v", record)
}
updated, err := store.update("real-printer", ptrString("Living room printer"), ptrString("full"), ptrString("http://printer-ui.local"), ptrBool(false), ptrString("nozzle"), "readonly")
if err != nil {
t.Fatal(err)
}
if updated.DisplayName != "Living room printer" || updated.ControlMode != "full" || updated.UIURL != "http://printer-ui.local" || updated.UIAutoDetect || updated.NotificationSnapshotCamera != "nozzle" {
t.Fatalf("unexpected updated record: %+v", updated)
}
snapshot, ok := store.snapshot("real-printer")
if !ok {
t.Fatal("expected snapshot to exist")
}
if snapshot.DisplayName != "Living room printer" || snapshot.ControlMode != "full" || snapshot.UIURL != "http://printer-ui.local" || snapshot.UIAutoDetect || snapshot.NotificationSnapshotCamera != "nozzle" {
t.Fatalf("unexpected snapshot: %+v", snapshot)
}
}
func TestPrinterSettingsUpdateDisplayNameOnly(t *testing.T) {
store, err := newPrinterSettingsStore(filepath.Join(t.TempDir(), "printer-settings.json"))
if err != nil {
t.Fatal(err)
}
updated, err := store.update("real-printer", ptrString("Workshop"), nil, nil, nil, nil, "readonly")
if err != nil {
t.Fatal(err)
}
if updated.DisplayName != "Workshop" {
t.Fatalf("unexpected display name: %+v", updated)
}
if updated.ControlMode != "readonly" {
t.Fatalf("unexpected control mode: %+v", updated)
}
}
func TestPrinterSettingsUpdateInstallDetails(t *testing.T) {
store, err := newPrinterSettingsStore(filepath.Join(t.TempDir(), "printer-settings.json"))
if err != nil {
t.Fatal(err)
}
updated, err := store.updateInstall("real-printer", printerInstallRecord{
Host: "192.168.1.60",
Port: 22,
Username: "root",
HostType: "rinkhals",
ServerURL: "http://192.168.1.35:8080",
MoonrakerURL: "http://127.0.0.1:7125",
UIURL: "http://192.168.1.60",
UIAutoDetect: true,
RequestedBy: "admin",
RemoteHost: "F005-02B0",
ReleaseURL: "https://example.com/releases/latest/download",
BinarySource: "release",
ConfigPath: "/tmp/agent.json",
BinaryPath: "/tmp/klipperanywhere-agent",
Message: "installed",
PostInstallConnected: true,
PostInstallMessage: "confirmed",
RollbackPerformed: false,
RollbackMessage: "",
Status: "installed",
})
if err != nil {
t.Fatal(err)
}
if updated.Install == nil {
t.Fatal("expected install record")
}
if updated.Install.Host != "192.168.1.60" || updated.Install.Username != "root" || updated.Install.HostType != "rinkhals" || updated.Install.Status != "installed" {
t.Fatalf("unexpected install record: %+v", updated.Install)
}
if updated.Install.PrinterID != "real-printer" {
t.Fatalf("printer id = %q, want real-printer", updated.Install.PrinterID)
}
if len(updated.InstallHistory) != 1 {
t.Fatalf("expected 1 history entry, got %d", len(updated.InstallHistory))
}
if updated.InstallHistory[0].PrinterID != "real-printer" || updated.InstallHistory[0].Status != "installed" {
t.Fatalf("unexpected history entry: %+v", updated.InstallHistory[0])
}
snapshot, ok := store.snapshot("real-printer")
if !ok || snapshot.Install == nil {
t.Fatal("expected stored install snapshot")
}
if snapshot.Install.RequestedBy != "admin" || !snapshot.Install.PostInstallConnected {
t.Fatalf("unexpected snapshot install record: %+v", snapshot.Install)
}
}
func TestPrinterSettingsUpdateNotifications(t *testing.T) {
store, err := newPrinterSettingsStore(filepath.Join(t.TempDir(), "printer-settings.json"))
if err != nil {
t.Fatal(err)
}
updated, err := store.updateNotifications("real-printer", ptrBool(true), ptrBool(true), ptrBool(false), ptrBool(true), ptrBool(true), ptrBool(false), ptrBool(true), ptrBool(true), ptrBool(false))
if err != nil {
t.Fatal(err)
}
if !updated.PrintNotificationsEnabled || !updated.NotifyPrintStarted || updated.NotifyPrintPaused || !updated.NotifyPrintResumed || !updated.NotifyPrintCompleted || updated.NotifyPrintCanceled || !updated.NotifyPrintFailed || !updated.NotifyFirstLayer || updated.NotifyThirdLayer {
t.Fatalf("unexpected notification record: %+v", updated)
}
updated, err = store.updatePrintObservation("real-printer", &protocol.PrintStatus{State: "printing", Filename: "benchy.gcode"}, "telegram", true, "")
if err != nil {
t.Fatal(err)
}
if updated.LastObservedPrintState != "printing" || updated.LastNotifiedPrintState != "printing" {
t.Fatalf("unexpected observation record: %+v", updated)
}
}
func TestPrinterSettingsUpdatePrintLayerObservation(t *testing.T) {
store, err := newPrinterSettingsStore(filepath.Join(t.TempDir(), "printer-settings.json"))
if err != nil {
t.Fatal(err)
}
updated, err := store.updatePrintLayerObservation("real-printer", &protocol.PrintStatus{State: "printing", Filename: "benchy.gcode", CurrentLayer: 1}, "telegram", true, "")
if err != nil {
t.Fatal(err)
}
if updated.LastObservedPrintLayer != 1 || updated.LastLayerNotifiedPrintLayer != 1 {
t.Fatalf("unexpected layer observation record: %+v", updated)
}
}
func TestPrinterSettingsInstallHistoryAppends(t *testing.T) {
store, err := newPrinterSettingsStore(filepath.Join(t.TempDir(), "printer-settings.json"))
if err != nil {
t.Fatal(err)
}
if _, err := store.updateInstall("real-printer", printerInstallRecord{Status: "failed", Error: "first"}); err != nil {
t.Fatal(err)
}
if _, err := store.updateInstall("real-printer", printerInstallRecord{Status: "installed", Message: "second"}); err != nil {
t.Fatal(err)
}
snapshot, ok := store.snapshot("real-printer")
if !ok {
t.Fatal("expected snapshot")
}
if len(snapshot.InstallHistory) != 2 {
t.Fatalf("expected 2 history entries, got %d", len(snapshot.InstallHistory))
}
if snapshot.InstallHistory[0].Status != "installed" || snapshot.InstallHistory[1].Status != "failed" {
t.Fatalf("unexpected history order: %+v", snapshot.InstallHistory)
}
}
func TestNormalizeControlMode(t *testing.T) {
if got := normalizeControlMode("readonly"); got != "readonly" {
t.Fatalf("normalizeControlMode(readonly) = %q", got)
}
if got := normalizeControlMode("full"); got != "full" {
t.Fatalf("normalizeControlMode(full) = %q", got)
}
if got := normalizeControlMode("invalid"); got != "" {
t.Fatalf("normalizeControlMode(invalid) = %q", got)
}
}
func TestDescribePrinterSettingsChange(t *testing.T) {
before := printerSettingsRecord{
DisplayName: "Workshop",
ControlMode: "readonly",
UIURL: "http://192.168.1.60",
UIAutoDetect: true,
NotificationSnapshotCamera: "wide",
}
after := printerSettingsRecord{
DisplayName: "Living room",
ControlMode: "full",
UIURL: "http://printer.local",
UIAutoDetect: false,
NotificationSnapshotCamera: "nozzle",
}
got := describePrinterSettingsChange(before, after)
if got == "" {
t.Fatal("expected non-empty description")
}
for _, want := range []string{"display_name", "control_mode", "ui_url", "ui_auto_detect", "notification_snapshot_camera"} {
if !strings.Contains(got, want) {
t.Fatalf("description %q missing %q", got, want)
}
}
}
func ptrString(v string) *string { return &v }
func ptrBool(v bool) *bool { return &v }