52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { test } from "node:test";
|
|
|
|
import { loadSpotifyConfig, resolveSpotifyPaths } from "../src/config.js";
|
|
|
|
test("uses SPOTIFY_CONFIG_DIR when it exists", async () => {
|
|
const root = await mkdtemp(join(tmpdir(), "spotify-config-"));
|
|
await writeFile(join(root, "config.json"), JSON.stringify({ clientId: "id", redirectUri: "http://127.0.0.1:8888/callback" }));
|
|
|
|
const paths = await resolveSpotifyPaths({ env: { SPOTIFY_CONFIG_DIR: root }, homeDir: "/missing-home" });
|
|
|
|
assert.equal(paths.configDir, root);
|
|
assert.equal(paths.configPath, join(root, "config.json"));
|
|
assert.equal(paths.tokenPath, join(root, "token.json"));
|
|
});
|
|
|
|
test("loads and validates config json", async () => {
|
|
const root = await mkdtemp(join(tmpdir(), "spotify-config-"));
|
|
await writeFile(join(root, "config.json"), JSON.stringify({ clientId: "client", redirectUri: "http://127.0.0.1:8888/callback" }));
|
|
|
|
const config = await loadSpotifyConfig({ env: { SPOTIFY_CONFIG_DIR: root }, homeDir: "/missing-home" });
|
|
|
|
assert.deepEqual(config, {
|
|
clientId: "client",
|
|
redirectUri: "http://127.0.0.1:8888/callback"
|
|
});
|
|
});
|
|
|
|
test("finds upward clawdbot credentials", async () => {
|
|
const root = await mkdtemp(join(tmpdir(), "spotify-upward-"));
|
|
const configDir = join(root, ".clawdbot", "credentials", "spotify");
|
|
const nested = join(root, "a", "b");
|
|
await mkdir(configDir, { recursive: true });
|
|
await mkdir(nested, { recursive: true });
|
|
|
|
const paths = await resolveSpotifyPaths({ env: {}, startDir: nested, homeDir: "/missing-home" });
|
|
|
|
assert.equal(paths.configDir, configDir);
|
|
});
|
|
|
|
test("rejects missing config file", async () => {
|
|
const root = await mkdtemp(join(tmpdir(), "spotify-config-"));
|
|
|
|
await assert.rejects(
|
|
() => loadSpotifyConfig({ env: { SPOTIFY_CONFIG_DIR: root }, homeDir: "/missing-home" }),
|
|
/Spotify config not found/
|
|
);
|
|
});
|