|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { runCli } from "./index.js"; |
| 3 | +import { |
| 4 | + createCliTestFixture, |
| 5 | + createTaskAndGetId, |
| 6 | + CliTestFixture, |
| 7 | +} from "./test-helpers.js"; |
| 8 | + |
| 9 | +describe("start command", () => { |
| 10 | + let fixture: CliTestFixture; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + fixture = createCliTestFixture(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + fixture.cleanup(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("marks a pending task as in progress", async () => { |
| 21 | + const taskId = await createTaskAndGetId(fixture, "To start"); |
| 22 | + |
| 23 | + await runCli(["start", taskId], { storage: fixture.storage }); |
| 24 | + |
| 25 | + const out = fixture.output.stdout.join("\n"); |
| 26 | + expect(out).toContain("Started"); |
| 27 | + expect(out).toContain(taskId); |
| 28 | + |
| 29 | + // Verify started_at is set |
| 30 | + const tasks = await fixture.storage.readAsync(); |
| 31 | + const task = tasks.tasks.find((t) => t.id === taskId); |
| 32 | + expect(task?.started_at).toBeTruthy(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("fails when starting an already-started task without force", async () => { |
| 36 | + const taskId = await createTaskAndGetId(fixture, "To start"); |
| 37 | + |
| 38 | + // Start the task once |
| 39 | + await runCli(["start", taskId], { storage: fixture.storage }); |
| 40 | + fixture.output.stdout.length = 0; |
| 41 | + fixture.output.stderr.length = 0; |
| 42 | + |
| 43 | + // Try to start it again without force |
| 44 | + await expect( |
| 45 | + runCli(["start", taskId], { storage: fixture.storage }), |
| 46 | + ).rejects.toThrow("process.exit"); |
| 47 | + |
| 48 | + const err = fixture.output.stderr.join("\n"); |
| 49 | + expect(err).toContain("already in progress"); |
| 50 | + expect(err).toContain("--force"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("succeeds when starting an already-started task with --force", async () => { |
| 54 | + const taskId = await createTaskAndGetId(fixture, "To start"); |
| 55 | + |
| 56 | + // Start the task once |
| 57 | + await runCli(["start", taskId], { storage: fixture.storage }); |
| 58 | + |
| 59 | + // Get the original started_at |
| 60 | + let tasks = await fixture.storage.readAsync(); |
| 61 | + const originalStartedAt = tasks.tasks.find( |
| 62 | + (t) => t.id === taskId, |
| 63 | + )?.started_at; |
| 64 | + |
| 65 | + // Wait a tiny bit to ensure timestamp would be different |
| 66 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 67 | + |
| 68 | + fixture.output.stdout.length = 0; |
| 69 | + fixture.output.stderr.length = 0; |
| 70 | + |
| 71 | + // Start again with force |
| 72 | + await runCli(["start", taskId, "--force"], { storage: fixture.storage }); |
| 73 | + |
| 74 | + const out = fixture.output.stdout.join("\n"); |
| 75 | + expect(out).toContain("Started"); |
| 76 | + |
| 77 | + // Verify started_at was updated |
| 78 | + tasks = await fixture.storage.readAsync(); |
| 79 | + const newStartedAt = tasks.tasks.find((t) => t.id === taskId)?.started_at; |
| 80 | + expect(newStartedAt).toBeTruthy(); |
| 81 | + expect(newStartedAt).not.toBe(originalStartedAt); |
| 82 | + }); |
| 83 | + |
| 84 | + it("fails when starting a completed task", async () => { |
| 85 | + const taskId = await createTaskAndGetId(fixture, "To complete first"); |
| 86 | + |
| 87 | + // Complete the task |
| 88 | + await runCli(["complete", taskId, "-r", "Done"], { |
| 89 | + storage: fixture.storage, |
| 90 | + }); |
| 91 | + fixture.output.stdout.length = 0; |
| 92 | + fixture.output.stderr.length = 0; |
| 93 | + |
| 94 | + // Try to start it |
| 95 | + await expect( |
| 96 | + runCli(["start", taskId], { storage: fixture.storage }), |
| 97 | + ).rejects.toThrow("process.exit"); |
| 98 | + |
| 99 | + const err = fixture.output.stderr.join("\n"); |
| 100 | + expect(err).toContain("Cannot start a completed task"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("fails for nonexistent task", async () => { |
| 104 | + await expect( |
| 105 | + runCli(["start", "nonexist"], { storage: fixture.storage }), |
| 106 | + ).rejects.toThrow("process.exit"); |
| 107 | + |
| 108 | + const err = fixture.output.stderr.join("\n"); |
| 109 | + expect(err).toContain("not found"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("requires task ID", async () => { |
| 113 | + await expect( |
| 114 | + runCli(["start"], { storage: fixture.storage }), |
| 115 | + ).rejects.toThrow("process.exit"); |
| 116 | + |
| 117 | + const err = fixture.output.stderr.join("\n"); |
| 118 | + expect(err).toContain("Task ID is required"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("shows help with --help flag", async () => { |
| 122 | + await runCli(["start", "--help"], { storage: fixture.storage }); |
| 123 | + |
| 124 | + const out = fixture.output.stdout.join("\n"); |
| 125 | + expect(out).toContain("dex start"); |
| 126 | + expect(out).toContain("--force"); |
| 127 | + expect(out).toContain("in progress"); |
| 128 | + }); |
| 129 | +}); |
0 commit comments