@zinn-dev/cli 0.37.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +198 -85
- package/index.ts +1 -15
- package/package.json +2 -2
- package/src/lib.test.ts +28 -2
- package/src/lib.ts +52 -15
- package/src/presentation/activity-presentation.test.ts +112 -0
- package/src/presentation/activity-presentation.ts +490 -0
- package/src/presentation/column-presentation.ts +19 -9
- package/src/presentation/description-presentation.ts +7 -4
- package/src/presentation/task-presentation.ts +5 -4
- package/src/presentation/time.ts +17 -0
- package/src/project.test.ts +479 -0
- package/src/project.ts +53 -0
- package/src/routes/activity-router.ts +81 -0
- package/src/routes/backup-router.ts +3 -2
- package/src/routes/comment-router.ts +39 -27
- package/src/routes/label-router.ts +25 -17
- package/src/routes/migrate-router.ts +4 -2
- package/src/routes/project-router.ts +110 -88
- package/src/routes/reindex-router.ts +3 -2
- package/src/routes/relation-router.ts +9 -7
- package/src/routes/restore-router.ts +2 -2
- package/src/routes/router.test.ts +18 -0
- package/src/routes/router.ts +108 -22
- package/src/routes/routes.ts +2 -0
- package/src/routes/task-router.ts +172 -103
- package/src/text-edit.ts +68 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
import { runCliInTerminal } from "../testing";
|
|
7
|
+
|
|
8
|
+
const PREFIX = join(tmpdir(), "zinndefaulttest-");
|
|
9
|
+
// Each precedence matrix starts several dozen CLI processes.
|
|
10
|
+
const COMMAND_MATRIX_TIMEOUT_MS = 30_000;
|
|
11
|
+
|
|
12
|
+
function withTestDirectory(run: (directory: string) => void) {
|
|
13
|
+
const directory = mkdtempSync(PREFIX);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
run(directory);
|
|
17
|
+
} finally {
|
|
18
|
+
removeTestDirectory(directory);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Same as withTestDirectory, but removes the directory only after an async run settles.
|
|
23
|
+
async function withTestDirectoryAsync(run: (directory: string) => Promise<void>) {
|
|
24
|
+
const directory = mkdtempSync(PREFIX);
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
await run(directory);
|
|
28
|
+
} finally {
|
|
29
|
+
removeTestDirectory(directory);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function removeTestDirectory(directory: string) {
|
|
34
|
+
if (!directory.startsWith(PREFIX)) {
|
|
35
|
+
throw new Error(`Refusing to delete "${directory}": not a ${PREFIX}* directory`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
rmSync(directory, { recursive: true, force: true });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function runZinn(args: Array<string>, zinnDirectory: string) {
|
|
42
|
+
const proc = Bun.spawnSync(["bun", join(import.meta.dir, "..", "index.ts"), ...args], {
|
|
43
|
+
env: { ...process.env, ZINN_DIR: zinnDirectory },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return { stdout: proc.stdout.toString(), stderr: proc.stderr.toString(), code: proc.exitCode };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function runZinnFrom(
|
|
50
|
+
args: Array<string>,
|
|
51
|
+
directory: string,
|
|
52
|
+
cwd: string,
|
|
53
|
+
additionalEnv: Record<string, string> = {},
|
|
54
|
+
) {
|
|
55
|
+
const env: Record<string, string | undefined> = { ...process.env, HOME: join(directory, "home") };
|
|
56
|
+
delete env.ZINN_DIR;
|
|
57
|
+
Object.assign(env, additionalEnv);
|
|
58
|
+
const cliPath = join(import.meta.dir, "..", "index.ts");
|
|
59
|
+
const proc = Bun.spawnSync(["bun", cliPath, ...args], { cwd, env });
|
|
60
|
+
|
|
61
|
+
return { stdout: proc.stdout.toString(), stderr: proc.stderr.toString(), code: proc.exitCode };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function runZinnWithPipedYes(args: Array<string>, directory: string, cwd: string) {
|
|
65
|
+
const env: Record<string, string | undefined> = { ...process.env, HOME: join(directory, "home") };
|
|
66
|
+
delete env.ZINN_DIR;
|
|
67
|
+
const cliPath = join(import.meta.dir, "..", "index.ts");
|
|
68
|
+
const proc = Bun.spawnSync(
|
|
69
|
+
["/bin/sh", "-c", 'printf "y\\n" | exec bun "$@"', "zinn-confirm", cliPath, ...args],
|
|
70
|
+
{ cwd, env },
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return { stdout: proc.stdout.toString(), stderr: proc.stderr.toString(), code: proc.exitCode };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Runs the CLI attached to a pseudo-terminal and types the input; stdout and stderr arrive together. */
|
|
77
|
+
function runZinnInTerminal(args: Array<string>, directory: string, cwd: string, input: string) {
|
|
78
|
+
const env: Record<string, string | undefined> = { ...process.env, HOME: join(directory, "home") };
|
|
79
|
+
delete env.ZINN_DIR;
|
|
80
|
+
|
|
81
|
+
return runCliInTerminal(args, { cwd, env, input });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function makeRepository(directory: string, localSettings: Record<string, unknown> = {}) {
|
|
85
|
+
const repository = join(directory, "repository");
|
|
86
|
+
const globalDirectory = join(directory, "home", ".zinn");
|
|
87
|
+
mkdirSync(join(repository, ".git"), { recursive: true });
|
|
88
|
+
mkdirSync(globalDirectory, { recursive: true });
|
|
89
|
+
writeFileSync(join(globalDirectory, "settings.json"), JSON.stringify({ storage: { directory: join(directory, "store") } }));
|
|
90
|
+
writeFileSync(join(repository, "zinn.local.json"), JSON.stringify(localSettings));
|
|
91
|
+
|
|
92
|
+
return { repository, globalSettingsPath: join(globalDirectory, "settings.json"), localSettingsPath: join(repository, "zinn.local.json") };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function writeGlobalSettings(directory: string, settings: Record<string, unknown>) {
|
|
96
|
+
const globalSettingsPath = join(directory, "home", ".zinn", "settings.json");
|
|
97
|
+
writeFileSync(globalSettingsPath, JSON.stringify({ storage: { directory: join(directory, "store") }, ...settings }));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function seedProjects(directory: string, store = join(directory, "store")) {
|
|
101
|
+
expect(runZinn(["project", "create", "DEF", "--name", "Default"], store).code).toBe(0);
|
|
102
|
+
expect(runZinn(["project", "create", "ALT", "--name", "Alternate"], store).code).toBe(0);
|
|
103
|
+
expect(runZinn(["task", "create", "DEF", "--title", "needle default"], store).code).toBe(0);
|
|
104
|
+
expect(runZinn(["task", "create", "ALT", "--title", "needle alternate"], store).code).toBe(0);
|
|
105
|
+
expect(runZinn(["project", "column", "create", "DEF", "Only DEF"], store).code).toBe(0);
|
|
106
|
+
expect(runZinn(["project", "column", "create", "ALT", "Only ALT"], store).code).toBe(0);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
test("ZINN_DIR selects an empty or populated store without applying the repository default", () => {
|
|
110
|
+
withTestDirectory((directory) => {
|
|
111
|
+
const { repository } = makeRepository(directory, { defaultProject: "DEF" });
|
|
112
|
+
const emptyStore = join(directory, "empty-store");
|
|
113
|
+
const emptyRun = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: emptyStore });
|
|
114
|
+
|
|
115
|
+
for (const args of [["task", "list"], ["task", "search", "needle"]]) {
|
|
116
|
+
const result = emptyRun(...args, "--json");
|
|
117
|
+
expect(result.code).toBe(0);
|
|
118
|
+
expect(JSON.parse(result.stdout)).toEqual([]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const populatedStore = join(directory, "populated-store");
|
|
122
|
+
seedProjects(directory, populatedStore);
|
|
123
|
+
const populatedRun = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: populatedStore });
|
|
124
|
+
expect(JSON.parse(populatedRun("task", "list", "--json").stdout).map((task: { key: string }) => task.key).sort()).toEqual([
|
|
125
|
+
"ALT-1",
|
|
126
|
+
"DEF-1",
|
|
127
|
+
]);
|
|
128
|
+
expect(JSON.parse(populatedRun("task", "search", "needle", "--json").stdout).map((task: { key: string }) => task.key).sort()).toEqual([
|
|
129
|
+
"ALT-1",
|
|
130
|
+
"DEF-1",
|
|
131
|
+
]);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("ZINN_DIR uses its own default and reports unknown defaults from its settings file", () => {
|
|
136
|
+
withTestDirectory((directory) => {
|
|
137
|
+
const { repository } = makeRepository(directory, { defaultProject: "DEF" });
|
|
138
|
+
const store = join(directory, "store");
|
|
139
|
+
mkdirSync(store, { recursive: true });
|
|
140
|
+
writeFileSync(join(store, "settings.json"), JSON.stringify({ defaultProject: "ALT" }));
|
|
141
|
+
seedProjects(directory, store);
|
|
142
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: store });
|
|
143
|
+
|
|
144
|
+
expect(JSON.parse(invoke("task", "list", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
|
|
145
|
+
expect(JSON.parse(invoke("task", "search", "needle", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
|
|
146
|
+
|
|
147
|
+
writeFileSync(join(store, "settings.json"), JSON.stringify({ defaultProject: "NOPE" }));
|
|
148
|
+
const unknown = invoke("task", "list", "--json");
|
|
149
|
+
expect(unknown.code).toBe(1);
|
|
150
|
+
expect(unknown.stderr).toContain("NOPE");
|
|
151
|
+
expect(unknown.stderr).toContain(join(store, "settings.json"));
|
|
152
|
+
|
|
153
|
+
expect(JSON.parse(invoke("task", "list", "ALT", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
|
|
154
|
+
expect(JSON.parse(invoke("task", "list", "--all-projects", "--json").stdout)).toHaveLength(2);
|
|
155
|
+
expect(JSON.parse(invoke("task", "search", " alt-1 ", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("ZINN_DIR ignores malformed repository and home settings but validates its own settings", () => {
|
|
160
|
+
withTestDirectory((directory) => {
|
|
161
|
+
const { repository, localSettingsPath, globalSettingsPath } = makeRepository(directory);
|
|
162
|
+
const store = join(directory, "store");
|
|
163
|
+
writeFileSync(localSettingsPath, "{");
|
|
164
|
+
writeFileSync(globalSettingsPath, "{");
|
|
165
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository, { ZINN_DIR: store });
|
|
166
|
+
|
|
167
|
+
const ignoredMalformedFiles = invoke("task", "list", "--json");
|
|
168
|
+
expect(ignoredMalformedFiles.code).toBe(0);
|
|
169
|
+
expect(JSON.parse(ignoredMalformedFiles.stdout)).toEqual([]);
|
|
170
|
+
expect(ignoredMalformedFiles.stderr).not.toContain(localSettingsPath);
|
|
171
|
+
expect(ignoredMalformedFiles.stderr).not.toContain(globalSettingsPath);
|
|
172
|
+
|
|
173
|
+
const selectedStoreSettingsPath = join(store, "settings.json");
|
|
174
|
+
writeFileSync(selectedStoreSettingsPath, JSON.stringify({ defaultProject: 42 }));
|
|
175
|
+
const invalidSelectedStoreSettings = invoke("task", "list", "--json");
|
|
176
|
+
expect(invalidSelectedStoreSettings.code).toBe(1);
|
|
177
|
+
expect(invalidSelectedStoreSettings.stderr).toContain(selectedStoreSettingsPath);
|
|
178
|
+
expect(invalidSelectedStoreSettings.stderr).toContain("defaultProject");
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
async function expectDefaultCommands(directory: string, repository: string, expectedKey: string, otherKey: string) {
|
|
183
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
184
|
+
const list = invoke("task", "list", "--json");
|
|
185
|
+
const search = invoke("task", "search", "needle", "--json");
|
|
186
|
+
const created = invoke("task", "create", "--title", "created by default", "--json");
|
|
187
|
+
const viewed = invoke("project", "view", "--json");
|
|
188
|
+
const columns = invoke("project", "column", "list", "--json");
|
|
189
|
+
const edited = invoke("project", "edit", "--name", "Edited by default", "--json");
|
|
190
|
+
|
|
191
|
+
expect(list.code).toBe(0);
|
|
192
|
+
expect(JSON.parse(list.stdout).map((task: { key: string }) => task.key)).toEqual([`${expectedKey}-1`]);
|
|
193
|
+
expect(search.code).toBe(0);
|
|
194
|
+
expect(JSON.parse(search.stdout).map((task: { key: string }) => task.key)).toEqual([`${expectedKey}-1`]);
|
|
195
|
+
expect(created.code).toBe(0);
|
|
196
|
+
expect(JSON.parse(created.stdout).key).toBe(`${expectedKey}-2`);
|
|
197
|
+
expect(viewed.code).toBe(0);
|
|
198
|
+
expect(JSON.parse(viewed.stdout).key).toBe(expectedKey);
|
|
199
|
+
expect(columns.code).toBe(0);
|
|
200
|
+
expect(JSON.parse(columns.stdout).map((column: { name: string }) => column.name)).toContain(`Only ${expectedKey}`);
|
|
201
|
+
expect(edited.code).toBe(0);
|
|
202
|
+
expect(JSON.parse(edited.stdout)).toMatchObject({ key: expectedKey, name: "Edited by default" });
|
|
203
|
+
|
|
204
|
+
const explicitList = invoke("task", "list", otherKey, "--json");
|
|
205
|
+
const explicitSearch = invoke("task", "search", "needle", otherKey, "--json");
|
|
206
|
+
const explicitCreate = invoke("task", "create", otherKey, "--title", "created explicitly", "--json");
|
|
207
|
+
const explicitView = invoke("project", "view", otherKey, "--json");
|
|
208
|
+
const explicitColumns = invoke("project", "column", "list", otherKey, "--json");
|
|
209
|
+
const explicitEdit = invoke("project", "edit", otherKey, "--name", "Edited explicitly", "--json");
|
|
210
|
+
|
|
211
|
+
expect(JSON.parse(explicitList.stdout).map((task: { key: string }) => task.key)).toEqual([`${otherKey}-1`]);
|
|
212
|
+
expect(JSON.parse(explicitSearch.stdout).map((task: { key: string }) => task.key)).toEqual([`${otherKey}-1`]);
|
|
213
|
+
expect(JSON.parse(explicitCreate.stdout).key).toBe(`${otherKey}-2`);
|
|
214
|
+
expect(JSON.parse(explicitView.stdout).key).toBe(otherKey);
|
|
215
|
+
expect(JSON.parse(explicitColumns.stdout).map((column: { name: string }) => column.name)).toContain(`Only ${otherKey}`);
|
|
216
|
+
expect(JSON.parse(explicitEdit.stdout)).toMatchObject({ key: otherKey, name: "Edited explicitly" });
|
|
217
|
+
expect(JSON.parse(invoke("task", "view", `${otherKey}-1`, "--json").stdout).key).toBe(`${otherKey}-1`);
|
|
218
|
+
|
|
219
|
+
const deleted = await runZinnInTerminal(["project", "delete"], directory, repository, "y\n");
|
|
220
|
+
expect(deleted.code).toBe(0);
|
|
221
|
+
expect(deleted.output).toContain(expectedKey);
|
|
222
|
+
expect(JSON.parse(invoke("project", "list", "--json").stdout).map((project: { key: string }) => project.key)).toEqual([otherKey]);
|
|
223
|
+
const explicitDelete = runZinnFrom(["project", "delete", otherKey, "--yes", "--json"], directory, repository);
|
|
224
|
+
expect(explicitDelete.code).toBe(0);
|
|
225
|
+
expect(JSON.parse(explicitDelete.stdout).key).toBe(otherKey);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
test("each default-project command uses local settings, overrides global settings and yields to a typed key", async () => {
|
|
229
|
+
await withTestDirectoryAsync(async (directory) => {
|
|
230
|
+
makeRepository(directory, { defaultProject: "def" });
|
|
231
|
+
writeGlobalSettings(directory, { defaultProject: "ALT" });
|
|
232
|
+
seedProjects(directory);
|
|
233
|
+
|
|
234
|
+
await expectDefaultCommands(directory, join(directory, "repository"), "DEF", "ALT");
|
|
235
|
+
});
|
|
236
|
+
}, COMMAND_MATRIX_TIMEOUT_MS);
|
|
237
|
+
|
|
238
|
+
test("each default-project command falls back to global settings when local settings omit the field", async () => {
|
|
239
|
+
await withTestDirectoryAsync(async (directory) => {
|
|
240
|
+
makeRepository(directory, {});
|
|
241
|
+
writeGlobalSettings(directory, { defaultProject: "DEF" });
|
|
242
|
+
seedProjects(directory);
|
|
243
|
+
|
|
244
|
+
await expectDefaultCommands(directory, join(directory, "repository"), "DEF", "ALT");
|
|
245
|
+
});
|
|
246
|
+
}, COMMAND_MATRIX_TIMEOUT_MS);
|
|
247
|
+
|
|
248
|
+
test("commands without a default keep their existing project behavior, and task-key commands stay project-specific", () => {
|
|
249
|
+
withTestDirectory((directory) => {
|
|
250
|
+
const { repository } = makeRepository(directory);
|
|
251
|
+
seedProjects(directory);
|
|
252
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
253
|
+
|
|
254
|
+
expect(JSON.parse(invoke("task", "list", "--json").stdout)).toHaveLength(2);
|
|
255
|
+
expect(JSON.parse(invoke("task", "search", "needle", "--json").stdout)).toHaveLength(2);
|
|
256
|
+
expect(JSON.parse(invoke("task", "list", "--all-projects", "--json").stdout)).toHaveLength(2);
|
|
257
|
+
expect(JSON.parse(invoke("task", "search", "needle", "--all-projects", "--json").stdout)).toHaveLength(2);
|
|
258
|
+
expect(invoke("task", "create", "--title", "needs a project").code).toBe(1);
|
|
259
|
+
expect(invoke("project", "view").code).toBe(1);
|
|
260
|
+
expect(invoke("project", "column", "list").code).toBe(1);
|
|
261
|
+
expect(invoke("project", "edit", "--name", "No project").code).toBe(1);
|
|
262
|
+
expect(invoke("project", "delete").code).toBe(1);
|
|
263
|
+
expect(JSON.parse(invoke("task", "view", "ALT-1", "--json").stdout).key).toBe("ALT-1");
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test("all-projects bypasses an unknown default, conflicts with typed keys, and keeps list filters", () => {
|
|
268
|
+
withTestDirectory((directory) => {
|
|
269
|
+
const { repository } = makeRepository(directory, { defaultProject: "NOPE" });
|
|
270
|
+
seedProjects(directory);
|
|
271
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
272
|
+
|
|
273
|
+
for (const args of [["task", "list"], ["task", "search", "needle"]]) {
|
|
274
|
+
const allProjects = invoke(...args, "--all-projects", "--json");
|
|
275
|
+
expect(allProjects.code).toBe(0);
|
|
276
|
+
expect(JSON.parse(allProjects.stdout)).toHaveLength(2);
|
|
277
|
+
const typedConflict = args[1] === "list"
|
|
278
|
+
? invoke("task", "list", "ALT", "--all-projects", "--json")
|
|
279
|
+
: invoke("task", "search", "needle", "ALT", "--all-projects", "--json");
|
|
280
|
+
expect(typedConflict.code).toBe(1);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const defaultUnknown = invoke("task", "list", "--json");
|
|
284
|
+
expect(defaultUnknown.code).toBe(1);
|
|
285
|
+
expect(defaultUnknown.stderr).toContain("NOPE");
|
|
286
|
+
expect(defaultUnknown.stderr).toContain(join(repository, "zinn.local.json"));
|
|
287
|
+
|
|
288
|
+
writeFileSync(join(repository, "zinn.local.json"), JSON.stringify({ defaultProject: "DEF" }));
|
|
289
|
+
expect(runZinn(["task", "archive", "DEF-1"], join(directory, "store")).code).toBe(0);
|
|
290
|
+
const active = invoke("task", "list", "--all-projects", "--json");
|
|
291
|
+
const all = invoke("task", "list", "--all-projects", "--all", "--json");
|
|
292
|
+
const archived = invoke("task", "list", "--all-projects", "--archived", "--json");
|
|
293
|
+
const searchAll = invoke("task", "search", "needle", "--all-projects", "--all", "--json");
|
|
294
|
+
const searchArchived = invoke("task", "search", "needle", "--all-projects", "--archived", "--json");
|
|
295
|
+
expect(JSON.parse(active.stdout)).toHaveLength(1);
|
|
296
|
+
expect(JSON.parse(all.stdout)).toHaveLength(2);
|
|
297
|
+
expect(JSON.parse(archived.stdout).map((task: { key: string }) => task.key)).toEqual(["DEF-1"]);
|
|
298
|
+
expect(JSON.parse(searchAll.stdout)).toHaveLength(2);
|
|
299
|
+
expect(JSON.parse(searchArchived.stdout).map((task: { key: string }) => task.key)).toEqual(["DEF-1"]);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
test("task-key search queries use the matching project instead of a default and keep an explicit project filter", () => {
|
|
304
|
+
withTestDirectory((directory) => {
|
|
305
|
+
const { repository } = makeRepository(directory, { defaultProject: "DEF" });
|
|
306
|
+
seedProjects(directory);
|
|
307
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
308
|
+
|
|
309
|
+
expect(JSON.parse(invoke("task", "search", "needle", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["DEF-1"]);
|
|
310
|
+
expect(JSON.parse(invoke("task", "search", "ALT-1", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
|
|
311
|
+
|
|
312
|
+
writeFileSync(join(repository, "zinn.local.json"), JSON.stringify({ defaultProject: "NOPE" }));
|
|
313
|
+
expect(JSON.parse(invoke("task", "search", " alt-1 ", "--json").stdout).map((task: { key: string }) => task.key)).toEqual(["ALT-1"]);
|
|
314
|
+
|
|
315
|
+
const filtered = invoke("task", "search", " ALT-1 ", "DEF", "--json");
|
|
316
|
+
expect(filtered.code).toBe(0);
|
|
317
|
+
expect(JSON.parse(filtered.stdout)).toEqual([]);
|
|
318
|
+
|
|
319
|
+
const conflict = invoke("task", "search", "ALT-1", "DEF", "--all-projects", "--json");
|
|
320
|
+
expect(conflict.code).toBe(1);
|
|
321
|
+
expect(conflict.stdout).toBe("");
|
|
322
|
+
expect(conflict.stderr).toContain("--all-projects");
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("task list and search report conflicting archive flags before resolving an unknown default", () => {
|
|
327
|
+
withTestDirectory((directory) => {
|
|
328
|
+
const { repository, localSettingsPath } = makeRepository(directory, { defaultProject: "NOPE" });
|
|
329
|
+
seedProjects(directory);
|
|
330
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
331
|
+
|
|
332
|
+
for (const args of [["task", "list"], ["task", "search", "needle"]]) {
|
|
333
|
+
const result = invoke(...args, "--archived", "--all", "--json");
|
|
334
|
+
expect(result.code).toBe(1);
|
|
335
|
+
expect(result.stdout).toBe("");
|
|
336
|
+
expect(result.stderr).toContain("--archived");
|
|
337
|
+
expect(result.stderr).toContain("--all");
|
|
338
|
+
expect(result.stderr).not.toContain("NOPE");
|
|
339
|
+
expect(result.stderr).not.toContain(localSettingsPath);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("a missing conditional project key reports its value and optional usage", () => {
|
|
345
|
+
withTestDirectory((directory) => {
|
|
346
|
+
const { repository } = makeRepository(directory);
|
|
347
|
+
seedProjects(directory);
|
|
348
|
+
const result = runZinnFrom(["project", "view", "--json"], directory, repository);
|
|
349
|
+
|
|
350
|
+
expect(result.code).toBe(1);
|
|
351
|
+
expect(result.stdout).toBe("");
|
|
352
|
+
const error = JSON.parse(result.stderr).error;
|
|
353
|
+
expect(error.message).toContain("<project-key>");
|
|
354
|
+
expect(error.message).toContain("defaultProject");
|
|
355
|
+
expect(error.help).toContain("Usage: zinn project view [<project-key>]");
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test("unknown defaults identify their key and settings file", () => {
|
|
360
|
+
withTestDirectory((directory) => {
|
|
361
|
+
const { repository, globalSettingsPath, localSettingsPath } = makeRepository(directory, { defaultProject: "NOPE" });
|
|
362
|
+
seedProjects(directory);
|
|
363
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
364
|
+
const assertUnknownDefaultDoesNotMutate = (settingsPath: string) => {
|
|
365
|
+
for (const args of [
|
|
366
|
+
["project", "view", "--json"],
|
|
367
|
+
["task", "create", "--title", "must not be created", "--json"],
|
|
368
|
+
["project", "edit", "--name", "Must not be applied", "--json"],
|
|
369
|
+
["project", "delete", "--yes"],
|
|
370
|
+
]) {
|
|
371
|
+
const result = invoke(...args);
|
|
372
|
+
expect(result.code).toBe(1);
|
|
373
|
+
expect(result.stderr).toContain("NOPE");
|
|
374
|
+
expect(result.stderr).toContain(settingsPath);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
expect(JSON.parse(invoke("project", "view", "DEF", "--json").stdout).name).toBe("Default");
|
|
378
|
+
expect(JSON.parse(invoke("task", "list", "DEF", "--json").stdout)).toHaveLength(1);
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
assertUnknownDefaultDoesNotMutate(localSettingsPath);
|
|
382
|
+
|
|
383
|
+
const explicitList = invoke("task", "list", "ALT", "--json");
|
|
384
|
+
const explicitSearch = invoke("task", "search", "needle", "ALT", "--json");
|
|
385
|
+
const explicitCreate = invoke("task", "create", "ALT", "--title", "typed key bypasses unknown default", "--json");
|
|
386
|
+
const explicitView = invoke("project", "view", "ALT", "--json");
|
|
387
|
+
const explicitColumns = invoke("project", "column", "list", "ALT", "--json");
|
|
388
|
+
const explicitEdit = invoke("project", "edit", "ALT", "--name", "Edited explicitly", "--json");
|
|
389
|
+
const explicitDelete = invoke("project", "delete", "ALT", "--yes", "--json");
|
|
390
|
+
expect(explicitList.code).toBe(0);
|
|
391
|
+
expect(explicitSearch.code).toBe(0);
|
|
392
|
+
expect(JSON.parse(explicitCreate.stdout).key).toBe("ALT-2");
|
|
393
|
+
expect(JSON.parse(explicitView.stdout).key).toBe("ALT");
|
|
394
|
+
expect(explicitColumns.code).toBe(0);
|
|
395
|
+
expect(JSON.parse(explicitEdit.stdout).name).toBe("Edited explicitly");
|
|
396
|
+
expect(JSON.parse(explicitDelete.stdout).key).toBe("ALT");
|
|
397
|
+
expect(JSON.parse(invoke("project", "view", "DEF", "--json").stdout).name).toBe("Default");
|
|
398
|
+
|
|
399
|
+
writeFileSync(join(repository, "zinn.local.json"), "{}");
|
|
400
|
+
writeGlobalSettings(directory, { defaultProject: "NOPE" });
|
|
401
|
+
assertUnknownDefaultDoesNotMutate(globalSettingsPath);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
test("project delete with a default prompts despite --yes, and refuses without a terminal or with JSON", async () => {
|
|
406
|
+
const directory = mkdtempSync(PREFIX);
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
const { repository } = makeRepository(directory, { defaultProject: "DEF" });
|
|
410
|
+
seedProjects(directory);
|
|
411
|
+
const jsonRefusal = runZinnFrom(["project", "delete", "--yes", "--json"], directory, repository);
|
|
412
|
+
expect(jsonRefusal.code).toBe(1);
|
|
413
|
+
expect(jsonRefusal.stdout).toBe("");
|
|
414
|
+
const refusal = JSON.parse(jsonRefusal.stderr);
|
|
415
|
+
expect(Object.keys(refusal)).toEqual(["error"]);
|
|
416
|
+
expect(Object.keys(refusal.error)).toEqual(["message"]);
|
|
417
|
+
expect(typeof refusal.error.message).toBe("string");
|
|
418
|
+
expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout)).toHaveLength(2);
|
|
419
|
+
|
|
420
|
+
const noInput = runZinnFrom(["project", "delete", "--yes"], directory, repository);
|
|
421
|
+
const pipedInput = runZinnWithPipedYes(["project", "delete", "--yes"], directory, repository);
|
|
422
|
+
for (const result of [noInput, pipedInput]) {
|
|
423
|
+
expect(result.code).toBe(1);
|
|
424
|
+
expect(result.stdout).toBe("");
|
|
425
|
+
expect(result.stderr).toContain("zinn project delete DEF --yes");
|
|
426
|
+
}
|
|
427
|
+
expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout)).toHaveLength(2);
|
|
428
|
+
|
|
429
|
+
const prompted = await runZinnInTerminal(["project", "delete", "--yes"], directory, repository, "y\n");
|
|
430
|
+
expect(prompted.code).toBe(0);
|
|
431
|
+
expect(prompted.output).toContain("--yes");
|
|
432
|
+
expect(prompted.output).toContain("Default");
|
|
433
|
+
expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout).map((project: { key: string }) => project.key)).toEqual(["ALT"]);
|
|
434
|
+
} finally {
|
|
435
|
+
removeTestDirectory(directory);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
test("malformed defaultProject settings still fail with a typed key or --all-projects", () => {
|
|
440
|
+
withTestDirectory((directory) => {
|
|
441
|
+
const { repository, localSettingsPath } = makeRepository(directory, { defaultProject: 42 });
|
|
442
|
+
seedProjects(directory);
|
|
443
|
+
const typed = runZinnFrom(["task", "list", "ALT", "--json"], directory, repository);
|
|
444
|
+
const allProjects = runZinnFrom(["task", "list", "--all-projects", "--json"], directory, repository);
|
|
445
|
+
|
|
446
|
+
for (const result of [typed, allProjects]) {
|
|
447
|
+
expect(result.code).toBe(1);
|
|
448
|
+
expect(result.stderr).toContain(localSettingsPath);
|
|
449
|
+
expect(result.stderr).toContain("defaultProject");
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
test("project delete with a typed key and --yes deletes without prompting", () => {
|
|
455
|
+
withTestDirectory((directory) => {
|
|
456
|
+
const { repository } = makeRepository(directory, { defaultProject: "DEF" });
|
|
457
|
+
seedProjects(directory);
|
|
458
|
+
|
|
459
|
+
const result = runZinnFrom(["project", "delete", "ALT", "--yes", "--json"], directory, repository);
|
|
460
|
+
expect(result.code).toBe(0);
|
|
461
|
+
expect(JSON.parse(result.stdout).key).toBe("ALT");
|
|
462
|
+
expect(JSON.parse(runZinnFrom(["project", "list", "--json"], directory, repository).stdout).map((project: { key: string }) => project.key)).toEqual(["DEF"]);
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
test("column writes still require an explicit project key when a default is set", () => {
|
|
467
|
+
withTestDirectory((directory) => {
|
|
468
|
+
const { repository } = makeRepository(directory, { defaultProject: "DEF" });
|
|
469
|
+
seedProjects(directory);
|
|
470
|
+
const invoke = (...args: Array<string>) => runZinnFrom(args, directory, repository);
|
|
471
|
+
|
|
472
|
+
expect(invoke("project", "column", "create", "Later", "--json").code).toBe(1);
|
|
473
|
+
expect(invoke("project", "column", "edit", "Backlog", "--name", "Later", "--json").code).toBe(1);
|
|
474
|
+
expect(invoke("project", "column", "order", "Backlog", "first", "--json").code).toBe(1);
|
|
475
|
+
const listedColumns = invoke("project", "column", "list", "--json");
|
|
476
|
+
expect(listedColumns.code).toBe(0);
|
|
477
|
+
expect(JSON.parse(listedColumns.stdout).map((column: { name: string }) => column.name)).toContain("Only DEF");
|
|
478
|
+
});
|
|
479
|
+
});
|
package/src/project.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { project, resolveSettings } from "@zinn-dev/core";
|
|
3
|
+
import { UsageError } from "./lib";
|
|
4
|
+
|
|
5
|
+
/** Chooses the explicit project or the configured default, retaining unscoped list/search behavior. */
|
|
6
|
+
export function resolveProjectKey(
|
|
7
|
+
typedKey: string | undefined,
|
|
8
|
+
{
|
|
9
|
+
required = false,
|
|
10
|
+
allProjects = false,
|
|
11
|
+
useDefault = true,
|
|
12
|
+
}: {
|
|
13
|
+
required?: boolean;
|
|
14
|
+
allProjects?: boolean;
|
|
15
|
+
useDefault?: boolean;
|
|
16
|
+
} = {},
|
|
17
|
+
): string | undefined {
|
|
18
|
+
if (allProjects) {
|
|
19
|
+
if (typedKey !== undefined) {
|
|
20
|
+
throw new UsageError("--all-projects cannot be combined with a project key");
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typedKey !== undefined) {
|
|
26
|
+
return typedKey;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!useDefault) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const settings = resolveSettings({ cwd: process.cwd(), env: process.env, home: homedir() });
|
|
34
|
+
const defaultProject = settings.merged.defaultProject;
|
|
35
|
+
if (defaultProject === undefined) {
|
|
36
|
+
if (required) {
|
|
37
|
+
throw new UsageError(
|
|
38
|
+
"Missing argument <project-key>. Supply a key or configure defaultProject",
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const settingsFile =
|
|
45
|
+
settings.local?.settings.defaultProject === undefined ? settings.global! : settings.local;
|
|
46
|
+
if (project.getByKey(defaultProject) == null) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Default project "${defaultProject}" from settings file ${settingsFile.path} does not exist`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return defaultProject;
|
|
53
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { RouteDef } from "../types";
|
|
2
|
+
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { activity, activityLimitMessage, positiveIntegerPattern } from "@zinn-dev/core";
|
|
5
|
+
import { output, quit, validatePositionals, validateUniqueOptions } from "../lib";
|
|
6
|
+
import { formatActivityList, presentActivities } from "../presentation/activity-presentation";
|
|
7
|
+
|
|
8
|
+
export const activityRouter = {
|
|
9
|
+
list: {
|
|
10
|
+
run: (args) => {
|
|
11
|
+
const { values, positionals, tokens } = parseArgs({
|
|
12
|
+
args,
|
|
13
|
+
options: { task: { type: "string" }, limit: { type: "string" } },
|
|
14
|
+
allowPositionals: true,
|
|
15
|
+
strict: true,
|
|
16
|
+
tokens: true,
|
|
17
|
+
});
|
|
18
|
+
validateUniqueOptions(tokens);
|
|
19
|
+
validatePositionals(positionals, []);
|
|
20
|
+
|
|
21
|
+
// Safe integers can have one more digit than readable task numbers.
|
|
22
|
+
const limitPattern = new RegExp(`^${positiveIntegerPattern}\\d?$`);
|
|
23
|
+
if (values.limit !== undefined && !limitPattern.test(values.limit)) {
|
|
24
|
+
quit(activityLimitMessage);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const events = activity.getAll({
|
|
28
|
+
taskKey: values.task,
|
|
29
|
+
limit: values.limit === undefined ? undefined : Number(values.limit),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
output(events, (items) =>
|
|
33
|
+
formatActivityList(presentActivities(items, { taskKey: values.task })),
|
|
34
|
+
);
|
|
35
|
+
},
|
|
36
|
+
help: `Usage: zinn activity list [--task <task-key>] [--limit <n>] [--json]
|
|
37
|
+
|
|
38
|
+
List recorded activity across the database, newest first.
|
|
39
|
+
By default, all matching events are returned. --limit takes a positive safe integer.
|
|
40
|
+
--task includes existing, archived and deleted task lifetimes for that key,
|
|
41
|
+
including the recorded project deletion that removed a task.
|
|
42
|
+
A key reused after project deletion includes every known lifetime.
|
|
43
|
+
A valid key without recoverable history returns an empty list.
|
|
44
|
+
|
|
45
|
+
Text times are local dates/times with a UTC offset.
|
|
46
|
+
Event IDs are the shortest globally unique left prefixes of at least eight hex digits.
|
|
47
|
+
Use activity view for full recorded changes; full IDs are durable, short prefixes may become ambiguous.
|
|
48
|
+
With --json, output is an array of full activity records, with parsed changes or null.
|
|
49
|
+
Group JSON records by entity_type and entity_id to distinguish tasks sharing a readable key.
|
|
50
|
+
id identifies the activity event.
|
|
51
|
+
A project deletion event's entity_id is the project ID and removed task IDs are in changes.tasks.
|
|
52
|
+
|
|
53
|
+
Examples:
|
|
54
|
+
zinn activity list --limit 10
|
|
55
|
+
zinn activity list --task APP-2 --json`,
|
|
56
|
+
},
|
|
57
|
+
view: {
|
|
58
|
+
run: (args) => {
|
|
59
|
+
const { positionals } = parseArgs({ args, allowPositionals: true, strict: true });
|
|
60
|
+
validatePositionals(positionals, ["<activity-id>"]);
|
|
61
|
+
output(activity.getById(positionals[0]!), (event) =>
|
|
62
|
+
formatActivityList(presentActivities([event], { showsFullIds: true }), {
|
|
63
|
+
showsDetails: true,
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
help: `Usage: zinn activity view <activity-id> [--json]
|
|
68
|
+
|
|
69
|
+
Show one recorded event with full activity/entity IDs, actor and complete changes.
|
|
70
|
+
Text and line breaks are preserved; unknown change fields remain available as raw JSON.
|
|
71
|
+
Text times are local dates/times with a UTC offset.
|
|
72
|
+
|
|
73
|
+
Accept a full UUID or unique left prefix with at least eight hexadecimal digits.
|
|
74
|
+
Matching ignores case and accepts canonical hyphens or uninterrupted hexadecimal digits.
|
|
75
|
+
An ambiguous prefix lists candidate IDs and requires more characters.
|
|
76
|
+
With --json, output is one full activity record, with parsed changes or null.
|
|
77
|
+
id identifies this event itself, while entity_type and entity_id identify its affected entity.
|
|
78
|
+
|
|
79
|
+
Example: zinn activity view 019abcdf-1234-7`,
|
|
80
|
+
},
|
|
81
|
+
} satisfies RouteDef;
|
|
@@ -3,12 +3,13 @@ import type { Command } from "../types";
|
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
4
|
import { backup } from "@zinn-dev/core";
|
|
5
5
|
|
|
6
|
-
import { output } from "../lib";
|
|
6
|
+
import { output, validatePositionals } from "../lib";
|
|
7
7
|
import packageJson from "../../package.json";
|
|
8
8
|
|
|
9
9
|
export const backupCommand = {
|
|
10
10
|
run: (args) => {
|
|
11
|
-
parseArgs({ args, strict: true, allowPositionals:
|
|
11
|
+
const { positionals } = parseArgs({ args, strict: true, allowPositionals: true });
|
|
12
|
+
validatePositionals(positionals, []);
|
|
12
13
|
|
|
13
14
|
const created = backup.create({ client: "cli", version: packageJson.version });
|
|
14
15
|
|