|
| 1 | +import type { Compose, ComposeSpecification } from "@dokploy/server"; |
| 2 | +import { |
| 3 | + addAppNameToPreventCollision, |
| 4 | + addSuffixToAllProperties, |
| 5 | +} from "@dokploy/server"; |
| 6 | +import { addDomainToCompose } from "@dokploy/server/utils/docker/domain"; |
| 7 | +import { execAsyncRemote } from "@dokploy/server/utils/process/execAsync"; |
| 8 | +import { expect, test, vi } from "vitest"; |
| 9 | +import { parse, stringify } from "yaml"; |
| 10 | + |
| 11 | +vi.mock("@dokploy/server/utils/process/execAsync", async (importOriginal) => ({ |
| 12 | + ...(await importOriginal< |
| 13 | + typeof import("@dokploy/server/utils/process/execAsync") |
| 14 | + >()), |
| 15 | + execAsyncRemote: vi.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const modelsComposeFile = ` |
| 19 | +services: |
| 20 | + chat: |
| 21 | + image: my-chat-app |
| 22 | + models: |
| 23 | + - llm |
| 24 | + worker: |
| 25 | + image: my-worker |
| 26 | + models: |
| 27 | + embed: |
| 28 | + endpoint_var: EMBED_URL |
| 29 | + model_var: EMBED_MODEL |
| 30 | +models: |
| 31 | + llm: |
| 32 | + model: ai/smollm2 |
| 33 | + context_size: 2048 |
| 34 | + runtime_flags: |
| 35 | + - "--verbose" |
| 36 | + embed: |
| 37 | + model: ai/all-minilm |
| 38 | + name: embeddings |
| 39 | +`; |
| 40 | + |
| 41 | +const expectedModels = { |
| 42 | + llm: { |
| 43 | + model: "ai/smollm2", |
| 44 | + context_size: 2048, |
| 45 | + runtime_flags: ["--verbose"], |
| 46 | + }, |
| 47 | + embed: { |
| 48 | + model: "ai/all-minilm", |
| 49 | + name: "embeddings", |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +const expectedChatModels = ["llm"]; |
| 54 | +const expectedWorkerModels = { |
| 55 | + embed: { |
| 56 | + endpoint_var: "EMBED_URL", |
| 57 | + model_var: "EMBED_MODEL", |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +const assertModelsPreserved = ( |
| 62 | + spec: ComposeSpecification | null, |
| 63 | + serviceNames: { chat: string; worker: string } = { |
| 64 | + chat: "chat", |
| 65 | + worker: "worker", |
| 66 | + }, |
| 67 | +) => { |
| 68 | + expect(spec?.models).toEqual(expectedModels); |
| 69 | + expect(spec?.services?.[serviceNames.chat]?.models).toEqual( |
| 70 | + expectedChatModels, |
| 71 | + ); |
| 72 | + expect(spec?.services?.[serviceNames.worker]?.models).toEqual( |
| 73 | + expectedWorkerModels, |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const rawCompose = (overrides?: Record<string, unknown>) => |
| 78 | + ({ |
| 79 | + appName: "chat-app", |
| 80 | + composeFile: modelsComposeFile, |
| 81 | + composePath: "./docker-compose.yml", |
| 82 | + composeType: "docker-compose", |
| 83 | + isolatedDeployment: false, |
| 84 | + isolatedDeploymentsVolume: false, |
| 85 | + randomize: false, |
| 86 | + serverId: null, |
| 87 | + sourceType: "raw", |
| 88 | + suffix: "", |
| 89 | + ...overrides, |
| 90 | + }) as unknown as Compose; |
| 91 | + |
| 92 | +test("compose without models is unchanged besides existing suffix behavior", () => { |
| 93 | + const composeData = parse(` |
| 94 | +services: |
| 95 | + web: |
| 96 | + image: nginx:latest |
| 97 | + volumes: |
| 98 | + - web_data:/data |
| 99 | +volumes: |
| 100 | + web_data: |
| 101 | +`) as ComposeSpecification; |
| 102 | + const updated = addSuffixToAllProperties(composeData, "testhash"); |
| 103 | + |
| 104 | + expect(updated.models).toBeUndefined(); |
| 105 | + expect(updated.services).toEqual({ |
| 106 | + "web-testhash": { |
| 107 | + image: "nginx:latest", |
| 108 | + volumes: ["web_data-testhash:/data"], |
| 109 | + }, |
| 110 | + }); |
| 111 | + expect(updated.volumes).toEqual({ |
| 112 | + "web_data-testhash": null, |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +test("suffixing does not rename model identifiers or drop model config", () => { |
| 117 | + const updated = addSuffixToAllProperties( |
| 118 | + parse(modelsComposeFile) as ComposeSpecification, |
| 119 | + "testhash", |
| 120 | + ); |
| 121 | + |
| 122 | + assertModelsPreserved(updated, { |
| 123 | + chat: "chat-testhash", |
| 124 | + worker: "worker-testhash", |
| 125 | + }); |
| 126 | + expect(updated.services).not.toHaveProperty("chat"); |
| 127 | + expect(updated.models).not.toHaveProperty("llm-testhash"); |
| 128 | +}); |
| 129 | + |
| 130 | +test("isolated deployment preserves model identifiers", () => { |
| 131 | + assertModelsPreserved( |
| 132 | + addAppNameToPreventCollision( |
| 133 | + parse(modelsComposeFile) as ComposeSpecification, |
| 134 | + "chat-app", |
| 135 | + false, |
| 136 | + ), |
| 137 | + ); |
| 138 | +}); |
| 139 | + |
| 140 | +test("raw remote compose conversion preserves models", async () => { |
| 141 | + vi.mocked(execAsyncRemote).mockResolvedValue({ |
| 142 | + stdout: "services:\n dropped:\n image: alpine:latest\n", |
| 143 | + stderr: "", |
| 144 | + }); |
| 145 | + |
| 146 | + const converted = await addDomainToCompose( |
| 147 | + rawCompose({ serverId: "remote-server" }), |
| 148 | + [], |
| 149 | + ); |
| 150 | + |
| 151 | + assertModelsPreserved(converted); |
| 152 | + expect(execAsyncRemote).not.toHaveBeenCalled(); |
| 153 | + |
| 154 | + const written = parse( |
| 155 | + stringify(converted, { lineWidth: 1000 }), |
| 156 | + ) as ComposeSpecification; |
| 157 | + assertModelsPreserved(written); |
| 158 | +}); |
| 159 | + |
| 160 | +test("invalid YAML still fails at parse", async () => { |
| 161 | + await expect( |
| 162 | + addDomainToCompose(rawCompose({ composeFile: "services: [" }), []), |
| 163 | + ).rejects.toThrow(); |
| 164 | +}); |
0 commit comments