|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterAll, describe, expect, it } from "vitest"; |
| 5 | +import { commandWordIndex, openShortcutArgv } from "./projectShortcut.js"; |
| 6 | + |
| 7 | +const commands = new Set(["open", "render", "lint"]); |
| 8 | +const cwd = mkdtempSync(join(tmpdir(), "hf-shortcut-")); |
| 9 | +mkdirSync(join(cwd, "my-video")); |
| 10 | +mkdirSync(join(cwd, "render")); |
| 11 | +writeFileSync(join(cwd, "notes.txt"), ""); |
| 12 | +afterAll(() => rmSync(cwd, { recursive: true, force: true })); |
| 13 | + |
| 14 | +describe("openShortcutArgv", () => { |
| 15 | + it.each([".", "./my-video", "my-video", cwd])("opens the folder %s", (dir) => { |
| 16 | + expect(openShortcutArgv([dir], commands, cwd)).toEqual(["open", dir]); |
| 17 | + }); |
| 18 | + |
| 19 | + it("keeps the flags that follow the folder", () => { |
| 20 | + expect(openShortcutArgv(["my-video", "--json"], commands, cwd)).toEqual([ |
| 21 | + "open", |
| 22 | + "my-video", |
| 23 | + "--json", |
| 24 | + ]); |
| 25 | + }); |
| 26 | + |
| 27 | + it("finds the folder after root flags, like the command word", () => { |
| 28 | + expect(openShortcutArgv(["--verbose", "my-video", "--json"], commands, cwd)).toEqual([ |
| 29 | + "--verbose", |
| 30 | + "open", |
| 31 | + "my-video", |
| 32 | + "--json", |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("ignores a folder after the -- separator", () => { |
| 37 | + expect(openShortcutArgv(["--", "my-video"], commands, cwd)).toEqual(["--", "my-video"]); |
| 38 | + }); |
| 39 | + |
| 40 | + it("lets a known command win over a folder with its name", () => { |
| 41 | + expect(openShortcutArgv(["render"], commands, cwd)).toEqual(["render"]); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each([[[]], [["--help"]], [["notes.txt"]], [["nope"]]])("leaves %j alone", (argv) => { |
| 45 | + expect(openShortcutArgv(argv, commands, cwd)).toEqual(argv); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("commandWordIndex", () => { |
| 50 | + it.each([ |
| 51 | + [["render", "x"], 0], |
| 52 | + [["--verbose", "render"], 1], |
| 53 | + [["--verbose"], -1], |
| 54 | + [["--", "render"], -1], |
| 55 | + [["--verbose", "--", "render"], -1], |
| 56 | + [[], -1], |
| 57 | + ] as const)("%j -> %i", (argv, index) => { |
| 58 | + expect(commandWordIndex(argv)).toBe(index); |
| 59 | + }); |
| 60 | +}); |
0 commit comments