Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { execFileSync } from "node:child_process";
import prompts from "prompts";
import { deploy } from "./deploy.ts";
import type { DeployConfig } from "./config.ts";
import type { SectionLibraryRevision } from "./sectionLibraryApi.ts";

vi.mock("prompts");

Expand All @@ -17,10 +18,20 @@ const config: DeployConfig = {
partition: "US",
apiHost: "https://sbx-api.yextapis.com",
};
const successfulResponse = JSON.stringify({
meta: { errors: [] },
response: {},
});
const sectionLibrary = {
name: "accounts/123/sectionLibraries/library-123",
uid: "019c9cb4-a7ad-7312-a603-9a828ac7c103",
displayName: "Library",
description: "Test",
};
const sectionLibraryRevision = {
name: `${sectionLibrary.name}/revisions/019c9cb5-0bc5-76cd-848c-25657b850a8f`,
status: "STATUS_BUILD_PROCESSING",
} satisfies SectionLibraryRevision;

function successfulResponse(response: object): string {
return JSON.stringify({ meta: { errors: [] }, response });
}

let rootDir: string;
let sourceCommitHash: string;
Expand Down Expand Up @@ -73,11 +84,19 @@ describe("deploy", () => {
it("creates a section library revision with Git source metadata", async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response(successfulResponse, { status: 200 }))
.mockResolvedValueOnce(new Response(successfulResponse, { status: 201 }));
.mockResolvedValueOnce(
new Response(successfulResponse(sectionLibrary), { status: 200 })
)
.mockResolvedValueOnce(
new Response(successfulResponse(sectionLibraryRevision), {
status: 201,
})
);
vi.stubGlobal("fetch", fetchMock);

await deploy(config);
const revision = await deploy(config);

expect(revision).toEqual(sectionLibraryRevision);

expect(fetchMock).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -120,7 +139,7 @@ describe("deploy", () => {
vi
.fn()
.mockResolvedValueOnce(
new Response(successfulResponse, { status: 200 })
new Response(successfulResponse(sectionLibrary), { status: 200 })
)
.mockResolvedValueOnce(
new Response(
Expand Down Expand Up @@ -151,7 +170,7 @@ describe("deploy", () => {
vi
.fn()
.mockResolvedValueOnce(
new Response(successfulResponse, { status: 200 })
new Response(successfulResponse(sectionLibrary), { status: 200 })
)
.mockResolvedValueOnce(
new Response(
Expand Down Expand Up @@ -250,7 +269,7 @@ describe("deploy", () => {
vi
.fn()
.mockResolvedValueOnce(
new Response(successfulResponse, { status: 200 })
new Response(successfulResponse(sectionLibrary), { status: 200 })
)
.mockResolvedValueOnce(new Response(responseBody, { status: 400 }))
);
Expand All @@ -265,9 +284,17 @@ describe("deploy", () => {
vi.mocked(prompts).mockResolvedValueOnce({ value: true });
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response(successfulResponse, { status: 404 }))
.mockResolvedValueOnce(new Response(successfulResponse, { status: 201 }))
.mockResolvedValueOnce(new Response(successfulResponse, { status: 201 }));
.mockResolvedValueOnce(
new Response(successfulResponse({}), { status: 404 })
)
.mockResolvedValueOnce(
new Response(successfulResponse(sectionLibrary), { status: 201 })
)
.mockResolvedValueOnce(
new Response(successfulResponse(sectionLibraryRevision), {
status: 201,
})
);
vi.stubGlobal("fetch", fetchMock);

await deploy(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const config: DeployConfig = {
partition: "US",
apiHost: "https://sbx-api.yextapis.com",
};
const revisionId = "019c9cb5-0bc5-76cd-848c-25657b850a8f";
const revisionName = `accounts/123/sectionLibraries/test-library/revisions/${revisionId}`;

afterEach(() => {
vi.clearAllMocks();
Expand All @@ -37,17 +39,15 @@ describe("pollRevision", () => {
vi.useFakeTimers();
vi.mocked(getSectionLibraryRevision)
.mockResolvedValueOnce({
name: "revision-name",
name: revisionName,
status: "STATUS_BUILD_PROCESSING",
uid: "revision-uid",
})
.mockResolvedValueOnce({
name: "revision-name",
name: revisionName,
status: "STATUS_BUILD_SUCCEEDED",
uid: "revision-uid",
});

const polling = pollRevision(config, "revision-name", false);
const polling = pollRevision(config, revisionName, false);
await vi.advanceTimersByTimeAsync(2000);
await polling;

Expand All @@ -56,28 +56,27 @@ describe("pollRevision", () => {
"Waiting for Section Library Revision build..."
);
expect(vi.mocked(ora).mock.results[0].value.succeed).toHaveBeenCalledWith(
"Section Library Revision revision-uid build succeeded after 2s."
`Section Library Revision ${revisionId} build succeeded after 2s.`
);
});

it("rejects when the build reaches an unsuccessful terminal status", async () => {
vi.useFakeTimers();
vi.mocked(getSectionLibraryRevision).mockResolvedValueOnce({
name: "revision-name",
status: "STATUS_BUILD_FAILED",
uid: "test-uid",
name: revisionName,
status: "STATUS_BUILD_FAILURE",
});

const polling = pollRevision(config, "revision-name", false);
const polling = pollRevision(config, revisionName, false);
const rejection = expect(polling).rejects.toThrow(
"Section Library Revision failed with status STATUS_BUILD_FAILED."
"Section Library Revision failed with status STATUS_BUILD_FAILURE."
);
await vi.advanceTimersByTimeAsync(1000);
await rejection;

const spinner = vi.mocked(ora).mock.results[0]?.value;
expect(spinner.fail).toHaveBeenCalledWith(
"Section Library Revision failed with status STATUS_BUILD_FAILED."
"Section Library Revision failed with status STATUS_BUILD_FAILURE."
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export async function pollRevision(

if (revision?.status === BUILD_SUCCESS_STATUS) {
const elapsedSeconds = Math.floor((Date.now() - startedAt) / 1000);
const revisionId = revision.name.split("/").pop();
spinner.succeed(
`Section Library Revision ${revision.uid} build succeeded after ${formatElapsed(elapsedSeconds)}.`
`Section Library Revision ${revisionId} build succeeded after ${formatElapsed(elapsedSeconds)}.`
);
} else {
const message = `Section Library Revision failed with status ${lastStatus}.`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ export interface SectionLibrary {

export interface SectionLibraryRevision {
name: string;
status: string;
uid: string;
status: SectionLibraryRevisionStatus;
}

type SectionLibraryRevisionStatus =
| "STATUS_UNSPECIFIED"
| "STATUS_BUILD_PROCESSING"
| "STATUS_BUILD_SUCCEEDED"
| "STATUS_BUILD_SYSTEM_ERROR"
| "STATUS_BUILD_FAILURE"
| "STATUS_BUILD_TIMED_OUT";

interface RevisionSource {
sourceGitOrigin: string;
sourceCommitHash: string;
Expand Down
Loading