Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: formatDuration not formatting hours #3

Merged
merged 5 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm run test
- name: Build
run: npm run build
- name: Build docs
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist
/lib
/logs
.eslintcache
/coverage

# local env files
.env
Expand Down
1,834 changes: 1,251 additions & 583 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:serve": "vitepress serve docs",
"test": "vitest --coverage",
"update": "npx npm-check-updates -i",
"prepare": "husky install"
},
Expand Down Expand Up @@ -53,6 +54,7 @@
"@types/node": "^17.0.41",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"c8": "^7.11.3",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
Expand All @@ -61,7 +63,8 @@
"prettier": "^2.6.2",
"tiny-typed-emitter": "^2.1.0",
"typescript": "^4.7.3",
"vitepress": "^1.0.0-alpha.2"
"vitepress": "^1.0.0-alpha.2",
"vitest": "^0.15.1"
},
"release": {
"branches": [
Expand Down
1 change: 1 addition & 0 deletions src/commands/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const createRepeatCommand: CreateCommandFunc = (
description: playerManager.translations.repeat.modeDescription,
required: true,
choices: Object.entries(PlayerRepeatMode)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.filter(([_, value]) => typeof value === "number")
.map<ApplicationCommandOptionChoiceData>(([key, value]) => {
return {
Expand Down
52 changes: 52 additions & 0 deletions src/utils/fs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import { isSubPath } from "./fs";

interface Path {
root: string;
subpath: string;
}

describe("fs util", () => {
it("recognized sub paths", () => {
const validPaths: Path[] = [
{
root: "/",
subpath: "test.mp3",
},
{
root: "",
subpath: "test.mp3",
},
{
root: "/public",
subpath: "/public/test.mp3",
},
{
root: "/public",
subpath: "/public/sub/test.mp3",
},
];
const invalidPaths: Path[] = [
{
root: "/public",
subpath: "test.mp3",
},
{
root: "/public",
subpath: "/etc",
},
{
root: "/public/test",
subpath: "public/test.mp3",
},
];

validPaths.forEach((path) => {
expect(isSubPath(path.root, path.subpath)).toBe(true);
});

invalidPaths.forEach((path) => {
expect(isSubPath(path.root, path.subpath)).toBe(false);
});
});
});
89 changes: 89 additions & 0 deletions src/utils/player.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, expect, it } from "vitest";
import { Track } from "../types/engines";
import {
formatDuration,
shuffle,
trackToMarkdown,
urlToMarkdown,
validateVolume,
} from "../utils/player";

describe("player util", () => {
it("shuffles array", () => {
const original = [1, 2, 3, 4, 5, 6, 7, 8];
const arr = original.slice();
const shuffled = shuffle(arr);

expect(arr).toBe(shuffled);
expect(arr.length).toBe(shuffled.length);
expect(arr.join(",")).not.toBe(original.join(","));
});

it("validates volume", () => {
const testCases = [
{ input: -1, expected: false },
{ input: 0, expected: true },
{ input: 1, expected: true },
{ input: 200, expected: true },
{ input: 201, expected: false },
];

testCases.forEach((c) => {
expect(validateVolume(c.input)).toBe(c.expected);
});
});

it("formats track to markdown", () => {
const track: Track = {
title: "Example track",
duration: 61,
source: "file",
url: "https://example.com",
};

expect(trackToMarkdown(track)).toBe(
"[Example track](https://example.com) (01:01)"
);

expect(trackToMarkdown({ ...track, artist: "Test artist" })).toBe(
"[Example track](https://example.com) (01:01), Test artist"
);

expect(trackToMarkdown(track, true)).toBe(
"[Example track](<https://example.com>) (01:01)"
);

expect(trackToMarkdown({ ...track, artist: "Test artist" }, true)).toBe(
"[Example track](<https://example.com>) (01:01), Test artist"
);
});

it("formats url to markdown", () => {
expect(urlToMarkdown("Example track", "https://example.com")).toBe(
"[Example track](https://example.com)"
);

expect(urlToMarkdown("Example track", "https://example.com", true)).toBe(
"[Example track](<https://example.com>)"
);
});

it("formats track duration", () => {
const testCases = [
{ input: 0, expected: "00:00" },
{ input: 4, expected: "00:04" },
{ input: 4.123, expected: "00:04" },
{ input: 42, expected: "00:42" },
{ input: 60, expected: "01:00" },
{ input: 90, expected: "01:30" },
{ input: 600, expected: "10:00" },
{ input: 3599, expected: "59:59" },
{ input: 3661, expected: "01:01:01" },
{ input: 360061, expected: "100:01:01" },
];

testCases.forEach((c) => {
expect(formatDuration(c.input)).toBe(c.expected);
});
});
});
12 changes: 10 additions & 2 deletions src/utils/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ export function urlToMarkdown(
}

export function formatDuration(durationInSecs: number): string {
const minutes = Math.floor(durationInSecs / 60);
const seconds = durationInSecs % 60;
const seconds = Math.floor(durationInSecs % 60);
const minutes = Math.floor((durationInSecs / 60) % 60);
const hours = Math.floor(durationInSecs / 3600);

if (hours) {
return `${hours.toString().padStart(2, "0")}:${minutes
.toString()
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}

return `${minutes.toString().padStart(2, "0")}:${seconds
.toString()
.padStart(2, "0")}`;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"]
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.ts"]
}