|
| 1 | +// @ts-ignore |
| 2 | +import {CliPlatformTest} from "@tsed/cli-testing"; |
| 3 | + |
| 4 | +import {CliDockerComposeYaml} from "./CliDockerComposeYaml.js"; |
| 5 | +import {CliFs} from "./CliFs.js"; |
| 6 | +import {CliYaml} from "./CliYaml.js"; |
| 7 | +import {ProjectPackageJson} from "./ProjectPackageJson.js"; |
| 8 | + |
| 9 | +describe("CliDockerComposeYaml", () => { |
| 10 | + beforeEach(() => CliPlatformTest.create()); |
| 11 | + afterEach(() => CliPlatformTest.reset()); |
| 12 | + |
| 13 | + async function createService(deps: Partial<Record<string, any>> = {}) { |
| 14 | + const cliYaml = deps.cliYaml || { |
| 15 | + read: vi.fn().mockResolvedValue({}), |
| 16 | + write: vi.fn().mockResolvedValue(undefined) |
| 17 | + }; |
| 18 | + const fs = deps.fs || { |
| 19 | + exists: vi.fn().mockReturnValue(true), |
| 20 | + findUpFile: vi.fn().mockReturnValue(undefined) |
| 21 | + }; |
| 22 | + const projectPkg = deps.projectPkg || ({dir: "/project"} as ProjectPackageJson); |
| 23 | + |
| 24 | + const service = await CliPlatformTest.invoke<CliDockerComposeYaml>(CliDockerComposeYaml, [ |
| 25 | + { |
| 26 | + token: CliYaml, |
| 27 | + use: cliYaml |
| 28 | + }, |
| 29 | + { |
| 30 | + token: CliFs, |
| 31 | + use: fs |
| 32 | + }, |
| 33 | + { |
| 34 | + token: ProjectPackageJson, |
| 35 | + use: projectPkg |
| 36 | + } |
| 37 | + ]); |
| 38 | + |
| 39 | + return {service, cliYaml, fs, projectPkg}; |
| 40 | + } |
| 41 | + |
| 42 | + describe("read()", () => { |
| 43 | + it("should read docker-compose.yml from project root when it exists", async () => { |
| 44 | + const {service, cliYaml, fs} = await createService(); |
| 45 | + |
| 46 | + const result = await service.read(); |
| 47 | + |
| 48 | + expect(fs.exists).toHaveBeenCalledWith("docker-compose.yml"); |
| 49 | + expect(cliYaml.read).toHaveBeenCalledWith("docker-compose.yml"); |
| 50 | + expect(result).toEqual({}); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return an empty object when no docker-compose file is found", async () => { |
| 54 | + const fs = { |
| 55 | + exists: vi.fn().mockReturnValue(false), |
| 56 | + findUpFile: vi.fn().mockReturnValue(undefined) |
| 57 | + }; |
| 58 | + const {service, cliYaml} = await createService({fs}); |
| 59 | + |
| 60 | + const result = await service.read(); |
| 61 | + |
| 62 | + expect(fs.findUpFile).toHaveBeenCalled(); |
| 63 | + expect(cliYaml.read).not.toHaveBeenCalled(); |
| 64 | + expect(result).toEqual({}); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("write()", () => { |
| 69 | + it("should write to the discovered docker-compose file", async () => { |
| 70 | + const cliYaml = { |
| 71 | + read: vi.fn(), |
| 72 | + write: vi.fn().mockResolvedValue(undefined) |
| 73 | + }; |
| 74 | + const fs = { |
| 75 | + exists: vi.fn(), |
| 76 | + findUpFile: vi.fn().mockReturnValue("/repo/docker-compose.yml") |
| 77 | + }; |
| 78 | + const {service} = await createService({cliYaml, fs}); |
| 79 | + const payload = {services: {}}; |
| 80 | + |
| 81 | + await service.write(payload); |
| 82 | + |
| 83 | + expect(cliYaml.write).toHaveBeenCalledWith("/repo/docker-compose.yml", payload); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should fallback to project dir when docker-compose file does not exist yet", async () => { |
| 87 | + const cliYaml = { |
| 88 | + read: vi.fn(), |
| 89 | + write: vi.fn().mockResolvedValue(undefined) |
| 90 | + }; |
| 91 | + const fs = { |
| 92 | + exists: vi.fn(), |
| 93 | + findUpFile: vi.fn().mockReturnValue(undefined) |
| 94 | + }; |
| 95 | + const projectPkg = {dir: "/repo"} as ProjectPackageJson; |
| 96 | + const {service} = await createService({cliYaml, fs, projectPkg}); |
| 97 | + const payload = {services: {}}; |
| 98 | + |
| 99 | + await service.write(payload); |
| 100 | + |
| 101 | + expect(cliYaml.write).toHaveBeenCalledWith("/repo/docker-compose.yml", payload); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe("addDatabaseService()", () => { |
| 106 | + it("should append a postgres service and persist the file", async () => { |
| 107 | + const cliYaml = { |
| 108 | + read: vi.fn().mockResolvedValue({services: {}}), |
| 109 | + write: vi.fn().mockResolvedValue(undefined) |
| 110 | + }; |
| 111 | + const fs = { |
| 112 | + exists: vi.fn().mockReturnValue(true), |
| 113 | + findUpFile: vi.fn() |
| 114 | + }; |
| 115 | + const {service} = await createService({cliYaml, fs}); |
| 116 | + |
| 117 | + await service.addDatabaseService("OrdersDb", "postgres"); |
| 118 | + |
| 119 | + expect(cliYaml.write).toHaveBeenCalledTimes(1); |
| 120 | + const [, dockerCompose] = cliYaml.write.mock.calls[0]; |
| 121 | + expect(dockerCompose).toEqual({ |
| 122 | + services: { |
| 123 | + orders_db: { |
| 124 | + image: "postgres:9.6.1", |
| 125 | + ports: ["5432:5432"], |
| 126 | + volumes: ["./pgdata:/var/lib/postgresql/data"], |
| 127 | + environment: { |
| 128 | + POSTGRES_USER: "test", |
| 129 | + POSTGRES_PASSWORD: "test", |
| 130 | + POSTGRES_DB: "test" |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should append a mongodb service definition when requested", async () => { |
| 138 | + const cliYaml = { |
| 139 | + read: vi.fn().mockResolvedValue({services: {}}), |
| 140 | + write: vi.fn().mockResolvedValue(undefined) |
| 141 | + }; |
| 142 | + const fs = { |
| 143 | + exists: vi.fn().mockReturnValue(true), |
| 144 | + findUpFile: vi.fn() |
| 145 | + }; |
| 146 | + const {service} = await createService({cliYaml, fs}); |
| 147 | + |
| 148 | + await service.addDatabaseService("Analytics", "mongodb"); |
| 149 | + |
| 150 | + expect(cliYaml.write).toHaveBeenCalledTimes(1); |
| 151 | + const [, dockerCompose] = cliYaml.write.mock.calls[0]; |
| 152 | + expect(dockerCompose).toEqual({ |
| 153 | + services: { |
| 154 | + analytics: { |
| 155 | + image: "mongo:5.0.8", |
| 156 | + ports: ["27017:27017"] |
| 157 | + } |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments