-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.test.mjs
More file actions
88 lines (73 loc) · 2.76 KB
/
Copy pathcli.test.mjs
File metadata and controls
88 lines (73 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { expect, test, describe } from "bun:test";
import pkg from "./package.json" with { type: "json" };
const { version } = pkg;
/**
* Helper to run the CLI and capture its output.
*/
const runCli = async (args) => {
const proc = Bun.spawn(["bun", "run", "cli.mjs", ...args], {
stdout: "pipe",
stderr: "pipe",
});
const stdout = await new Response(proc.stdout).text();
const stderr = await new Response(proc.stderr).text();
await proc.exited;
return { stdout, stderr, status: proc.exitCode };
};
describe("CLI", () => {
test("no arguments returns 8 digits (YYYYMMDD)", async () => {
const { stdout, status } = await runCli([]);
expect(status).toBe(0);
expect(stdout.trim()).toMatch(/^\d{8}$/);
});
test("--precision seconds returns 14 digits", async () => {
const { stdout, status } = await runCli(["--precision", "seconds"]);
expect(status).toBe(0);
expect(stdout.trim()).toMatch(/^\d{14}$/);
});
test("-p ms -s - returns 17 digits with separators", async () => {
const { stdout, status } = await runCli(["-p", "ms", "-s", "-"]);
expect(status).toBe(0);
expect(stdout.trim()).toMatch(/^\d{8}-\d{6}-\d{3}$/);
});
test("--date 2024-06-15 works", async () => {
const { stdout, status } = await runCli(["--date", "2024-06-15"]);
expect(status).toBe(0);
expect(stdout.trim()).toContain("20240615");
});
test("-d with short flag works", async () => {
const { stdout, status } = await runCli(["-d", "2024-06-15"]);
expect(status).toBe(0);
expect(stdout.trim()).toContain("20240615");
});
test("--version returns current version", async () => {
const { stdout, status } = await runCli(["--version"]);
expect(status).toBe(0);
expect(stdout.trim()).toBe(version);
});
test("-v returns current version", async () => {
const { stdout, status } = await runCli(["-v"]);
expect(status).toBe(0);
expect(stdout.trim()).toBe(version);
});
test("missing flag value falls back gracefully", async () => {
const { stdout, status } = await runCli(["--precision"]);
expect(status).toBe(0);
expect(stdout.trim()).toMatch(/^\d{8}$/);
});
test("--help returns usage instructions", async () => {
const { stdout, status } = await runCli(["--help"]);
expect(status).toBe(0);
expect(stdout).toContain("Usage: datr [options]");
});
test("-h returns usage instructions", async () => {
const { stdout, status } = await runCli(["-h"]);
expect(status).toBe(0);
expect(stdout).toContain("Usage: datr [options]");
});
test("invalid date input results in exit code 1 and error message", async () => {
const { stderr, status } = await runCli(["--date", "invalid"]);
expect(status).toBe(1);
expect(stderr).toContain("invalid date value");
});
});