Skip to content

Commit

Permalink
fix: add missing return types, add some basic tests
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Dec 16, 2024
1 parent 7ae0427 commit dcce169
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 135 deletions.
28 changes: 19 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
name: Test

on: [push]
on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
branches:
- main

jobs:
test:
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork == false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Use Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 20

- uses: oven-sh/setup-bun@v1
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Install Dependencies & Build
run: bun install && bun run build

- name: Run Tests
run: bun run test
env:
SUBSONIC_URL: ${{ secrets.SUBSONIC_URL }}
SUBSONIC_USER: ${{ secrets.SUBSONIC_USER }}
SUBSONIC_PASS: ${{ secrets.SUBSONIC_PASS }}
run: bun test --timeout 10000
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ lib
.nyc_output

.pnp.*
.pnpm-debug.log
.pnpm-debug.log
.env.test
3 changes: 0 additions & 3 deletions __tests__/basic.test.ts

This file was deleted.

109 changes: 46 additions & 63 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,48 @@
{
"name": "subsonic-api",
"version": "3.0.0",
"description": "Subsonic and OpenSubsonic Client for Node.js and browsers",
"license": "MIT",
"author": "Henry Gressmann <mail@henrygressmann.de>",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/explodingcamera/subsonic-api.git"
},
"keywords": [
"subsonic",
"api",
"music",
"streaming",
"media",
"player",
"client",
"typescript"
],
"homepage": "https://github.com/explodingcamera/subsonic-api",
"files": [
"lib",
"README.md",
"LICENSE.md"
],
"exports": {
".": "./lib/index.js"
},
"scripts": {
"build": "bun run build:js && bun run build:types",
"build:js": "esbuild ./src/index.ts --minify --target=es2022 --outdir=lib --external:node:crypto --format=esm --bundle",
"build:types": "tsc -p tsconfig.build.json",
"test": "bun test",
"release": "release-it",
"preversion": "npm run build",
"format": "biome format ./src --write",
"lint": "biome check ./src"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"bun-types": "^1.1.38",
"esbuild": "^0.24.0",
"release-it": "^17.10.0"
},
"release-it": {
"git": {
"commitMessage": "chore: release v${version}"
},
"github": {
"release": true
}
},
"engines": {
"node": ">=18"
},
"engineStrict": false,
"trustedDependencies": [
"@biomejs/biome",
"esbuild"
]
"name": "subsonic-api",
"version": "3.0.0",
"description": "Subsonic and OpenSubsonic Client for Node.js and browsers",
"license": "MIT",
"author": "Henry Gressmann <mail@henrygressmann.de>",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/explodingcamera/subsonic-api.git"
},
"keywords": ["subsonic", "api", "music", "streaming", "media", "player", "client", "typescript"],
"homepage": "https://github.com/explodingcamera/subsonic-api",
"files": ["lib", "README.md", "LICENSE.md"],
"exports": {
".": "./lib/index.js"
},
"scripts": {
"build": "bun run build:js && bun run build:types",
"build:js": "esbuild ./src/index.ts --minify --target=es2022 --outdir=lib --external:node:crypto --format=esm --bundle",
"build:types": "tsc -p tsconfig.build.json",
"release": "release-it",
"preversion": "npm run build",
"format": "biome format ./src --write",
"lint": "biome check ./src"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"bun-types": "^1.1.38",
"esbuild": "^0.24.0",
"release-it": "^17.10.0"
},
"release-it": {
"git": {
"commitMessage": "chore: release v${version}"
},
"github": {
"release": true
}
},
"engines": {
"node": ">=18"
},
"engineStrict": false,
"trustedDependencies": ["@biomejs/biome", "esbuild"]
}
107 changes: 107 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { beforeAll, describe, expect, test } from "bun:test";

import { SubsonicAPI } from ".";

let api: SubsonicAPI;
describe("basic", () => {
beforeAll(() => {
api = new SubsonicAPI({
url: process.env.SUBSONIC_URL!,
auth: {
password: process.env.SUBSONIC_PASS!,
username: process.env.SUBSONIC_USER!,
},
});
});

test("ping", async () => {
const { status } = await api.ping();
expect(status).toBe("ok");
});

test("basic usage", async () => {
// Fetch music folders
const { musicFolders } = await api.getMusicFolders();
expect(musicFolders.musicFolder).toBeDefined();
const folderId = musicFolders.musicFolder?.[0]?.id;
expect(folderId).toBeDefined();

// Fetch artists from the first music folder
const { artists } = await api.getArtists({ musicFolderId: folderId! });
expect(artists.index?.length).toBeGreaterThan(0);
const artistId = artists.index?.[0]?.artist?.[0].id;
expect(artistId).toBeDefined();

// Fetch albums for the first artist
const { artist } = await api.getArtist({ id: artistId! });
expect(artist.album?.length).toBeGreaterThan(0);
const albumId = artist?.album?.[0]?.id;
expect(albumId).toBeDefined();

// Fetch songs from the first album
const albumInfo = await api.getAlbum({ id: albumId! });
expect(albumInfo.album.songCount).toBeGreaterThan(0);
const songId = albumInfo.album.song?.[0].id;
expect(songId).toBeDefined();

// Star the song
const starResponse = await api.star({ id: songId });
expect(starResponse.status).toBe("ok");

// Unstar the song
const unstarResponse = await api.unstar({ id: songId });
expect(unstarResponse.status).toBe("ok");
});

test("additional API usage scenarios", async () => {
// Fetch random songs
const { randomSongs } = await api.getRandomSongs();
expect(randomSongs.song?.length).toBeGreaterThan(0);
const randomSongId = randomSongs.song?.[0]?.id;
expect(randomSongId).toBeDefined();

// Fetch a song by ID
const songInfo = await api.getSong({ id: randomSongId! });

expect(songInfo.song.id).toBe(randomSongId!);
expect(songInfo.song.title).toBeDefined();

// Check user favorites
const { starred } = await api.getStarred();
expect(starred.song?.length).toBeDefined();

// Create and delete a playlist
const { playlist } = await api.createPlaylist({
name: "Test Playlist",
songId: [randomSongId!],
});
expect(playlist?.id).toBeDefined();

const playlistInfo = await api.getPlaylist({ id: playlist?.id! });
expect(playlistInfo.playlist.entry?.length).toBeGreaterThan(0);

const deleteResponse = await api.deletePlaylist({ id: playlist?.id! });
expect(deleteResponse.status).toBe("ok");

// Check server ping
const pingResponse = await api.ping();
expect(pingResponse.status).toBe("ok");

// Get music folder details
const { musicFolders } = await api.getMusicFolders();
const folderId = musicFolders.musicFolder?.[0]?.id;
const folderDetails = await api.getIndexes({ musicFolderId: folderId! });
expect(folderDetails.indexes?.index?.length).toBeGreaterThan(0);

// Test searching
const { searchResult2 } = await api.search2({ query: "" });
expect(searchResult2.song?.length).toBeGreaterThan(0);
expect(searchResult2.album?.length).toBeGreaterThan(0);
expect(searchResult2.artist?.length).toBeGreaterThan(0);

const { searchResult3 } = await api.search3({ query: "" });
expect(searchResult3.song?.length).toBeGreaterThan(0);
expect(searchResult3.album?.length).toBeGreaterThan(0);
expect(searchResult3.artist?.length).toBeGreaterThan(0);
});
});
Loading

0 comments on commit dcce169

Please sign in to comment.