Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Improve test coverage

rcourtman67bde72c934f
diff --git a/cmd/hashpw/main.go b/cmd/hashpw/main.go
index 45557dec6e..660566f589 100644
--- a/cmd/hashpw/main.go
+++ b/cmd/hashpw/main.go
@@ -2,23 +2,29 @@ package main
import (
"fmt"
+ "io"
"os"
"github.com/rcourtman/pulse-go-rewrite/internal/auth"
)
-func main() {
- if len(os.Args) < 2 {
- fmt.Println("Usage: hashpw <password>")
- os.Exit(1)
+func run(args []string, out io.Writer) int {
+ if len(args) < 2 {
+ fmt.Fprintln(out, "Usage: hashpw <password>")
+ return 1
}
- password := os.Args[1]
+ password := args[1]
hash, err := auth.HashPassword(password)
if err != nil {
- fmt.Printf("Error: %v\n", err)
- os.Exit(1)
+ fmt.Fprintf(out, "Error: %v\n", err)
+ return 1
}
- fmt.Println(hash)
+ fmt.Fprintln(out, hash)
+ return 0
+}
+
+func main() {
+ os.Exit(run(os.Args, os.Stdout))
}
diff --git a/cmd/hashpw/main_test.go b/cmd/hashpw/main_test.go
new file mode 100644
index 0000000000..d2b9c85e5c
--- /dev/null
+++ b/cmd/hashpw/main_test.go
@@ -0,0 +1,36 @@
+package main
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/auth"
+)
+
+func TestRunUsage(t *testing.T) {
+ var out bytes.Buffer
+ code := run([]string{"hashpw"}, &out)
+ if code != 1 {
+ t.Fatalf("expected exit code 1, got %d", code)
+ }
+ if !strings.Contains(out.String(), "Usage: hashpw <password>") {
+ t.Fatalf("expected usage output, got %q", out.String())
+ }
+}
+
+func TestRunHashesPassword(t *testing.T) {
+ var out bytes.Buffer
+ code := run([]string{"hashpw", "this-is-a-test-password"}, &out)
+ if code != 0 {
+ t.Fatalf("expected exit code 0, got %d (output: %q)", code, out.String())
+ }
+
+ hash := strings.TrimSpace(out.String())
+ if hash == "" {
+ t.Fatalf("expected hash output, got empty string")
+ }
+ if !auth.CheckPasswordHash("this-is-a-test-password", hash) {
+ t.Fatalf("expected hash to validate against original password")
+ }
+}
diff --git a/frontend-modern/package-lock.json b/frontend-modern/package-lock.json
index 18132824e3..e4e4d31dc6 100644
--- a/frontend-modern/package-lock.json
+++ b/frontend-modern/package-lock.json
@@ -22,6 +22,7 @@
"@types/node": "^20.10.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
+ "@vitest/coverage-v8": "^3.2.4",
"autoprefixer": "^10.4.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.0.0",
@@ -56,6 +57,20 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/@ampproject/remapping": {
+ "version": "2.3.0",
+ "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz",
+ "integrity": "sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "@jridgewell/gen-mapping": "^0.3.5",
+ "@jridgewell/trace-mapping": "^0.3.24"
+ },
+ "engines": {
+ "node": ">=6.0.0"
+ }
+ },
"node_modules/@asamuzakjp/css-color": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/@asamuzakjp/css-color/-/css-color-3.2.0.tgz",
@@ -377,6 +392,16 @@
"node": ">=6.9.0"
}
},
+ "node_modules/@bcoe/v8-coverage": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-1.0.2.tgz",
+ "integrity": "sha512-6zABk/ECA/QYSCQ1NGiVwwbQerUCZ+TQbp64Q3AgmfNvurHH0j8TtXa1qbShXA6qqkpAj4V5W8pP6mLe1mcMqA==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=18"
+ }
+ },
"node_modules/@csstools/color-helpers": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/@csstools/color-helpers/-/color-helpers-5.1.0.tgz",
@@ -1095,6 +1120,63 @@
"dev": true,
"license": "BSD-3-Clause"
},
+ "node_modules/@isaacs/cliui": {
+ "version": "8.0.2",
+ "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz",
+ "integrity": "sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==",
+ "dev": true,
+ "license": "ISC",
+ "dependencies": {
+ "string-width": "^5.1.2",
+ "string-width-cjs": "npm:string-width@^4.2.0",
+ "strip-ansi": "^7.0.1",
+ "strip-ansi-cjs": "npm:strip-ansi@^6.0.1",
+ "wrap-ansi": "^8.1.0",
+ "wrap-ansi-cjs": "npm:wrap-ansi@^7.0.0"
+ },
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/@isaacs/cliui/node_modules/ansi-regex": {
+ "version": "6.2.2",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.2.2.tgz",
+ "integrity": "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/ansi-regex?sponsor=1"
+ }
+ },
+ "node_modules/@isaacs/cliui/node_modules/strip-ansi": {
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.2.tgz",
+ "integrity": "sha512-gmBGslpoQJtgnMAvOVqGZpEz9dyoKTCzy2nfz/n8aIFhN/jCE/rCmcxabB6jOOHV+0WNnylOxaxBQPSvcWklhA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "ansi-regex": "^6.0.1"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/strip-ansi?sponsor=1"
+ }
+ },
+ "node_modules/@istanbuljs/schema": {
+ "version": "0.1.3",
+ "resolved": "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.3.tgz",
+ "integrity": "sha512-ZXRY4jNvVgSVQ8DL3LTcakaAtXwTVUxE81hslsyD2AtoXW/wVob10HkOJ1X/pAlcI7D+2YoZKg5do8G/w6RYgA==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/@jridgewell/gen-mapping": {
"version": "0.3.13",
"resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz",
@@ -1183,6 +1265,17 @@
"node": ">= 8"
}
},
+ "node_modules/@pkgjs/parseargs": {
+ "version": "0.11.0",
+ "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
+ "integrity": "sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==",
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "engines": {
+ "node": ">=14"
+ }
+ },
"node_modules/@rollup/rollup-android-arm-eabi": {
"version": "4.53.3",
"resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.53.3.tgz",
@@ -1919,6 +2012,40 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/@vitest/coverage-v8": {
+ "version": "3.2.4",
+ "resolved": "https://registry.npmjs.org/@vitest/coverage-v8/-/coverage-v8-3.2.4.tgz",
+ "integrity": "sha512-EyF9SXU6kS5Ku/U82E259WSnvg6c8KTjppUncuNdm5QHpe17mwREHnjDzozC8x9MZ0xfBUFSaLkRv4TMA75ALQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@ampproject/remapping": "^2.3.0",
+ "@bcoe/v8-coverage": "^1.0.2",
+ "ast-v8-to-istanbul": "^0.3.3",
+ "debug": "^4.4.1",
+ "istanbul-lib-coverage": "^3.2.2",
+ "istanbul-lib-report": "^3.0.1",
+ "istanbul-lib-source-maps": "^5.0.6",
+ "istanbul-reports": "^3.1.7",
+ "magic-string": "^0.30.17",
+ "magicast": "^0.3.5",
+ "std-env": "^3.9.0",
+ "test-exclude": "^7.0.1",
+ "tinyrainbow": "^2.0.0"
+ },
+ "funding": {
+ "url": "https://opencollective.com/vitest"
+ },
+ "peerDependencies": {
+ "@vitest/browser": "3.2.4",
+ "vitest": "3.2.4"
+ },
+ "peerDependenciesMeta": {
+ "@vitest/browser": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@vitest/expect": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.2.4.tgz",
@@ -2166,6 +2293,25 @@
"node": ">=12"
}
},
+ "node_modules/ast-v8-to-istanbul": {
+ "version": "0.3.9",
+ "resolved": "https://registry.npmjs.org/ast-v8-to-istanbul/-/ast-v8-to-istanbul-0.3.9.tgz",
+ "integrity": "sha512-dSC6tJeOJxbZrPzPbv5mMd6CMiQ1ugaVXXPRad2fXUSsy1kstFn9XQWemV9VW7Y7kpxgQ/4WMoZfwdH8XSU48w==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@jridgewell/trace-mapping": "^0.3.31",
+ "estree-walker": "^3.0.3",
+ "js-tokens": "^9.0.1"
+ }
+ },
+ "node_modules/ast-v8-to-istanbul/node_modules/js-tokens": {
+ "version": "9.0.1",
+ "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-9.0.1.tgz",
+ "integrity": "sha512-mxa9E9ITFOt0ban3j6L5MpjwegGz6lBQmM1IJkWeBZGcMxto50+eWdjC/52xDbS2vy0k7vIMK0Fe2wfL9OQSpQ==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
@@ -2748,6 +2894,13 @@
"node": ">= 0.4"
}
},
+ "node_modules/eastasianwidth": {
+ "version": "0.2.0",
+ "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz",
+ "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/electron-to-chromium": {
"version": "1.5.267",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.267.tgz",
@@ -2755,6 +2908,13 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/emoji-regex": {
+ "version": "9.2.2",
+ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-9.2.2.tgz",
+ "integrity": "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/entities": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/entities/-/entities-6.0.1.tgz",
@@ -3256,6 +3416,23 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/foreground-child": {
+ "version": "3.3.1",
+ "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.3.1.tgz",
+ "integrity": "sha512-gIXjKqtFuWEgzFRJA9WCQeSJLZDjgJUOMCMzxtvFq/37KojM1BFGufqsCy0r4qSQmYLsZYMeyRqzIWOMup03sw==",
+ "dev": true,
+ "license": "ISC",
+ "dependencies": {
+ "cross-spawn": "^7.0.6",
+ "signal-exit": "^4.0.1"
+ },
+ "engines": {
+ "node": ">=14"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
"node_modules/form-data": {
"version": "4.0.5",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz",
@@ -3535,6 +3712,13 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/html-escaper": {
+ "version": "2.0.2",
+ "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz",
+ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/html-tags": {
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/html-tags/-/html-tags-3.3.1.tgz",
@@ -3701,6 +3885,16 @@
"node": ">=0.10.0"
}
},
+ "node_modules/is-fullwidth-code-point": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
+ "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/is-glob": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
@@ -3774,6 +3968,76 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/istanbul-lib-coverage": {
+ "version": "3.2.2",
+ "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz",
+ "integrity": "sha512-O8dpsF+r0WV/8MNRKfnmrtCWhuKjxrq2w+jpzBL5UZKTi2LeVWnWOmWRxFlesJONmc+wLAGvKQZEOanko0LFTg==",
+ "dev": true,
+ "license": "BSD-3-Clause",
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/istanbul-lib-report": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/istanbul-lib-report/-/istanbul-lib-report-3.0.1.tgz",
+ "integrity": "sha512-GCfE1mtsHGOELCU8e/Z7YWzpmybrx/+dSTfLrvY8qRmaY6zXTKWn6WQIjaAFw069icm6GVMNkgu0NzI4iPZUNw==",
+ "dev": true,
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "istanbul-lib-coverage": "^3.0.0",
+ "make-dir": "^4.0.0",
+ "supports-color": "^7.1.0"
+ },
+ "engines": {
+ "node": ">=10"
+ }
+ },
+ "node_modules/istanbul-lib-source-maps": {
+ "version": "5.0.6",
+ "resolved": "https://registry.npmjs.org/istanbul-lib-source-maps/-/istanbul-lib-source-maps-5.0.6.tgz",
+ "integrity": "sha512-yg2d+Em4KizZC5niWhQaIomgf5WlL4vOOjZ5xGCmF8SnPE/mDWWXgvRExdcpCgh9lLRRa1/fSYp2ymmbJ1pI+A==",
+ "dev": true,
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "@jridgewell/trace-mapping": "^0.3.23",
+ "debug": "^4.1.1",
+ "istanbul-lib-coverage": "^3.0.0"
+ },
+ "engines": {
+ "node": ">=10"
+ }
+ },
+ "node_modules/istanbul-reports": {
+ "version": "3.2.0",
+ "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.2.0.tgz",
+ "integrity": "sha512-HGYWWS/ehqTV3xN10i23tkPkpH46MLCIMFNCaaKNavAXTF1RkqxawEPtnjnGZ6XKSInBKkiOA5BKS+aZiY3AvA==",
+ "dev": true,
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "html-escaper": "^2.0.0",
+ "istanbul-lib-report": "^3.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/jackspeak": {
+ "version": "3.4.3",
+ "resolved": "https://registry.npmjs.org/jackspeak/-/jackspeak-3.4.3.tgz",
+ "integrity": "sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw==",
+ "dev": true,
+ "license": "BlueOak-1.0.0",
+ "dependencies": {
+ "@isaacs/cliui": "^8.0.2"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ },
+ "optionalDependencies": {
+ "@pkgjs/parseargs": "^0.11.0"
+ }
+ },
"node_modules/jiti": {
"version": "1.21.7",
"resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.7.tgz",
@@ -4018,6 +4282,34 @@
"@jridgewell/sourcemap-codec": "^1.5.5"
}
},
+ "node_modules/magicast": {
+ "version": "0.3.5",
+ "resolved": "https://registry.npmjs.org/magicast/-/magicast-0.3.5.tgz",
+ "integrity": "sha512-L0WhttDl+2BOsybvEOLK7fW3UA0OQ0IQ2d6Zl2x/a6vVRs3bAY0ECOSHHeL5jD+SbOpOCUEi0y1DgHEn9Qn1AQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@babel/parser": "^7.25.4",
+ "@babel/types": "^7.25.4",
+ "source-map-js": "^1.2.0"
+ }
+ },
+ "node_modules/make-dir": {
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-4.0.0.tgz",
+ "integrity": "sha512-hXdUTZYIVOt1Ex//jAQi+wTZZpUpwBj/0QsOzqegb3rGMMeJiSEu5xLHnYfBrRV4RH2+OCSOO95Is/7x1WJ4bw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "semver": "^7.5.3"
+ },
+ "engines": {
+ "node": ">=10"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
"node_modules/marked": {
"version": "17.0.1",
"resolved": "https://registry.npmjs.org/marked/-/marked-17.0.1.tgz",
@@ -4129,6 +4421,16 @@
"url": "https://github.com/sponsors/isaacs"
}
},
+ "node_modules/minipass": {
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.2.tgz",
+ "integrity": "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==",
+ "dev": true,
+ "license": "ISC",
+ "engines": {
+ "node": ">=16 || 14 >=14.17"
+ }
+ },
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
@@ -4288,6 +4590,13 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/package-json-from-dist": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/package-json-from-dist/-/package-json-from-dist-1.0.1.tgz",
+ "integrity": "sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==",
+ "dev": true,
+ "license": "BlueOak-1.0.0"
+ },
"node_modules/parent-module": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
@@ -4351,6 +4660,23 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/path-scurry": {
+ "version": "1.11.1",
+ "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.11.1.tgz",
+ "integrity": "sha512-Xa4Nw17FS9ApQFJ9umLiJS4orGjm7ZzwUrwamcGQuHSzDyth9boKDaycYdDcZDuqYATXw4HFXgaqWTctW/v1HA==",
+ "dev": true,
+ "license": "BlueOak-1.0.0",
+ "dependencies": {
+ "lru-cache": "^10.2.0",
+ "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0"
+ },
+ "engines": {
+ "node": ">=16 || 14 >=14.18"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
"node_modules/pathe": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz",
@@ -4959,6 +5285,19 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/signal-exit": {
+ "version": "4.1.0",
+ "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz",
+ "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==",
+ "dev": true,
+ "license": "ISC",
+ "engines": {
+ "node": ">=14"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
"node_modules/solid-js": {
"version": "1.9.10",
"resolved": "https://registry.npmjs.org/solid-js/-/solid-js-1.9.10.tgz",
@@ -5010,6 +5349,76 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/string-width": {
+ "version": "5.1.2",
+ "resolved": "https://registry.npmjs.org/string-width/-/string-width-5.1.2.tgz",
+ "integrity": "sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "eastasianwidth": "^0.2.0",
+ "emoji-regex": "^9.2.2",
+ "strip-ansi": "^7.0.1"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
+ "node_modules/string-width-cjs": {
+ "name": "string-width",
+ "version": "4.2.3",
+ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
+ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "emoji-regex": "^8.0.0",
+ "is-fullwidth-code-point": "^3.0.0",
+ "strip-ansi": "^6.0.1"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/string-width-cjs/node_modules/emoji-regex": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
+ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/string-width/node_modules/ansi-regex": {
+ "version": "6.2.2",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.2.2.tgz",
+ "integrity": "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/ansi-regex?sponsor=1"
+ }
+ },
+ "node_modules/string-width/node_modules/strip-ansi": {
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.2.tgz",
+ "integrity": "sha512-gmBGslpoQJtgnMAvOVqGZpEz9dyoKTCzy2nfz/n8aIFhN/jCE/rCmcxabB6jOOHV+0WNnylOxaxBQPSvcWklhA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "ansi-regex": "^6.0.1"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/strip-ansi?sponsor=1"
+ }
+ },
"node_modules/strip-ansi": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
@@ -5023,6 +5432,20 @@
"node": ">=8"
}
},
+ "node_modules/strip-ansi-cjs": {
+ "name": "strip-ansi",
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
+ "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "ansi-regex": "^5.0.1"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/strip-indent": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/strip-indent/-/strip-indent-3.0.0.tgz",
@@ -5188,6 +5611,42 @@
"node": ">=4"
}
},
+ "node_modules/test-exclude": {
+ "version": "7.0.1",
+ "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-7.0.1.tgz",
+ "integrity": "sha512-pFYqmTw68LXVjeWJMST4+borgQP2AyMNbg1BpZh9LbyhUeNkeaPF9gzfPGUAnSMV3qPYdWUwDIjjCLiSDOl7vg==",
+ "dev": true,
+ "license": "ISC",
+ "dependencies": {
+ "@istanbuljs/schema": "^0.1.2",
+ "glob": "^10.4.1",
+ "minimatch": "^9.0.4"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+ },
+ "node_modules/test-exclude/node_modules/glob": {
+ "version": "10.5.0",
+ "resolved": "https://registry.npmjs.org/glob/-/glob-10.5.0.tgz",
+ "integrity": "sha512-DfXN8DfhJ7NH3Oe7cFmu3NCu1wKbkReJ8TorzSAFbSKrlNaQSKfIzqYqVY8zlbs2NLBbWpRiU52GX2PbaBVNkg==",
+ "dev": true,
+ "license": "ISC",
+ "dependencies": {
+ "foreground-child": "^3.1.0",
+ "jackspeak": "^3.1.2",
+ "minimatch": "^9.0.4",
+ "minipass": "^7.1.2",
+ "package-json-from-dist": "^1.0.0",
+ "path-scurry": "^1.11.1"
+ },
+ "bin": {
+ "glob": "dist/esm/bin.mjs"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
"node_modules/text-table": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz",
@@ -5672,6 +6131,7 @@
"integrity": "sha512-LUCP5ev3GURDysTWiP47wRRUpLKMOfPh+yKTx3kVIEiu5KOMeqzpnYNsKyOoVrULivR8tLcks4+lga33Whn90A==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@types/chai": "^5.2.2",
"@vitest/expect": "3.2.4",
@@ -5855,6 +6315,107 @@
"node": ">=0.10.0"
}
},
+ "node_modules/wrap-ansi": {
+ "version": "8.1.0",
+ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz",
+ "integrity": "sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "ansi-styles": "^6.1.0",
+ "string-width": "^5.0.1",
+ "strip-ansi": "^7.0.1"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/wrap-ansi?sponsor=1"
+ }
+ },
+ "node_modules/wrap-ansi-cjs": {
+ "name": "wrap-ansi",
+ "version": "7.0.0",
+ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz",
+ "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "ansi-styles": "^4.0.0",
+ "string-width": "^4.1.0",
+ "strip-ansi": "^6.0.0"
+ },
+ "engines": {
+ "node": ">=10"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/wrap-ansi?sponsor=1"
+ }
+ },
+ "node_modules/wrap-ansi-cjs/node_modules/emoji-regex": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
+ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/wrap-ansi-cjs/node_modules/string-width": {
+ "version": "4.2.3",
+ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
+ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "emoji-regex": "^8.0.0",
+ "is-fullwidth-code-point": "^3.0.0",
+ "strip-ansi": "^6.0.1"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/wrap-ansi/node_modules/ansi-regex": {
+ "version": "6.2.2",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.2.2.tgz",
+ "integrity": "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/ansi-regex?sponsor=1"
+ }
+ },
+ "node_modules/wrap-ansi/node_modules/ansi-styles": {
+ "version": "6.2.3",
+ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz",
+ "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/ansi-styles?sponsor=1"
+ }
+ },
+ "node_modules/wrap-ansi/node_modules/strip-ansi": {
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.2.tgz",
+ "integrity": "sha512-gmBGslpoQJtgnMAvOVqGZpEz9dyoKTCzy2nfz/n8aIFhN/jCE/rCmcxabB6jOOHV+0WNnylOxaxBQPSvcWklhA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "ansi-regex": "^6.0.1"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/strip-ansi?sponsor=1"
+ }
+ },
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
diff --git a/frontend-modern/package.json b/frontend-modern/package.json
index 9c94d57259..979fb74ab0 100644
--- a/frontend-modern/package.json
+++ b/frontend-modern/package.json
@@ -19,6 +19,8 @@
"preview": "vite preview",
"generate-types": "cd ../scripts && go run generate-types.go",
"test": "vitest run",
+ "test:coverage": "vitest run --coverage --coverage.provider=v8 --coverage.include=src/**/*.ts --coverage.include=src/**/*.tsx --coverage.exclude=src/index.tsx",
+ "test:coverage:ai": "vitest run --coverage --coverage.provider=v8 --coverage.thresholds.100 --coverage.thresholds.perFile --coverage.include=src/components/AI/aiChatUtils.ts",
"type-check": "tsc --noEmit",
"lint": "eslint \"src/**/*.{ts,tsx}\"",
"lint:fix": "eslint \"src/**/*.{ts,tsx}\" --fix",
@@ -39,6 +41,7 @@
"@types/node": "^20.10.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
+ "@vitest/coverage-v8": "^3.2.4",
"autoprefixer": "^10.4.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.0.0",
diff --git a/frontend-modern/src/api/__tests__/ai.test.ts b/frontend-modern/src/api/__tests__/ai.test.ts
new file mode 100644
index 0000000000..103e9302c9
--- /dev/null
+++ b/frontend-modern/src/api/__tests__/ai.test.ts
@@ -0,0 +1,130 @@
+import { describe, expect, it, beforeEach, vi } from 'vitest';
+
+vi.mock('@/utils/apiClient', () => ({
+ apiFetchJSON: vi.fn(),
+ apiFetch: vi.fn(),
+}));
+
+vi.mock('@/utils/logger', () => ({
+ logger: {
+ debug: vi.fn(),
+ info: vi.fn(),
+ warn: vi.fn(),
+ error: vi.fn(),
+ },
+}));
+
+import { AIAPI } from '@/api/ai';
+import { apiFetch, apiFetchJSON } from '@/utils/apiClient';
+
+describe('AIAPI', () => {
+ const apiFetchJSONMock = vi.mocked(apiFetchJSON);
+ const apiFetchMock = vi.mocked(apiFetch);
+
+ beforeEach(() => {
+ apiFetchJSONMock.mockReset();
+ apiFetchMock.mockReset();
+ });
+
+ it('calls the expected endpoints for settings and models', async () => {
+ apiFetchJSONMock.mockResolvedValueOnce({ configured: true } as any);
+ await AIAPI.getSettings();
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/settings/ai');
+
+ apiFetchJSONMock.mockResolvedValueOnce({ configured: true } as any);
+ await AIAPI.updateSettings({ enabled: true });
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/settings/ai/update', {
+ method: 'PUT',
+ body: JSON.stringify({ enabled: true }),
+ });
+
+ apiFetchJSONMock.mockResolvedValueOnce({ models: [] } as any);
+ await AIAPI.getModels();
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/models');
+ });
+
+ it('includes query parameters for cost and intelligence endpoints', async () => {
+ apiFetchJSONMock.mockResolvedValueOnce({} as any);
+ await AIAPI.getCostSummary();
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/cost/summary?days=30');
+
+ apiFetchJSONMock.mockResolvedValueOnce({} as any);
+ await AIAPI.getCostSummary(7);
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/cost/summary?days=7');
+
+ apiFetchJSONMock.mockResolvedValueOnce({} as any);
+ await AIAPI.getPatterns('vm:101');
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/intelligence/patterns?resource_id=vm%3A101');
+
+ apiFetchJSONMock.mockResolvedValueOnce({} as any);
+ await AIAPI.getRecentChanges(12);
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/intelligence/changes?hours=12');
+ });
+
+ it('sanitizes runCommand payload consistently', async () => {
+ apiFetchJSONMock.mockResolvedValueOnce({ output: 'ok', success: true } as any);
+ await AIAPI.runCommand({
+ command: 'echo hi',
+ target_type: 'vm',
+ target_id: 'vm-101',
+ run_on_host: undefined as any,
+ vmid: 101,
+ target_host: 'delly',
+ });
+
+ expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/run-command', {
+ method: 'POST',
+ body: JSON.stringify({
+ command: 'echo hi',
+ target_type: 'vm',
+ target_id: 'vm-101',
+ run_on_host: false,
+ vmid: '101',
+ target_host: 'delly',
+ }),
+ });
+ });
+
+ it('throws useful errors for investigateAlert failures', async () => {
+ apiFetchMock.mockResolvedValueOnce(new Response('backend error', { status: 500 }));
+ await expect(
+ AIAPI.investigateAlert(
+ {
+ alert_id: 'a1',
+ resource_id: 'r1',
+ resource_name: 'res',
+ resource_type: 'vm',
+ alert_type: 'cpu',
+ level: 'warning',
+ value: 1,
+ threshold: 2,
+ message: 'msg',
+ duration: '1m',
+ },
+ () => undefined
+ )
+ ).rejects.toThrow('backend error');
+ });
+
+ it('throws when investigateAlert has no streaming body', async () => {
+ apiFetchMock.mockResolvedValueOnce(new Response(null, { status: 200 }));
+ await expect(
+ AIAPI.investigateAlert(
+ {
+ alert_id: 'a1',
+ resource_id: 'r1',
+ resource_name: 'res',
+ resource_type: 'vm',
+ alert_type: 'cpu',
+ level: 'warning',
+ value: 1,
+ threshold: 2,
+ message: 'msg',
+ duration: '1m',
+ },
+ () => undefined
+ )
+ ).rejects.toThrow('No response body');
+ });
+});
+
diff --git a/frontend-modern/src/api/ai.ts b/frontend-modern/src/api/ai.ts
index 5c3e690653..9424161e75 100644
--- a/frontend-modern/src/api/ai.ts
+++ b/frontend-modern/src/api/ai.ts
@@ -141,7 +141,7 @@ export class AIAPI {
target_type: string;
target_id: string;
run_on_host: boolean;
- vmid?: string;
+ vmid?: string | number;
target_host?: string; // Explicit host for command routing
}): Promise<{ output: string; success: boolean; error?: string }> {
// Ensure run_on_host is explicitly a boolean (not undefined)
diff --git a/frontend-modern/src/components/AI/AIChat.tsx b/frontend-modern/src/components/AI/AIChat.tsx
index a9e90d24dc..d85b3f7e5a 100644
--- a/frontend-modern/src/components/AI/AIChat.tsx
+++ b/frontend-modern/src/components/AI/AIChat.tsx
@@ -1,12 +1,17 @@
import { Component, Show, createSignal, For, createEffect, createMemo, onMount, Switch, Match } from 'solid-js';
-import { marked } from 'marked';
-import DOMPurify from 'dompurify';
import { AIAPI } from '@/api/ai';
import { notificationStore } from '@/stores/notifications';
import { logger } from '@/utils/logger';
import { aiChatStore } from '@/stores/aiChat';
import { useWebSocket } from '@/App';
import { GuestNotes } from './GuestNotes';
+import {
+ PROVIDER_DISPLAY_NAMES,
+ getGuestName,
+ groupModelsByProvider,
+ renderMarkdown,
+ sanitizeThinking,
+} from './aiChatUtils';
import type {
AIToolExecution,
AIStreamEvent,
@@ -17,115 +22,6 @@ import type {
ModelInfo,
} from '@/types/ai';
-// Provider display names for grouped model selection
-const PROVIDER_DISPLAY_NAMES: Record<string, string> = {
- anthropic: 'Anthropic',
- openai: 'OpenAI',
- deepseek: 'DeepSeek',
- ollama: 'Ollama',
-};
-
-// Parse provider from model ID (format: "provider:model-name")
-function getProviderFromModelId(modelId: string): string {
- const colonIndex = modelId.indexOf(':');
- if (colonIndex > 0) {
- return modelId.substring(0, colonIndex);
- }
- // Default detection for models without prefix
- if (modelId.includes('claude') || modelId.includes('opus') || modelId.includes('sonnet') || modelId.includes('haiku')) {
- return 'anthropic';
- }
- if (modelId.includes('gpt') || modelId.includes('o1') || modelId.includes('o3')) {
- return 'openai';
- }
- if (modelId.includes('deepseek')) {
- return 'deepseek';
- }
- return 'ollama';
-}
-
-// Group models by provider for grouped rendering
-function groupModelsByProvider(models: ModelInfo[]): Map<string, ModelInfo[]> {
- const grouped = new Map<string, ModelInfo[]>();
-
- for (const model of models) {
- const provider = getProviderFromModelId(model.id);
- const existing = grouped.get(provider) || [];
- existing.push(model);
- grouped.set(provider, existing);
- }
-
- return grouped;
-}
-
-// Configure marked for safe rendering
-marked.setOptions({
- breaks: true, // Convert \n to <br>
- gfm: true, // GitHub Flavored Markdown
-});
-
-let domPurifyConfigured = false;
-const configureDOMPurify = () => {
- if (domPurifyConfigured) return;
- domPurifyConfigured = true;
-
- DOMPurify.addHook('afterSanitizeAttributes', (node) => {
- const element = node as Element | null;
- if (!element || element.tagName !== 'A') return;
- element.setAttribute('target', '_blank');
- element.setAttribute('rel', 'noopener noreferrer');
- });
-};
-
-// Helper to render markdown safely with XSS protection
-// LLM output should NEVER be trusted - always sanitize before rendering as HTML
-const renderMarkdown = (content: string): string => {
- try {
- configureDOMPurify();
- const rawHtml = marked.parse(content) as string;
- // Sanitize to prevent XSS from malicious LLM output or injected content
- return DOMPurify.sanitize(rawHtml, {
- // Allow common formatting tags but block scripts, iframes, etc.
- ALLOWED_TAGS: ['p', 'br', 'strong', 'em', 'b', 'i', 'u', 'code', 'pre', 'blockquote',
- 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'hr', 'table',
- 'thead', 'tbody', 'tr', 'th', 'td', 'span', 'div'],
- ALLOWED_ATTR: ['href', 'target', 'rel', 'class'],
- // Force all links to open in new tab and prevent opener attacks
- ADD_ATTR: ['target', 'rel'],
- });
- } catch {
- // If parsing fails, escape HTML entities as fallback
- return content.replace(/[&<>"']/g, (char) => {
- const entities: Record<string, string> = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
- return entities[char] || char;
- });
- }
-};
-
-// Helper to sanitize thinking/reasoning content for display
-// Removes raw network errors with IP addresses that are not user-friendly
-const sanitizeThinking = (content: string): string => {
- // Replace raw TCP connection details like "write tcp 192.168.0.123:7655->192.168.0.134:58004: i/o timeout"
- // with friendlier messages
- let sanitized = content.replace(
- /write tcp [\d.:]+->[\d.:]+: i\/o timeout/g,
- 'connection timed out'
- );
- sanitized = sanitized.replace(
- /read tcp [\d.:]+: i\/o timeout/g,
- 'connection timed out'
- );
- sanitized = sanitized.replace(
- /dial tcp [\d.:]+: connection refused/g,
- 'connection refused'
- );
- // Replace "failed to send command: <raw error>" patterns
- sanitized = sanitized.replace(
- /failed to send command: write tcp [\d.:->\s]+/g,
- 'failed to send command: connection error'
- );
- return sanitized;
-};
// In-progress tool execution (before completion)
interface PendingTool {
@@ -173,14 +69,6 @@ interface AIChatProps {
onClose: () => void;
}
-// Extract guest name from context if available
-const getGuestName = (context?: Record<string, unknown>): string | undefined => {
- if (!context) return undefined;
- if (typeof context.guestName === 'string') return context.guestName;
- if (typeof context.name === 'string') return context.name;
- return undefined;
-};
-
export const AIChat: Component<AIChatProps> = (props) => {
// Read all context from store for proper SolidJS reactivity
const isOpen = () => aiChatStore.isOpen;
diff --git a/frontend-modern/src/components/AI/__tests__/aiChatUtils.test.ts b/frontend-modern/src/components/AI/__tests__/aiChatUtils.test.ts
new file mode 100644
index 0000000000..7eed9c4468
--- /dev/null
+++ b/frontend-modern/src/components/AI/__tests__/aiChatUtils.test.ts
@@ -0,0 +1,104 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import * as utils from '@/components/AI/aiChatUtils';
+import { marked } from 'marked';
+import type { ModelInfo } from '@/types/ai';
+
+describe('aiChatUtils', () => {
+ describe('getProviderFromModelId', () => {
+ it('uses explicit provider prefix when present', () => {
+ expect(utils.getProviderFromModelId('openai:gpt-4o')).toBe('openai');
+ expect(utils.getProviderFromModelId('anthropic:claude-3-5-sonnet')).toBe('anthropic');
+ });
+
+ it('detects provider from known model naming', () => {
+ expect(utils.getProviderFromModelId('claude-3-5-sonnet')).toBe('anthropic');
+ expect(utils.getProviderFromModelId('o3-mini')).toBe('openai');
+ expect(utils.getProviderFromModelId('deepseek-r1')).toBe('deepseek');
+ expect(utils.getProviderFromModelId('llama3.1')).toBe('ollama');
+ });
+
+ it('handles odd strings without a provider prefix', () => {
+ // colon at index 0 should not be treated as a provider prefix
+ expect(utils.getProviderFromModelId(':gpt-4o')).toBe('openai');
+ expect(utils.getProviderFromModelId(':unknown-model')).toBe('ollama');
+ });
+ });
+
+ describe('groupModelsByProvider', () => {
+ it('groups models by detected provider', () => {
+ const models: ModelInfo[] = [
+ { id: 'openai:gpt-4o', name: 'GPT-4o' },
+ { id: 'claude-3-5-sonnet', name: 'Claude 3.5 Sonnet' },
+ { id: 'deepseek-r1', name: 'DeepSeek R1' },
+ { id: 'ollama:llama3.1', name: 'Llama 3.1' },
+ ];
+
+ const grouped = utils.groupModelsByProvider(models);
+ expect(Array.from(grouped.keys()).sort()).toEqual(['anthropic', 'deepseek', 'ollama', 'openai']);
+ expect(grouped.get('openai')?.map((m) => m.id)).toEqual(['openai:gpt-4o']);
+ expect(grouped.get('anthropic')?.map((m) => m.id)).toEqual(['claude-3-5-sonnet']);
+ });
+ });
+
+ describe('sanitizeThinking', () => {
+ it('replaces raw tcp timeout and connection errors', () => {
+ const input = [
+ 'write tcp 192.168.0.123:7655->192.168.0.134:58004: i/o timeout',
+ 'read tcp 10.0.0.1: i/o timeout',
+ 'dial tcp 127.0.0.1: connection refused',
+ 'failed to send command: write tcp 192.168.0.123:7655->192.168.0.134:58004: i/o timeout',
+ ].join('\n');
+
+ const output = utils.sanitizeThinking(input);
+ expect(output).toContain('connection timed out');
+ expect(output).toContain('connection refused');
+ expect(output).toContain('failed to send command: connection error');
+ expect(output).not.toContain('192.168.0.123');
+ expect(output).not.toContain('10.0.0.1');
+ expect(output).not.toContain('127.0.0.1');
+ });
+ });
+
+ describe('getGuestName', () => {
+ it('prefers guestName then falls back to name', () => {
+ expect(utils.getGuestName(undefined)).toBeUndefined();
+ expect(utils.getGuestName({})).toBeUndefined();
+ expect(utils.getGuestName({ name: 'vm-101' })).toBe('vm-101');
+ expect(utils.getGuestName({ guestName: 'container-202', name: 'ignored' })).toBe('container-202');
+ });
+ });
+
+ describe('renderMarkdown', () => {
+ it('sanitizes HTML and forces safe link attributes', () => {
+ const output = utils.renderMarkdown(
+ [
+ 'Hello',
+ '',
+ '[link](https://example.com)',
+ '',
+ '<script>alert("xss")</script>',
+ ].join('\n')
+ );
+
+ expect(output).toContain('Hello');
+ expect(output).toContain('<a');
+ expect(output).toContain('href="https://example.com"');
+ expect(output).toContain('target="_blank"');
+ expect(output).toContain('rel="noopener noreferrer"');
+ expect(output).not.toContain('<script');
+ expect(output).not.toContain('alert("xss")');
+ });
+
+ it('escapes HTML entities if markdown parsing fails', () => {
+ const spy = vi.spyOn(marked, 'parse').mockImplementation(() => {
+ throw new Error('boom');
+ });
+
+ const output = utils.renderMarkdown(`a&b <c> "d" 'e'`);
+ expect(output).toBe('a&amp;b &lt;c&gt; &quot;d&quot; &#39;e&#39;');
+
+ spy.mockRestore();
+ });
+ });
+});
diff --git a/frontend-modern/src/components/AI/aiChatUtils.ts b/frontend-modern/src/components/AI/aiChatUtils.ts
new file mode 100644
index 0000000000..b6fb3b0fed
--- /dev/null
+++ b/frontend-modern/src/components/AI/aiChatUtils.ts
@@ -0,0 +1,123 @@
+import { marked } from 'marked';
+import DOMPurify from 'dompurify';
+import type { ModelInfo } from '@/types/ai';
+
+// Provider display names for grouped model selection
+export const PROVIDER_DISPLAY_NAMES: Record<string, string> = {
+ anthropic: 'Anthropic',
+ openai: 'OpenAI',
+ deepseek: 'DeepSeek',
+ ollama: 'Ollama',
+};
+
+// Parse provider from model ID (format: "provider:model-name")
+export function getProviderFromModelId(modelId: string): string {
+ const colonIndex = modelId.indexOf(':');
+ if (colonIndex > 0) {
+ return modelId.substring(0, colonIndex);
+ }
+ // Default detection for models without prefix
+ if (modelId.includes('claude') || modelId.includes('opus') || modelId.includes('sonnet') || modelId.includes('haiku')) {
+ return 'anthropic';
+ }
+ if (modelId.includes('gpt') || modelId.includes('o1') || modelId.includes('o3')) {
+ return 'openai';
+ }
+ if (modelId.includes('deepseek')) {
+ return 'deepseek';
+ }
+ return 'ollama';
+}
+
+// Group models by provider for grouped rendering
+export function groupModelsByProvider(models: ModelInfo[]): Map<string, ModelInfo[]> {
+ const grouped = new Map<string, ModelInfo[]>();
+
+ for (const model of models) {
+ const provider = getProviderFromModelId(model.id);
+ const existing = grouped.get(provider) || [];
+ existing.push(model);
+ grouped.set(provider, existing);
+ }
+
+ return grouped;
+}
+
+// Configure marked for safe rendering
+marked.setOptions({
+ breaks: true, // Convert \n to <br>
+ gfm: true, // GitHub Flavored Markdown
+});
+
+let domPurifyConfigured = false;
+const configureDOMPurify = () => {
+ if (domPurifyConfigured) return;
+ domPurifyConfigured = true;
+
+ DOMPurify.addHook('afterSanitizeAttributes', (node) => {
+ const element = node as Element | null;
+ if (!element || element.tagName !== 'A') return;
+ element.setAttribute('target', '_blank');
+ element.setAttribute('rel', 'noopener noreferrer');
+ });
+};
+
+// Helper to render markdown safely with XSS protection
+// LLM output should NEVER be trusted - always sanitize before rendering as HTML
+export const renderMarkdown = (content: string): string => {
+ try {
+ configureDOMPurify();
+ const rawHtml = marked.parse(content) as string;
+ // Sanitize to prevent XSS from malicious LLM output or injected content
+ return DOMPurify.sanitize(rawHtml, {
+ // Allow common formatting tags but block scripts, iframes, etc.
+ ALLOWED_TAGS: ['p', 'br', 'strong', 'em', 'b', 'i', 'u', 'code', 'pre', 'blockquote',
+ 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'hr', 'table',
+ 'thead', 'tbody', 'tr', 'th', 'td', 'span', 'div'],
+ ALLOWED_ATTR: ['href', 'target', 'rel', 'class'],
+ // Force all links to open in new tab and prevent opener attacks
+ ADD_ATTR: ['target', 'rel'],
+ });
+ } catch {
+ // If parsing fails, escape HTML entities as fallback
+ return content.replace(/[&<>"']/g, (char) => {
+ const entities: Record<string, string> = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
+ return entities[char];
+ });
+ }
+};
+
+// Helper to sanitize thinking/reasoning content for display
+// Removes raw network errors with IP addresses that are not user-friendly
+export const sanitizeThinking = (content: string): string => {
+ // Replace raw TCP connection details like "write tcp 192.168.0.123:7655->192.168.0.134:58004: i/o timeout"
+ // with friendlier messages
+ // Replace "failed to send command: <raw error>" patterns first so follow-on replacements
+ // don't partially sanitize the error and prevent the higher-level pattern from matching.
+ let sanitized = content.replace(
+ /failed to send command: write tcp [\d.:->\s]+/g,
+ 'failed to send command: connection error'
+ );
+
+ sanitized = sanitized.replace(
+ /write tcp [\d.:]+->[\d.:]+: i\/o timeout/g,
+ 'connection timed out'
+ );
+ sanitized = sanitized.replace(
+ /read tcp [\d.:]+: i\/o timeout/g,
+ 'connection timed out'
+ );
+ sanitized = sanitized.replace(
+ /dial tcp [\d.:]+: connection refused/g,
+ 'connection refused'
+ );
+ return sanitized;
+};
+
+// Extract guest name from context if available
+export const getGuestName = (context?: Record<string, unknown>): string | undefined => {
+ if (!context) return undefined;
+ if (typeof context.guestName === 'string') return context.guestName;
+ if (typeof context.name === 'string') return context.name;
+ return undefined;
+};
diff --git a/frontend-modern/src/stores/__tests__/aiChat.test.ts b/frontend-modern/src/stores/__tests__/aiChat.test.ts
new file mode 100644
index 0000000000..4bc1da723d
--- /dev/null
+++ b/frontend-modern/src/stores/__tests__/aiChat.test.ts
@@ -0,0 +1,98 @@
+import { describe, expect, it, beforeEach } from 'vitest';
+import { aiChatStore } from '@/stores/aiChat';
+
+describe('aiChatStore', () => {
+ beforeEach(() => {
+ aiChatStore.close();
+ aiChatStore.clearContext();
+ aiChatStore.clearAllContext();
+ aiChatStore.clearConversation();
+ aiChatStore.setMessages([]);
+ aiChatStore.registerInput(null);
+ aiChatStore.setEnabled(false);
+ });
+
+ it('opens, closes, and toggles', () => {
+ expect(aiChatStore.isOpen).toBe(false);
+ aiChatStore.open();
+ expect(aiChatStore.isOpen).toBe(true);
+ aiChatStore.toggle();
+ expect(aiChatStore.isOpen).toBe(false);
+ aiChatStore.toggle();
+ expect(aiChatStore.isOpen).toBe(true);
+ aiChatStore.close();
+ expect(aiChatStore.isOpen).toBe(false);
+ });
+
+ it('sets legacy context and clears it', () => {
+ aiChatStore.setContext({ targetType: 'vm', targetId: 'vm-101', context: { name: 'vm-101' } });
+ expect(aiChatStore.context.targetType).toBe('vm');
+ expect(aiChatStore.context.targetId).toBe('vm-101');
+ aiChatStore.clearContext();
+ expect(aiChatStore.context.targetType).toBeUndefined();
+ });
+
+ it('adds context items without duplicates and updates legacy context', () => {
+ expect(aiChatStore.contextItems).toHaveLength(0);
+ expect(aiChatStore.hasContextItem('vm-101')).toBe(false);
+
+ aiChatStore.addContextItem('vm', 'vm-101', 'vm-101', { guestName: 'vm-101', a: 1 });
+ expect(aiChatStore.contextItems).toHaveLength(1);
+ expect(aiChatStore.hasContextItem('vm-101')).toBe(true);
+ expect(aiChatStore.context.targetId).toBe('vm-101');
+ expect(aiChatStore.context.context).toMatchObject({ a: 1 });
+
+ aiChatStore.addContextItem('vm', 'vm-101', 'vm-101', { guestName: 'vm-101', a: 2 });
+ expect(aiChatStore.contextItems).toHaveLength(1);
+ expect(aiChatStore.contextItems[0].data).toMatchObject({ a: 2 });
+ expect(aiChatStore.context.targetId).toBe('vm-101');
+ expect(aiChatStore.context.context).toMatchObject({ a: 2 });
+ });
+
+ it('removes context items and keeps legacy context consistent', () => {
+ aiChatStore.addContextItem('vm', 'vm-101', 'vm-101', { name: 'vm-101' });
+ aiChatStore.addContextItem('node', 'node-1', 'node-1', { name: 'node-1' });
+ expect(aiChatStore.contextItems).toHaveLength(2);
+ expect(aiChatStore.context.targetId).toBe('node-1');
+
+ aiChatStore.removeContextItem('node-1');
+ expect(aiChatStore.contextItems).toHaveLength(1);
+ expect(aiChatStore.context.targetId).toBe('vm-101');
+
+ aiChatStore.removeContextItem('vm-101');
+ expect(aiChatStore.contextItems).toHaveLength(0);
+ expect(aiChatStore.context.targetId).toBeUndefined();
+ });
+
+ it('setTargetContext and openForTarget derive a sensible name', () => {
+ aiChatStore.setTargetContext('vm', 'vm-101', { guestName: 'my-guest' });
+ expect(aiChatStore.contextItems).toHaveLength(1);
+ expect(aiChatStore.contextItems[0].name).toBe('my-guest');
+ expect(aiChatStore.context.targetId).toBe('vm-101');
+
+ aiChatStore.openForTarget('node', 'node-1', { name: 'delly' });
+ expect(aiChatStore.isOpen).toBe(true);
+ expect(aiChatStore.contextItems).toHaveLength(2);
+ expect(aiChatStore.contextItems[1].name).toBe('delly');
+ });
+
+ it('opens with a pre-filled prompt', () => {
+ aiChatStore.openWithPrompt('hello', { targetType: 'vm', targetId: 'vm-101' });
+ expect(aiChatStore.isOpen).toBe(true);
+ expect(aiChatStore.context.initialPrompt).toBe('hello');
+ expect(aiChatStore.context.targetId).toBe('vm-101');
+ });
+
+ it('focusInput returns false when closed and true when open with a registered element', () => {
+ const textarea = document.createElement('textarea');
+ document.body.appendChild(textarea);
+ aiChatStore.registerInput(textarea);
+
+ expect(aiChatStore.focusInput()).toBe(false);
+ aiChatStore.open();
+ expect(aiChatStore.focusInput()).toBe(true);
+ expect(document.activeElement).toBe(textarea);
+
+ textarea.remove();
+ });
+});
diff --git a/internal/agentexec/policy_test.go b/internal/agentexec/policy_test.go
new file mode 100644
index 0000000000..45d3975f00
--- /dev/null
+++ b/internal/agentexec/policy_test.go
@@ -0,0 +1,48 @@
+package agentexec
+
+import "testing"
+
+func TestCompilePatternsIgnoresInvalidRegex(t *testing.T) {
+ res := compilePatterns([]string{"^df(\\s|$)", "["})
+ if len(res) != 1 {
+ t.Fatalf("expected 1 compiled regex, got %d", len(res))
+ }
+}
+
+func TestDefaultPolicyEvaluate(t *testing.T) {
+ p := DefaultPolicy()
+
+ cases := []struct {
+ name string
+ command string
+ want PolicyDecision
+ }{
+ {"blocked", "rm -rf /", PolicyBlock},
+ {"blocked sudo", "sudo rm -rf /", PolicyBlock},
+ {"auto approve", "df -h", PolicyAllow},
+ {"require approval", "systemctl restart nginx", PolicyRequireApproval},
+ {"unknown defaults to approval", "echo hello", PolicyRequireApproval},
+ {"sudo with flags remains conservative", "sudo -u root df -h", PolicyRequireApproval},
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := p.Evaluate(tc.command); got != tc.want {
+ t.Fatalf("Evaluate(%q) = %q, want %q", tc.command, got, tc.want)
+ }
+ })
+ }
+}
+
+func TestPolicyHelpers(t *testing.T) {
+ p := DefaultPolicy()
+ if !p.IsBlocked("rm -rf /") {
+ t.Fatalf("expected rm -rf / to be blocked")
+ }
+ if !p.NeedsApproval("echo hello") {
+ t.Fatalf("expected echo hello to require approval by default")
+ }
+ if !p.IsAutoApproved("df -h") {
+ t.Fatalf("expected df -h to be auto approved")
+ }
+}
diff --git a/internal/agentexec/server_test.go b/internal/agentexec/server_test.go
new file mode 100644
index 0000000000..17ceb5e5ee
--- /dev/null
+++ b/internal/agentexec/server_test.go
@@ -0,0 +1,50 @@
+package agentexec
+
+import (
+ "context"
+ "testing"
+ "time"
+)
+
+func TestExecuteCommandAgentNotConnected(t *testing.T) {
+ s := NewServer(nil)
+ _, err := s.ExecuteCommand(context.Background(), "missing", ExecuteCommandPayload{RequestID: "r1", Timeout: 1})
+ if err == nil {
+ t.Fatalf("expected error when agent not connected")
+ }
+}
+
+func TestReadFileAgentNotConnected(t *testing.T) {
+ s := NewServer(nil)
+ _, err := s.ReadFile(context.Background(), "missing", ReadFilePayload{RequestID: "r1"})
+ if err == nil {
+ t.Fatalf("expected error when agent not connected")
+ }
+}
+
+func TestConnectedAgentLookups(t *testing.T) {
+ s := NewServer(nil)
+ now := time.Now().Add(-1 * time.Minute)
+
+ s.mu.Lock()
+ s.agents["a1"] = &agentConn{agent: ConnectedAgent{AgentID: "a1", Hostname: "host1", ConnectedAt: now}}
+ s.agents["a2"] = &agentConn{agent: ConnectedAgent{AgentID: "a2", Hostname: "host2", ConnectedAt: now}}
+ s.mu.Unlock()
+
+ if !s.IsAgentConnected("a1") {
+ t.Fatalf("expected a1 to be connected")
+ }
+ if s.IsAgentConnected("missing") {
+ t.Fatalf("expected missing to not be connected")
+ }
+
+ agentID, ok := s.GetAgentForHost("host2")
+ if !ok || agentID != "a2" {
+ t.Fatalf("expected GetAgentForHost(host2) = (a2, true), got (%q, %v)", agentID, ok)
+ }
+
+ agents := s.GetConnectedAgents()
+ if len(agents) != 2 {
+ t.Fatalf("expected 2 connected agents, got %d", len(agents))
+ }
+}
diff --git a/internal/ceph/collector_test.go b/internal/ceph/collector_test.go
new file mode 100644
index 0000000000..9c9485e0e2
--- /dev/null
+++ b/internal/ceph/collector_test.go
@@ -0,0 +1,114 @@
+package ceph
+
+import (
+ "testing"
+)
+
+func TestParseStatus(t *testing.T) {
+ data := []byte(`{
+ "fsid":"fsid-123",
+ "health":{
+ "status":"HEALTH_WARN",
+ "checks":{
+ "OSD_DOWN":{
+ "severity":"HEALTH_WARN",
+ "summary":{"message":"1 osd down"},
+ "detail":[{"message":"osd.1 is down"}]
+ }
+ }
+ },
+ "monmap":{"epoch":7,"mons":[{"name":"a","rank":0,"addr":"10.0.0.1"}]},
+ "mgrmap":{"available":true,"active_name":"mgr-a","standbys":[{"name":"mgr-b"}]},
+ "osdmap":{"epoch":3,"num_osds":3,"num_up_osds":2,"num_in_osds":1},
+ "pgmap":{
+ "num_pgs":64,
+ "bytes_total":1000,
+ "bytes_used":250,
+ "bytes_avail":750,
+ "data_bytes":200,
+ "degraded_ratio":0.1,
+ "misplaced_ratio":0.2,
+ "read_bytes_sec":1,
+ "write_bytes_sec":2,
+ "read_op_per_sec":3,
+ "write_op_per_sec":4
+ }
+ }`)
+
+ status, err := parseStatus(data)
+ if err != nil {
+ t.Fatalf("parseStatus returned error: %v", err)
+ }
+
+ if status.FSID != "fsid-123" {
+ t.Fatalf("expected FSID fsid-123, got %q", status.FSID)
+ }
+ if status.Health.Status != "HEALTH_WARN" {
+ t.Fatalf("expected HEALTH_WARN, got %q", status.Health.Status)
+ }
+ check, ok := status.Health.Checks["OSD_DOWN"]
+ if !ok || check.Severity != "HEALTH_WARN" || check.Message != "1 osd down" || len(check.Detail) != 1 {
+ t.Fatalf("unexpected parsed health checks: %+v", status.Health.Checks)
+ }
+
+ if status.MonMap.NumMons != 1 || len(status.MonMap.Monitors) != 1 {
+ t.Fatalf("expected 1 monitor, got %+v", status.MonMap)
+ }
+ if status.OSDMap.NumOSDs != 3 || status.OSDMap.NumUp != 2 || status.OSDMap.NumIn != 1 {
+ t.Fatalf("unexpected OSD map: %+v", status.OSDMap)
+ }
+ if status.OSDMap.NumDown != 1 || status.OSDMap.NumOut != 2 {
+ t.Fatalf("expected computed down/out counts, got %+v", status.OSDMap)
+ }
+
+ if status.PGMap.UsagePercent != 25.0 {
+ t.Fatalf("expected usage percent 25.0, got %v", status.PGMap.UsagePercent)
+ }
+
+ if len(status.Services) != 3 {
+ t.Fatalf("expected 3 service summaries, got %d", len(status.Services))
+ }
+}
+
+func TestParseStatusInvalidJSON(t *testing.T) {
+ _, err := parseStatus([]byte(`{not-json}`))
+ if err == nil {
+ t.Fatalf("expected error for invalid JSON")
+ }
+}
+
+func TestParseDF(t *testing.T) {
+ data := []byte(`{
+ "stats":{"total_bytes":1000,"total_used_bytes":123,"percent_used":0.1234},
+ "pools":[
+ {"id":1,"name":"pool-a","stats":{"bytes_used":10,"max_avail":90,"objects":7,"percent_used":0.2}}
+ ]
+ }`)
+
+ pools, usagePercent, err := parseDF(data)
+ if err != nil {
+ t.Fatalf("parseDF returned error: %v", err)
+ }
+ if usagePercent != 12.34 {
+ t.Fatalf("expected percent_used 12.34, got %v", usagePercent)
+ }
+ if len(pools) != 1 || pools[0].Name != "pool-a" || pools[0].PercentUsed != 20.0 {
+ t.Fatalf("unexpected pools parsed: %+v", pools)
+ }
+}
+
+func TestParseDFInvalidJSON(t *testing.T) {
+ _, _, err := parseDF([]byte(`{not-json}`))
+ if err == nil {
+ t.Fatalf("expected error for invalid JSON")
+ }
+}
+
+func TestBoolToInt(t *testing.T) {
+ if boolToInt(true) != 1 {
+ t.Fatalf("expected boolToInt(true)=1")
+ }
+ if boolToInt(false) != 0 {
+ t.Fatalf("expected boolToInt(false)=0")
+ }
+}
diff --git a/internal/metrics/store_test.go b/internal/metrics/store_test.go
new file mode 100644
index 0000000000..4869442b31
--- /dev/null
+++ b/internal/metrics/store_test.go
@@ -0,0 +1,101 @@
+package metrics
+
+import (
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+func TestStoreWriteBatchAndQuery(t *testing.T) {
+ dir := t.TempDir()
+ cfg := DefaultConfig(dir)
+ cfg.DBPath = filepath.Join(dir, "metrics-test.db")
+ cfg.FlushInterval = time.Hour
+ cfg.RetentionRaw = 10 * time.Second
+ cfg.RetentionMinute = 20 * time.Second
+ cfg.RetentionHourly = 30 * time.Second
+ cfg.RetentionDaily = 40 * time.Second
+
+ store, err := NewStore(cfg)
+ if err != nil {
+ t.Fatalf("NewStore returned error: %v", err)
+ }
+ defer store.Close()
+
+ ts := time.Unix(1000, 0)
+ store.writeBatch([]bufferedMetric{
+ {resourceType: "vm", resourceID: "vm-101", metricType: "cpu", value: 1.5, timestamp: ts},
+ {resourceType: "vm", resourceID: "vm-101", metricType: "cpu", value: 2.5, timestamp: ts.Add(1 * time.Second)},
+ })
+
+ points, err := store.Query("vm", "vm-101", "cpu", ts.Add(-1*time.Second), ts.Add(2*time.Second))
+ if err != nil {
+ t.Fatalf("Query returned error: %v", err)
+ }
+ if len(points) != 2 {
+ t.Fatalf("expected 2 points, got %d", len(points))
+ }
+ if points[0].Value != 1.5 || points[1].Value != 2.5 {
+ t.Fatalf("unexpected query values: %+v", points)
+ }
+
+ all, err := store.QueryAll("vm", "vm-101", ts.Add(-1*time.Second), ts.Add(2*time.Second))
+ if err != nil {
+ t.Fatalf("QueryAll returned error: %v", err)
+ }
+ if len(all["cpu"]) != 2 {
+ t.Fatalf("expected QueryAll to return 2 cpu points, got %+v", all)
+ }
+}
+
+func TestStoreSelectTierAndStats(t *testing.T) {
+ dir := t.TempDir()
+ cfg := DefaultConfig(dir)
+ cfg.DBPath = filepath.Join(dir, "metrics-test.db")
+ cfg.FlushInterval = time.Hour
+ cfg.RetentionRaw = 10 * time.Second
+ cfg.RetentionMinute = 20 * time.Second
+ cfg.RetentionHourly = 30 * time.Second
+ cfg.RetentionDaily = 40 * time.Second
+
+ store, err := NewStore(cfg)
+ if err != nil {
+ t.Fatalf("NewStore returned error: %v", err)
+ }
+ defer store.Close()
+
+ if store.selectTier(5*time.Second) != TierRaw {
+ t.Fatalf("expected raw tier")
+ }
+ if store.selectTier(15*time.Second) != TierMinute {
+ t.Fatalf("expected minute tier")
+ }
+ if store.selectTier(25*time.Second) != TierHourly {
+ t.Fatalf("expected hourly tier")
+ }
+ if store.selectTier(35*time.Second) != TierDaily {
+ t.Fatalf("expected daily tier")
+ }
+
+ // Insert one point for each tier to verify stats aggregation.
+ ts := int64(1000)
+ _, err = store.db.Exec(
+ `INSERT INTO metrics (resource_type, resource_id, metric_type, value, timestamp, tier) VALUES
+ ('vm','vm-101','cpu',1.0,?, 'raw'),
+ ('vm','vm-101','cpu',2.0,?, 'minute'),
+ ('vm','vm-101','cpu',3.0,?, 'hourly'),
+ ('vm','vm-101','cpu',4.0,?, 'daily')`,
+ ts, ts, ts, ts,
+ )
+ if err != nil {
+ t.Fatalf("insert metrics returned error: %v", err)
+ }
+
+ stats := store.GetStats()
+ if stats.RawCount != 1 || stats.MinuteCount != 1 || stats.HourlyCount != 1 || stats.DailyCount != 1 {
+ t.Fatalf("unexpected tier counts: %+v", stats)
+ }
+ if stats.DBPath == "" || stats.DBSize <= 0 {
+ t.Fatalf("expected stats DB info to be populated: %+v", stats)
+ }
+}