|
| 1 | +import { AppPlatform, BuildStatus } from "../../graphql/generated"; |
| 2 | +import { BuildQuery } from "../../graphql/queries/BuildQuery"; |
| 3 | +import Log from "../../log"; |
| 4 | +import fetch from "../../fetch"; |
| 5 | +import { |
| 6 | + parseBuildLogLines, |
| 7 | + streamBuildLogsAsync, |
| 8 | + streamBuildsLogsAsync, |
| 9 | +} from "../logs"; |
| 10 | + |
| 11 | +jest.mock("../../graphql/queries/BuildQuery"); |
| 12 | +jest.mock("../../fetch"); |
| 13 | +jest.mock("../../log"); |
| 14 | +jest.mock("../../ora", () => ({ |
| 15 | + ora: () => ({ |
| 16 | + start(text?: string) { |
| 17 | + return { |
| 18 | + text, |
| 19 | + succeed: jest.fn(), |
| 20 | + fail: jest.fn(), |
| 21 | + warn: jest.fn(), |
| 22 | + }; |
| 23 | + }, |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
| 27 | +describe("build log streaming", () => { |
| 28 | + afterEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("parses only valid JSON log lines", () => { |
| 33 | + expect( |
| 34 | + parseBuildLogLines('{"msg":"first"}\nnot-json\n{"msg":"second"}\n'), |
| 35 | + ).toEqual([{ msg: "first" }, { msg: "second" }]); |
| 36 | + }); |
| 37 | + |
| 38 | + it("streams only newly appended log lines across rotated log file urls", async () => { |
| 39 | + const inProgressBuild = buildFragment({ |
| 40 | + status: BuildStatus.InProgress, |
| 41 | + logFiles: ["https://example.com/logs/build.txt?token=first"], |
| 42 | + }); |
| 43 | + const finishedBuild = buildFragment({ |
| 44 | + status: BuildStatus.Finished, |
| 45 | + logFiles: ["https://example.com/logs/build.txt?token=second"], |
| 46 | + }); |
| 47 | + |
| 48 | + jest.mocked(fetch).mockResolvedValueOnce({ |
| 49 | + text: async () => |
| 50 | + [ |
| 51 | + JSON.stringify({ marker: "START_PHASE", phase: "PREBUILD" }), |
| 52 | + JSON.stringify({ phase: "PREBUILD", msg: "first line" }), |
| 53 | + ].join("\n"), |
| 54 | + } as any); |
| 55 | + jest.mocked(fetch).mockResolvedValueOnce({ |
| 56 | + text: async () => |
| 57 | + [ |
| 58 | + JSON.stringify({ marker: "START_PHASE", phase: "PREBUILD" }), |
| 59 | + JSON.stringify({ phase: "PREBUILD", msg: "first line" }), |
| 60 | + JSON.stringify({ phase: "PREBUILD", msg: "second line" }), |
| 61 | + ].join("\n"), |
| 62 | + } as any); |
| 63 | + jest |
| 64 | + .mocked(BuildQuery.byIdAsync) |
| 65 | + .mockResolvedValueOnce(finishedBuild as any); |
| 66 | + |
| 67 | + const finalBuild = await streamBuildLogsAsync( |
| 68 | + {} as any, |
| 69 | + inProgressBuild as any, |
| 70 | + { |
| 71 | + pollIntervalMs: 0, |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + expect(finalBuild).toBe(finishedBuild); |
| 76 | + expect(fetch).toHaveBeenNthCalledWith(1, inProgressBuild.logFiles[0], { |
| 77 | + method: "GET", |
| 78 | + }); |
| 79 | + expect(fetch).toHaveBeenNthCalledWith(2, finishedBuild.logFiles[0], { |
| 80 | + method: "GET", |
| 81 | + }); |
| 82 | + expect(Log.log).toHaveBeenCalledWith(expect.stringContaining("Prebuild")); |
| 83 | + expect(Log.log).toHaveBeenCalledWith(" first line"); |
| 84 | + expect(Log.log).toHaveBeenCalledWith(" second line"); |
| 85 | + expect( |
| 86 | + jest |
| 87 | + .mocked(Log.log) |
| 88 | + .mock.calls.filter(([message]) => message === " first line"), |
| 89 | + ).toHaveLength(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it("streams multiple builds with platform labels", async () => { |
| 93 | + const androidBuild = buildFragment({ |
| 94 | + id: "android-build", |
| 95 | + platform: AppPlatform.Android, |
| 96 | + status: BuildStatus.Finished, |
| 97 | + logFiles: ["https://example.com/logs/android.txt?token=1"], |
| 98 | + }); |
| 99 | + const iosBuild = buildFragment({ |
| 100 | + id: "ios-build", |
| 101 | + platform: AppPlatform.Ios, |
| 102 | + status: BuildStatus.Finished, |
| 103 | + logFiles: ["https://example.com/logs/ios.txt?token=1"], |
| 104 | + }); |
| 105 | + |
| 106 | + jest |
| 107 | + .mocked(fetch) |
| 108 | + .mockResolvedValueOnce({ |
| 109 | + text: async () => |
| 110 | + JSON.stringify({ phase: "PREBUILD", msg: "android line" }), |
| 111 | + } as any) |
| 112 | + .mockResolvedValueOnce({ |
| 113 | + text: async () => |
| 114 | + JSON.stringify({ phase: "PREBUILD", msg: "ios line" }), |
| 115 | + } as any); |
| 116 | + |
| 117 | + const builds = await streamBuildsLogsAsync( |
| 118 | + {} as any, |
| 119 | + [androidBuild as any, iosBuild as any], |
| 120 | + { |
| 121 | + pollIntervalMs: 0, |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + expect(builds).toEqual([androidBuild, iosBuild]); |
| 126 | + expect(Log.log).toHaveBeenCalledWith(expect.stringContaining("[Android]")); |
| 127 | + expect(Log.log).toHaveBeenCalledWith(expect.stringContaining("[iOS]")); |
| 128 | + expect(Log.log).toHaveBeenCalledWith( |
| 129 | + expect.stringContaining("android line"), |
| 130 | + ); |
| 131 | + expect(Log.log).toHaveBeenCalledWith(expect.stringContaining("ios line")); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +function buildFragment( |
| 136 | + overrides: Partial<{ |
| 137 | + id: string; |
| 138 | + platform: AppPlatform; |
| 139 | + status: BuildStatus; |
| 140 | + logFiles: string[]; |
| 141 | + }>, |
| 142 | +) { |
| 143 | + return { |
| 144 | + id: "build-id", |
| 145 | + platform: AppPlatform.Android, |
| 146 | + status: BuildStatus.InProgress, |
| 147 | + logFiles: [], |
| 148 | + ...overrides, |
| 149 | + }; |
| 150 | +} |
0 commit comments