Skip to content

Commit

Permalink
Merge pull request #229 from mittwald/task/refact-conversation-show
Browse files Browse the repository at this point in the history
Add example for command unit tests
  • Loading branch information
martin-helmich authored Feb 29, 2024
2 parents 8ae3d71 + 02ec27a commit 0a65a3a
Show file tree
Hide file tree
Showing 84 changed files with 984 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
31 changes: 26 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ jobs:
- run: yarn
- run: yarn compile

test:
licenses:
runs-on: ubuntu-latest
name: Check license compatibility
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: yarn
- run: yarn test:licenses

lint:
runs-on: ubuntu-latest
name: Run linters
steps:
Expand All @@ -25,7 +36,18 @@ jobs:
with:
node-version: 18
- run: yarn
- run: yarn test
- run: yarn test:format

tests:
runs-on: ubuntu-latest
name: Run unit tests
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: yarn
- run: yarn test:unit

readme:
name: Assert README.md is up-to-date
Expand All @@ -38,8 +60,7 @@ jobs:
node-version: 18
- run: yarn
- run: yarn compile
- run: yarn generate:readme
- run: git diff --exit-code README.md
- run: yarn test:readme

docker:
name: Build and run docker image
Expand All @@ -53,4 +74,4 @@ jobs:
- run: yarn compile
- run: npx oclif pack tarballs --targets=linux-x64
- run: docker build --build-arg PKG_SOURCE=dist -t mittwald/cli:testing .
- run: docker run --rm mittwald/cli:testing --help
- run: docker run --rm mittwald/cli:testing --help
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dist/
dist-cjs/
coverage/
*.tsbuildinfo
.nyc_output

# Other
*.log
Expand Down
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"timeout": 60000,
"node-option": [
"import=tsx",
"experimental-specifier-resolution=node"
"experimental-specifier-resolution=node",
"no-warnings"
]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"package:tarballs": "oclif pack tarballs --targets=linux-x64,linux-arm64,darwin-x64,darwin-arm64",
"package:windows": "oclif pack win",
"post:generate": "yarn run -T compile && yarn run -T compile:cjs",
"test": "yarn lint && yarn format --check && yarn test:licenses",
"test": "yarn test:format && yarn test:licenses && yarn test:unit",
"test:format": "yarn lint && yarn format --check",
"test:unit": "mocha --forbid-only \"src/**/*.test.ts\"",
"test:licenses": "yarn license-check --summary --unknown --failOn 'UNLICENSED;UNKNOWN'",
"test:readme": "yarn generate:readme && git diff --exit-code README.md"
},
Expand Down Expand Up @@ -68,10 +70,12 @@
"tempfile": "^5.0.0"
},
"devDependencies": {
"@oclif/test": "^3.1.16",
"@types/chalk": "^2.2.0",
"@types/copyfiles": "^2.4.1",
"@types/js-yaml": "^4.0.9",
"@types/marked-terminal": "^3.1.3",
"@types/mocha": "^10.0.6",
"@types/node": "^20.3.3",
"@types/node-notifier": "^8.0.2",
"@types/parse-duration": "^0.3.0",
Expand All @@ -87,12 +91,14 @@
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-prettier": "^5.1.3",
"license-checker-rseidelsohn": "^4.2.6",
"mocha": "^10.3.0",
"oclif": "^4.3.9",
"prettier": "^3.2.5",
"prettier-plugin-jsdoc": "^1.3.0",
"prettier-plugin-package": "^1.4.0",
"prettier-plugin-sort-json": "^3.1.0",
"rimraf": "^5.0.1",
"ts-node": "^10.9.2",
"tsx": "^4.7.1",
"type-fest": "^4.6.0",
"typescript": "^5.1.6"
Expand Down
80 changes: 80 additions & 0 deletions src/commands/conversation/show.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { expect, test } from "@oclif/test";
import type { MittwaldAPIV2 } from "@mittwald/api-client";

type Conversation = MittwaldAPIV2.Components.Schemas.ConversationConversation;
type Message = MittwaldAPIV2.Components.Schemas.ConversationMessage;
type StatusUpdate = MittwaldAPIV2.Components.Schemas.ConversationStatusUpdate;

describe("conversation:show", () => {
const conversationId = "186f8f22-aa0f-42bf-909d-757cb9d27b04";
const userId = "6dbd84b5-74e0-43ed-8a81-b0b8a0405a47";
const messageId = "10a59409-ff2d-478e-b07f-72c8f9f5b63f";
const now = new Date();
const user = {
userId,
clearName: "John Doe",
};

test
.nock("https://api.mittwald.de", (api) => {
api
.persist()
.get(`/v2/conversations/${conversationId}`)
.reply(200, {
conversationId,
shortId: "CONV-ID",
createdAt: now.toJSON(),
title: "Test conversation",
status: "open",
visibility: "shared",
mainUser: user,
} satisfies Conversation);

api.get(`/v2/conversations/${conversationId}/messages`).reply(200, [
{
conversationId,
type: "STATUS_UPDATE",
createdAt: now.toJSON(),
meta: { user },
messageContent: "CONVERSATION_CREATED",
},
{
messageId,
conversationId,
type: "MESSAGE",
createdAt: now.toJSON(),
createdBy: user,
messageContent: "Hello, World!",
},
{
conversationId,
type: "STATUS_UPDATE",
createdAt: now.toJSON(),
meta: { user },
messageContent: "STATUS_CLOSED",
},
] satisfies Array<Message | StatusUpdate>);
})
.env({ MITTWALD_API_TOKEN: "foo" })
.stdout()
.command(["conversation show", conversationId])
.it("shows a conversation and its messages", (ctx) => {
expect(ctx.stdout.trim()).to.equal(`Conversation metadata
─────────────────────
Title Test conversation
ID CONV-ID
Opened less than a minute ago by Unknown User
Status open
Messages
────────
CREATED, less than a minute ago
John Doe, less than a minute ago
Hello, World!
CLOSED, less than a minute ago`);
});
});
Loading

0 comments on commit 0a65a3a

Please sign in to comment.