Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit b4afe1a

Browse files
authored
Add tests (#24)
1 parent 34f48c4 commit b4afe1a

33 files changed

+1870
-402
lines changed

.changeset/lazy-lobsters-whisper.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@dandori/core": patch
3+
"@dandori/libs": patch
4+
"@dandori/cli": patch
5+
"@dandori/ui": patch
6+
---
7+
8+
Add tests

.eslintrc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"env": {
3-
"node": true,
4-
"jest": true
3+
"node": true
54
},
65
"extends": [
76
"eslint:recommended",
@@ -11,7 +10,8 @@
1110
"prettier"
1211
],
1312
"plugins": [
14-
"unused-imports"
13+
"unused-imports",
14+
"vitest"
1515
],
1616
"parser": "@typescript-eslint/parser",
1717
"parserOptions": {

.github/actions/setup/action.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Setup
2+
description: "Setup Node.js, pnpm and dependencies"
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
- name: Setup pnpm
8+
uses: pnpm/action-setup@v2
9+
- name: Setup Node
10+
uses: actions/setup-node@v3
11+
with:
12+
node-version-file: '.node-version'
13+
cache: 'pnpm'
14+
- name: Install Dependencies
15+
run: pnpm install --frozen-lockfile
16+
shell: bash

.github/workflows/release.yaml

+1-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ jobs:
1818
steps:
1919
- name: Checkout Repo
2020
uses: actions/checkout@v3
21-
- name: Setup pnpm
22-
uses: pnpm/action-setup@v2
23-
- name: Setup Node.js
24-
uses: actions/setup-node@v3
25-
with:
26-
node-version-file: '.node-version'
27-
cache: 'pnpm'
28-
- name: Install Packages
29-
run: pnpm install
21+
- uses: ./.github/actions/setup
3022
- name: Build Packages
3123
run: pnpm build
3224
- name: Create Release Pull Request or Publish to npm

.github/workflows/test.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- '*'
7+
- '!main'
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 180
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v3
16+
- uses: ./.github/actions/setup
17+
- name: Test
18+
run: pnpm t

.node-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18.18.0
1+
18.18.1

package.json

+15-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
"scripts": {
1111
"build": "turbo build",
1212
"prepare": "husky install",
13-
"publish-packages": "pnpm build && changeset version && changeset publish"
13+
"test": "turbo test"
1414
},
15-
"keywords": [],
15+
"keywords": [
16+
"javascript",
17+
"typescript",
18+
"chatgpt",
19+
"openai",
20+
"gpt-4",
21+
"gpt-3"
22+
],
1623
"author": "Hiroki Miyaji",
1724
"license": "MIT",
1825
"devDependencies": {
@@ -26,18 +33,19 @@
2633
"eslint-import-resolver-typescript": "^3.6.1",
2734
"eslint-plugin-import": "2.28.1",
2835
"eslint-plugin-unused-imports": "3.0.0",
36+
"eslint-plugin-vitest": "^0.3.8",
2937
"husky": "^8.0.3",
3038
"lint-staged": "^14.0.1",
31-
"npm-run-all": "^4.1.5",
3239
"prettier": "3.0.3",
3340
"tsup": "^7.2.0",
34-
"tsx": "^3.13.0",
35-
"turbo": "^1.10.15",
36-
"typescript": "5.2.2"
41+
"tsx": "^3.14.0",
42+
"turbo": "^1.10.16",
43+
"typescript": "5.2.2",
44+
"vitest": "^0.34.6"
3745
},
3846
"packageManager": "[email protected]",
3947
"engines": {
40-
"node": ">=18.17.1",
48+
"node": ">=18.18.1",
4149
"pnpm": "8.9.2"
4250
},
4351
"husky": {

packages/cli/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
"type": "module",
2020
"scripts": {
2121
"build": "tsup --config ../../tsup.config.ts",
22-
"dev:core": "tsx ./src/core.ts",
23-
"dev:miro": "tsx ./src/miro.ts ./tmp/test.md"
22+
"test": "vitest run",
23+
"dev:core": "tsx src/core/cli.ts",
24+
"dev:miro": "tsx src/miro/cli.ts"
2425
},
2526
"bin": {
26-
"dandori": "./dist/core.js",
27-
"dandori-miro": "./dist/miro.js"
27+
"dandori": "./dist/core/cli.js",
28+
"dandori-miro": "./dist/miro/cli.js"
2829
},
2930
"keywords": [],
3031
"author": "Hiroki Miyaji",

packages/cli/src/core.ts

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { rm, writeFile } from "fs/promises";
2+
import {
3+
describe,
4+
beforeEach,
5+
beforeAll,
6+
afterEach,
7+
afterAll,
8+
vi,
9+
expect,
10+
it,
11+
} from "vitest";
12+
import DandoriCoreCli from "../index";
13+
import generateDandoriTasks, { DandoriTask } from "@dandori/core";
14+
15+
const tasks: DandoriTask[] = [
16+
{
17+
id: "1",
18+
name: "task1",
19+
deadline: "2021-01-01",
20+
description: "task1-description",
21+
fromTaskIdList: [],
22+
},
23+
];
24+
25+
vi.mock("@dandori/core", () => ({
26+
default: vi.fn(() => tasks),
27+
}));
28+
29+
describe("DandoriCoreCli", () => {
30+
const mockConsole = vi.spyOn(console, "log");
31+
const inputFileName = "DandoriCoreCli.txt";
32+
const inputFileText = "DandoriCoreCli";
33+
const loadProcessArgv = (options: string[]) => {
34+
process.argv = ["node", "cli.js", inputFileName, ...options];
35+
};
36+
37+
beforeAll(async () => {
38+
await writeFile(inputFileName, inputFileText);
39+
});
40+
41+
afterAll(async () => {
42+
await rm(inputFileName);
43+
});
44+
45+
afterEach(() => {
46+
process.argv = [];
47+
vi.clearAllMocks();
48+
});
49+
50+
describe("without options", () => {
51+
beforeEach(async () => {
52+
loadProcessArgv([]);
53+
await new DandoriCoreCli().run();
54+
});
55+
56+
it("call generateDandoriTasks", () => {
57+
expect(generateDandoriTasks).toHaveBeenCalledWith(inputFileText, {
58+
envFilePath: undefined,
59+
});
60+
});
61+
62+
it("call console.log", () => {
63+
expect(mockConsole).toHaveBeenCalledWith(JSON.stringify(tasks, null, 2));
64+
});
65+
});
66+
67+
describe("with -e option", () => {
68+
const envFilePath = "/test/.env";
69+
70+
beforeEach(async () => {
71+
loadProcessArgv(["-e", envFilePath]);
72+
await new DandoriCoreCli().run();
73+
});
74+
75+
it("call generateDandoriTasks with envFilePath", () => {
76+
expect(generateDandoriTasks).toHaveBeenCalledWith(inputFileText, {
77+
envFilePath,
78+
});
79+
});
80+
});
81+
});

packages/cli/src/core/cli.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
3+
import DandoriCoreCli from "./index";
4+
5+
const cli = new DandoriCoreCli();
6+
await cli.run();
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
import packageJson from "../package.json";
1+
import packageJson from "../../package.json";
22
import { Command } from "commander";
33
import chalk from "chalk";
44
import generateDandoriTasks, { DandoriTask } from "@dandori/core";
55
import { readFile } from "fs/promises";
6-
import { generateDandoriFilePath, logger } from "@dandori/libs";
6+
import { generateDandoriFilePath } from "@dandori/libs";
77

8-
export class DandoriBaseCli {
9-
private program: Command;
8+
export default class DandoriCoreCli {
109
private inputFile: string = "";
10+
protected program: Command;
1111

1212
constructor() {
1313
this.program = this.buildCommand();
1414
}
1515

16-
async generateDandoriTasks(): Promise<DandoriTask[]> {
17-
this.program.parse(process.argv);
18-
const opts = this.program.opts();
19-
const source = await readFile(generateDandoriFilePath(this.inputFile));
20-
const { envFile } = opts;
21-
const tasks = await generateDandoriTasks(source.toString(), {
22-
envFilePath: envFile,
23-
});
24-
if (!tasks) {
25-
logger.error("Failed to generate dandori tasks.");
26-
process.exit(1);
27-
}
28-
return tasks;
16+
public async run(): Promise<void> {
17+
const tasks = await this.generateDandoriTasks();
18+
const jsonStringTasks = JSON.stringify(tasks, null, 2);
19+
console.log(jsonStringTasks);
2920
}
3021

3122
protected buildCommand(): Command {
@@ -38,4 +29,14 @@ export class DandoriBaseCli {
3829
this.inputFile = iFile;
3930
});
4031
}
32+
33+
protected async generateDandoriTasks(): Promise<DandoriTask[]> {
34+
this.program.parse(process.argv);
35+
const opts = this.program.opts();
36+
const source = await readFile(generateDandoriFilePath(this.inputFile));
37+
const { envFile } = opts;
38+
return generateDandoriTasks(source.toString(), {
39+
envFilePath: envFile,
40+
});
41+
}
4142
}

packages/cli/src/miro.ts

-17
This file was deleted.

0 commit comments

Comments
 (0)