CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | /**
* Driver-level tests for src/lib/server-targets.ts.
*
* We never actually shell out — `__setSpawnForTests` lets us inspect the
* command line the driver builds and feed back canned exit codes. Covers:
* - testConnection happy path (returns SHA256 fingerprint)
* - deployToTarget happy path (env file is materialised + sourced)
* - host-key fingerprint mismatch aborts the deploy
* - missing SERVER_TARGETS_KEY collapses to ok:false (no throw)
*/
import { describe, it, expect, afterEach, beforeEach } from "bun:test";
import {
__setSpawnForTests,
deployToTarget,
testConnection,
type SpawnResult,
} from "../lib/server-targets";
import { encryptValue } from "../lib/server-targets-crypto";
import type { ServerTarget } from "../db/schema";
const TEST_KEY =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
const HOST_FP = "SHA256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
const originalKey = process.env.SERVER_TARGETS_KEY;
beforeEach(() => {
process.env.SERVER_TARGETS_KEY = TEST_KEY;
});
afterEach(() => {
__setSpawnForTests(null);
if (originalKey === undefined) delete process.env.SERVER_TARGETS_KEY;
else process.env.SERVER_TARGETS_KEY = originalKey;
});
function buildTarget(overrides: Partial<ServerTarget> = {}): ServerTarget {
const enc = encryptValue("-----BEGIN OPENSSH PRIVATE KEY-----\nfake\n-----END OPENSSH PRIVATE KEY-----\n");
if (!enc.ok) throw new Error("encrypt failed in test setup: " + enc.error);
return {
id: "00000000-0000-0000-0000-000000000001",
name: "test-box",
host: "192.0.2.10",
port: 22,
sshUser: "deploy",
encryptedPrivateKey: enc.ciphertext,
hostFingerprint: null,
deployPath: "/var/www/app",
deployScript: "echo deployed",
watchedRepositoryId: null,
watchedBranch: null,
status: "unverified",
lastSeenAt: null,
createdBy: null,
createdAt: new Date(),
updatedAt: new Date(),
...overrides,
} as ServerTarget;
}
describe("testConnection", () => {
it("returns the fingerprint and ok=true on a clean run", async () => {
const calls: Array<string[]> = [];
__setSpawnForTests(async (cmd) => {
calls.push(cmd);
const [bin] = cmd;
if (bin === "ssh-keyscan") {
return ok("192.0.2.10 ssh-ed25519 AAAA...\n");
}
if (bin === "ssh-keygen") {
return ok(`256 ${HOST_FP} root@box (ED25519)\n`);
}
if (bin === "ssh") {
return ok("gluecron-ok\n");
}
return fail("unexpected cmd: " + cmd.join(" "));
});
const result = await testConnection(buildTarget());
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.fingerprint).toBe(HOST_FP);
expect(calls.length).toBe(3);
expect(calls[0][0]).toBe("ssh-keyscan");
expect(calls[2][0]).toBe("ssh");
});
it("returns ok=false at the scan stage when ssh-keyscan fails", async () => {
__setSpawnForTests(async (cmd) => {
if (cmd[0] === "ssh-keyscan") return fail("Connection refused");
return ok("");
});
const result = await testConnection(buildTarget());
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.stage).toBe("scan");
});
it("returns ok=false at auth stage when ssh probe fails", async () => {
__setSpawnForTests(async (cmd) => {
if (cmd[0] === "ssh-keyscan") return ok("192.0.2.10 ssh-ed25519 AAAA\n");
if (cmd[0] === "ssh-keygen") return ok(`256 ${HOST_FP} (ED25519)\n`);
if (cmd[0] === "ssh") return fail("Permission denied (publickey)");
return fail("?");
});
const result = await testConnection(buildTarget());
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.stage).toBe("auth");
});
});
describe("deployToTarget", () => {
it("scp's the env file then ssh-runs the deploy script", async () => {
const calls: Array<string[]> = [];
__setSpawnForTests(async (cmd) => {
calls.push(cmd);
if (cmd[0] === "scp") return ok("");
if (cmd[0] === "ssh") return ok("done\n");
return fail("?");
});
const result = await deployToTarget(buildTarget(), {
env: { FOO: "1", BAR: "two" },
commitSha: "abc1234",
ref: "refs/heads/main",
});
expect(result.ok).toBe(true);
expect(result.exitCode).toBe(0);
// Two calls: scp, then ssh.
expect(calls.length).toBe(2);
expect(calls[0][0]).toBe("scp");
// scp target ends at deploy_path/.env.gluecron
const scpDst = calls[0][calls[0].length - 1];
expect(scpDst).toBe("deploy@192.0.2.10:/var/www/app/.env.gluecron");
expect(calls[1][0]).toBe("ssh");
const remoteCmd = calls[1][calls[1].length - 1];
expect(remoteCmd).toContain("cd '/var/www/app'");
expect(remoteCmd).toContain(". ./.env.gluecron");
expect(remoteCmd).toContain("export GLUECRON_COMMIT_SHA='abc1234'");
expect(remoteCmd).toContain("echo deployed");
});
it("aborts deploy when pinned fingerprint doesn't match live", async () => {
__setSpawnForTests(async (cmd) => {
if (cmd[0] === "ssh-keyscan") return ok("192.0.2.10 ssh-ed25519 AAAA\n");
if (cmd[0] === "ssh-keygen") return ok("256 SHA256:DIFFERENT (ED25519)\n");
return fail("should not get here");
});
const result = await deployToTarget(
buildTarget({ hostFingerprint: HOST_FP }),
{ env: {} }
);
expect(result.ok).toBe(false);
expect(result.stderr).toContain("fingerprint mismatch");
});
it("collapses to ok=false when SERVER_TARGETS_KEY is missing", async () => {
const target = buildTarget(); // build while key is present so encrypt works
delete process.env.SERVER_TARGETS_KEY; // now drop it — decrypt must fail cleanly
const result = await deployToTarget(target, { env: {} });
expect(result.ok).toBe(false);
expect(result.exitCode).toBe(-1);
});
});
// ─── helpers ────────────────────────────────────────────────────────────────
function ok(stdout: string): SpawnResult {
return { exitCode: 0, stdout, stderr: "" };
}
function fail(stderr: string): SpawnResult {
return { exitCode: 1, stdout: "", stderr };
}
|