Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Jul 1, 2023
1 parent 5d07e05 commit 6ba47f1
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ module.exports = {
'no-bitwise': 'off',
'class-methods-use-this': 'off',
},
overrides: [
{
files: ['test/**/*.ts'],
rules: {
'import/no-extraneous-dependencies': 'off',
},
},
],
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flac-tagger",
"version": "1.0.2",
"version": "1.0.3",
"description": "Pure JavaScript FLAC Tag writer and reader.",
"main": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
Expand All @@ -17,7 +17,9 @@
"build": "npm-run-all -p build-esm build-cjs && tsconfig-to-dual-package",
"type-check": "tsc --noEmit",
"eslint-check": "eslint . --ext .cjs,.mjs,.ts",
"eslint-fix": "eslint . --ext .cjs,.mjs,.ts --fix"
"eslint-fix": "eslint . --ext .cjs,.mjs,.ts --fix",
"test-watch": "vitest",
"test": "vitest run"
},
"keywords": [
"nodejs",
Expand All @@ -41,7 +43,8 @@
"eslint-plugin-vue": "9.10.0",
"npm-run-all": "^4.1.5",
"tsconfig-to-dual-package": "^1.2.0",
"typescript": "5.0.4"
"typescript": "5.0.4",
"vitest": "^0.32.2"
},
"dependencies": {
"imageinfo": "^1.0.4"
Expand Down
822 changes: 822 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

Binary file added test/audio-blank.flac
Binary file not shown.
Binary file added test/audio-read.flac
Binary file not shown.
23 changes: 23 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { assert } from 'vitest'
import { readFile } from 'fs/promises'
import { FlacTags } from '../src/index'

export const coverBuffer = await readFile('./test/cover.jpg')
export const tags: FlacTags = {
tagMap: {
title: 'test-title',
artist: ['artist 1', 'artist 2'],
album: 'test-album',
albumSortOrder: 'TEST001',
},
picture: {
buffer: coverBuffer,
},
}
export const assertTags = (actualTags: FlacTags) => {
assert.equal(actualTags.tagMap.title, tags.tagMap.title)
assert.deepEqual(actualTags.tagMap.artist, tags.tagMap.artist)
assert.equal(actualTags.tagMap.album, tags.tagMap.album)
assert.equal(actualTags.tagMap.albumSortOrder, tags.tagMap.albumSortOrder)
assert.isTrue(coverBuffer.equals(actualTags.picture?.buffer))
}
Binary file added test/cover.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/read.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, test } from 'vitest'
import { readFlacTags, readFlacTagsSync } from '../src/index'
import { assertTags } from './common'

const readPath = './test/audio-read.flac'
describe('read FLAC tags', () => {
test('read async', async () => {
const actualTags = await readFlacTags(readPath)
assertTags(actualTags)
})
test('read sync', () => {
const actualTags = readFlacTagsSync(readPath)
assertTags(actualTags)
})
})
25 changes: 25 additions & 0 deletions test/write.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, test, beforeEach, afterEach } from 'vitest'
import { copyFile, unlink } from 'fs/promises'
import { writeFlacTags, readFlacTags, writeFlacTagsSync, readFlacTagsSync } from '../src/index'
import { assertTags, tags } from './common'

const sourcePath = './test/audio-blank.flac'
const writePath = './test/audio-write.flac'
beforeEach(async () => {
await copyFile(sourcePath, writePath)
})
afterEach(async () => {
await unlink(writePath)
})
describe('write FLAC tags', () => {
test('write async', async () => {
await writeFlacTags(tags, writePath)
const actualTags = await readFlacTags(writePath)
assertTags(actualTags)
})
test('write sync', () => {
writeFlacTagsSync(tags, writePath)
const actualTags = readFlacTagsSync(writePath)
assertTags(actualTags)
})
})

0 comments on commit 6ba47f1

Please sign in to comment.