|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + isIncompatibleRuntimeConfig, |
| 4 | + createRuntime, |
| 5 | + IncompatibleRuntimeError, |
| 6 | +} from "./runtimeFactory"; |
| 7 | +import type { RuntimeConfig } from "@/common/types/runtime"; |
| 8 | + |
| 9 | +describe("isIncompatibleRuntimeConfig", () => { |
| 10 | + it("returns false for undefined config", () => { |
| 11 | + expect(isIncompatibleRuntimeConfig(undefined)).toBe(false); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns false for valid local config with srcBaseDir", () => { |
| 15 | + const config: RuntimeConfig = { |
| 16 | + type: "local", |
| 17 | + srcBaseDir: "~/.mux/src", |
| 18 | + }; |
| 19 | + expect(isIncompatibleRuntimeConfig(config)).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns false for valid SSH config", () => { |
| 23 | + const config: RuntimeConfig = { |
| 24 | + type: "ssh", |
| 25 | + host: "example.com", |
| 26 | + srcBaseDir: "/home/user/mux", |
| 27 | + }; |
| 28 | + expect(isIncompatibleRuntimeConfig(config)).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns true for local config without srcBaseDir (future project-dir mode)", () => { |
| 32 | + // Simulate a config from a future version that has type: "local" without srcBaseDir |
| 33 | + // This bypasses TypeScript checks to simulate runtime data from newer versions |
| 34 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 35 | + const config = { type: "local" } as RuntimeConfig; |
| 36 | + expect(isIncompatibleRuntimeConfig(config)).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns true for local config with empty srcBaseDir", () => { |
| 40 | + // Simulate a malformed config - empty srcBaseDir shouldn't be valid |
| 41 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 42 | + const config = { type: "local", srcBaseDir: "" } as RuntimeConfig; |
| 43 | + expect(isIncompatibleRuntimeConfig(config)).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it("returns true for unknown runtime type (future types like worktree)", () => { |
| 47 | + // Simulate a config from a future version with new type |
| 48 | + |
| 49 | + const config = { type: "worktree", srcBaseDir: "~/.mux/src" } as unknown as RuntimeConfig; |
| 50 | + expect(isIncompatibleRuntimeConfig(config)).toBe(true); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("createRuntime", () => { |
| 55 | + it("creates LocalRuntime for valid local config", () => { |
| 56 | + const config: RuntimeConfig = { |
| 57 | + type: "local", |
| 58 | + srcBaseDir: "/tmp/test-src", |
| 59 | + }; |
| 60 | + const runtime = createRuntime(config); |
| 61 | + expect(runtime).toBeDefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("throws IncompatibleRuntimeError for local config without srcBaseDir", () => { |
| 65 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 66 | + const config = { type: "local" } as RuntimeConfig; |
| 67 | + expect(() => createRuntime(config)).toThrow(IncompatibleRuntimeError); |
| 68 | + expect(() => createRuntime(config)).toThrow(/newer version of mux/); |
| 69 | + }); |
| 70 | + |
| 71 | + it("throws IncompatibleRuntimeError for unknown runtime type", () => { |
| 72 | + const config = { type: "worktree", srcBaseDir: "~/.mux/src" } as unknown as RuntimeConfig; |
| 73 | + expect(() => createRuntime(config)).toThrow(IncompatibleRuntimeError); |
| 74 | + expect(() => createRuntime(config)).toThrow(/newer version of mux/); |
| 75 | + }); |
| 76 | +}); |
0 commit comments