feat: add buildToggleCommand for safe toggle command construction

Replaces inline ternary in QML with a pure function that returns
the correct tailscale up/down command based on connection state.
This commit is contained in:
Vybe (Coding Agent) 2026-05-20 19:21:17 +00:00
parent dddd9218f6
commit ce8707c618
2 changed files with 21 additions and 2 deletions

View file

@ -38,6 +38,10 @@ function buildCopyCommand(text, clipboardCmd) {
return ["sh", "-c", "printf '%s' '" + escaped + "' | " + clipboardCmd]
}
function buildToggleCommand(isConnected) {
return isConnected ? ["tailscale", "down"] : ["tailscale", "up"]
}
function parseClipboardDetection(stdout) {
var trimmed = typeof stdout === "string" ? stdout.trim() : ""
return validateClipboardCmd(trimmed) ? trimmed : "none"
@ -57,5 +61,5 @@ function errorMessage(cmd) {
// CommonJS export for Node.js tests (ignored by QML)
if (typeof module !== "undefined" && module.exports) {
module.exports = { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection }
module.exports = { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection, buildToggleCommand }
}

View file

@ -1,7 +1,7 @@
import { test } from "node:test"
import assert from "node:assert"
import lib from "../tailscalectl/lib.js"
const { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection } = lib
const { parsePeers, makeExitNodeCommand, findActiveExitNode, errorMessage, validateClipboardCmd, buildCopyCommand, parseClipboardDetection, buildToggleCommand } = lib
test("parsePeers extracts exitNode from ExitNodeOption", () => {
const peerMap = {
@ -143,3 +143,18 @@ test("parseClipboardDetection falls back to none for unexpected strings", () =>
assert.strictEqual(parseClipboardDetection("xclip -selection clipboard"), "none")
assert.strictEqual(parseClipboardDetection("custom-tool"), "none")
})
// --- buildToggleCommand ---
test("buildToggleCommand returns down command when connected", () => {
assert.deepStrictEqual(buildToggleCommand(true), ["tailscale", "down"])
})
test("buildToggleCommand returns up command when disconnected", () => {
assert.deepStrictEqual(buildToggleCommand(false), ["tailscale", "up"])
})
test("buildToggleCommand treats null and undefined as disconnected", () => {
assert.deepStrictEqual(buildToggleCommand(null), ["tailscale", "up"])
assert.deepStrictEqual(buildToggleCommand(undefined), ["tailscale", "up"])
})