import { describe, expect, test } from "bun:test"; import type { V1PodTemplateSpec, V1ReplicaSet } from "@kubernetes/client-node"; import { stripPodTemplateHash, templateMatches } from "../../lib/rollback"; const template = (labels: Record = {}): V1PodTemplateSpec => ({ metadata: { name: "pod", labels }, spec: { containers: [ { name: "app", image: "registry/kuber/demo:latest@sha256:aaaa" }, ], }, }); describe("rollback pod template handling", () => { test("strips the generated pod-template-hash label", () => { const result = stripPodTemplateHash( template({ app: "web", "pod-template-hash": "abc123" }), ); expect(result.metadata?.labels).toEqual({ app: "web" }); }); test("clears labels entirely when only the hash is present", () => { const result = stripPodTemplateHash( template({ "pod-template-hash": "abc123" }), ); expect(result.metadata?.labels).toBeUndefined(); }); test("returns a shallow copy without mutating the input", () => { const input = template({ app: "web" }); const result = stripPodTemplateHash(input); expect(result).not.toBe(input); expect(input.metadata?.labels).toEqual({ app: "web" }); }); test("treats templates as equal when they differ only by hash", () => { expect( templateMatches( template({ app: "web", "pod-template-hash": "one" }), template({ app: "web", "pod-template-hash": "two" }), ), ).toBe(true); }); test("treats templates as different when other fields differ", () => { const a = template({ app: "web" }); const b = template({ app: "worker" }); expect(templateMatches(a, b)).toBe(false); }); test("matches undefined templates", () => { expect(templateMatches(undefined, undefined)).toBe(true); expect(templateMatches(template(), undefined)).toBe(false); }); });