Skip to content

Commit c422d5e

Browse files
authored
chore: test output schema (#3)
Adds explicit tests for the output schema.
1 parent fdd9243 commit c422d5e

3 files changed

Lines changed: 55 additions & 17 deletions

File tree

dist/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27054,7 +27054,7 @@ async function main() {
2705427054
core2.setOutput("coder-username", outputs.coderUsername);
2705527055
core2.setOutput("task-name", outputs.taskName);
2705627056
core2.setOutput("task-url", outputs.taskUrl);
27057-
core2.setOutput("task-exists", outputs.taskCreated.toString());
27057+
core2.setOutput("task-created", outputs.taskCreated.toString());
2705827058
core2.debug("Action completed successfully");
2705927059
core2.debug(`Outputs: ${JSON.stringify(outputs, null, 2)}`);
2706027060
} catch (error2) {

src/action.test.ts

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test, beforeEach } from "bun:test";
22
import { CoderTaskAction } from "./action";
33
import type { Octokit } from "./action";
4+
import { ActionOutputsSchema, type ActionOutputs } from "./schemas";
45
import {
56
MockCoderClient,
67
createMockOctokit,
@@ -360,7 +361,7 @@ describe("CoderTaskAction", () => {
360361
// Execute
361362
const result = await action.run();
362363

363-
// Verify
364+
// Verify API calls
364365
expect(coderClient.mockGetCoderUserByGithubID).toHaveBeenCalledWith(12345);
365366
expect(coderClient.mockGetTask).toHaveBeenCalledWith(
366367
mockUser.username,
@@ -372,9 +373,9 @@ describe("CoderTaskAction", () => {
372373
template_version_preset_id: undefined,
373374
input: inputs.coderTaskPrompt,
374375
});
375-
expect(result.coderUsername).toBe("testuser");
376-
expect(result.taskCreated).toBe(true);
377-
expect(result.taskUrl).toContain("/tasks/testuser/");
376+
377+
const parsedResult = ActionOutputsSchema.parse(result);
378+
assertActionOutputs(parsedResult, true);
378379
});
379380

380381
test("sends prompt to existing task", async () => {
@@ -400,7 +401,7 @@ describe("CoderTaskAction", () => {
400401
// Execute
401402
const result = await action.run();
402403

403-
// Verify
404+
// Verify API calls
404405
expect(coderClient.mockGetTask).toHaveBeenCalledWith(
405406
mockUser.username,
406407
mockTask.name,
@@ -411,7 +412,9 @@ describe("CoderTaskAction", () => {
411412
inputs.coderTaskPrompt,
412413
);
413414
expect(coderClient.mockCreateTask).not.toHaveBeenCalled();
414-
expect(result.taskCreated).toBe(false);
415+
416+
const parsedResult = ActionOutputsSchema.parse(result);
417+
assertActionOutputs(parsedResult);
415418
});
416419

417420
test("errors without issue URL", async () => {
@@ -463,9 +466,12 @@ describe("CoderTaskAction", () => {
463466
);
464467

465468
// Execute
466-
await action.run();
469+
const result = await action.run();
470+
471+
const parsedResult = ActionOutputsSchema.parse(result);
472+
assertActionOutputs(parsedResult, true);
467473

468-
// Verify
474+
// Verify GitHub comment
469475
expect(octokit.rest.issues.createComment).toHaveBeenCalledWith(
470476
expect.objectContaining({
471477
owner: "owner",
@@ -517,9 +523,12 @@ describe("CoderTaskAction", () => {
517523
);
518524

519525
// Execute
520-
await action.run();
526+
const result = await action.run();
521527

522-
// Verify
528+
const parsedResult = ActionOutputsSchema.parse(result);
529+
assertActionOutputs(parsedResult, true);
530+
531+
// Verify GitHub comment update
523532
expect(octokit.rest.issues.updateComment).toHaveBeenCalledWith(
524533
expect.objectContaining({
525534
owner: "owner",
@@ -676,7 +685,11 @@ describe("CoderTaskAction", () => {
676685
inputs,
677686
);
678687

679-
await action.run();
688+
const result = await action.run();
689+
690+
const parsedResult = ActionOutputsSchema.parse(result);
691+
assertActionOutputs(parsedResult, true, "task-456");
692+
680693
expect(octokit.rest.issues.createComment).toHaveBeenCalledWith(
681694
expect.objectContaining({
682695
owner: "different-owner",
@@ -716,7 +729,10 @@ describe("CoderTaskAction", () => {
716729
);
717730

718731
// Execute
719-
await action.run();
732+
const result = await action.run();
733+
734+
const parsedResult = ActionOutputsSchema.parse(result);
735+
assertActionOutputs(parsedResult, true);
720736

721737
// Verify - should NOT have called comment APIs
722738
expect(octokit.rest.issues.listComments).not.toHaveBeenCalled();
@@ -753,7 +769,10 @@ describe("CoderTaskAction", () => {
753769
);
754770

755771
// Execute
756-
await action.run();
772+
const result = await action.run();
773+
774+
const parsedResult = ActionOutputsSchema.parse(result);
775+
assertActionOutputs(parsedResult, true);
757776

758777
// Verify - should have called comment APIs
759778
expect(octokit.rest.issues.listComments).toHaveBeenCalled();
@@ -789,7 +808,10 @@ describe("CoderTaskAction", () => {
789808
);
790809

791810
// Execute
792-
await action.run();
811+
const result = await action.run();
812+
813+
const parsedResult = ActionOutputsSchema.parse(result);
814+
assertActionOutputs(parsedResult, true);
793815

794816
// Verify - should have called comment APIs (default behavior)
795817
expect(octokit.rest.issues.listComments).toHaveBeenCalled();
@@ -821,7 +843,10 @@ describe("CoderTaskAction", () => {
821843
);
822844

823845
// Execute
824-
await action.run();
846+
const result = await action.run();
847+
848+
const parsedResult = ActionOutputsSchema.parse(result);
849+
assertActionOutputs(parsedResult);
825850

826851
// Verify - should NOT have called comment APIs
827852
expect(octokit.rest.issues.listComments).not.toHaveBeenCalled();
@@ -830,3 +855,16 @@ describe("CoderTaskAction", () => {
830855
});
831856
});
832857
});
858+
859+
function assertActionOutputs(
860+
outputs: ActionOutputs,
861+
create = false,
862+
taskName = mockTask.name.toString(),
863+
) {
864+
expect(outputs.coderUsername).toBe(mockUser.username);
865+
expect(outputs.taskCreated).toBe(create);
866+
expect(outputs.taskUrl).toMatch(
867+
/^https:\/\/coder\.test\/tasks\/testuser\/[a-f0-9-]+$/,
868+
);
869+
expect(outputs.taskName).toBe(taskName);
870+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async function main() {
4949
core.setOutput("coder-username", outputs.coderUsername);
5050
core.setOutput("task-name", outputs.taskName);
5151
core.setOutput("task-url", outputs.taskUrl);
52-
core.setOutput("task-exists", outputs.taskCreated.toString());
52+
core.setOutput("task-created", outputs.taskCreated.toString());
5353

5454
core.debug("Action completed successfully");
5555
core.debug(`Outputs: ${JSON.stringify(outputs, null, 2)}`);

0 commit comments

Comments
 (0)