From bc89c553b7db458ec5c5ecbe98b357d441c94962 Mon Sep 17 00:00:00 2001 From: saopig1 Date: Mon, 30 Mar 2026 00:26:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20address=20Phase=201=20review=20=E2=80=94?= =?UTF-8?q?=20union=20types,=20auto=20playedAt,=20ESM=20imports,=20gitigno?= =?UTF-8?q?re=20config.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 (1M context) --- .gitignore | 1 + src/data/config.test.ts | 3 +-- src/data/config.ts | 4 ++-- src/data/database.test.ts | 4 +--- src/data/database.ts | 17 ++++++++++------- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 900d237..c3c8a8b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dist/ /data/ *.db .env +config.json .superpowers/ diff --git a/src/data/config.test.ts b/src/data/config.test.ts index fc398a4..b9ad265 100644 --- a/src/data/config.test.ts +++ b/src/data/config.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect, afterEach } from "vitest"; import { join } from "node:path"; -import { mkdtempSync, rmSync } from "node:fs"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { getDefaultConfig, loadConfig, saveConfig } from "./config.js"; @@ -41,7 +41,6 @@ describe("config", () => { // Save a partial config by writing only some fields const partial = { webPort: 8080, locale: "en" }; - const { writeFileSync } = require("node:fs"); writeFileSync(path, JSON.stringify(partial), "utf-8"); const loaded = loadConfig(path); diff --git a/src/data/config.ts b/src/data/config.ts index e760dc0..ee08a29 100644 --- a/src/data/config.ts +++ b/src/data/config.ts @@ -3,8 +3,8 @@ import { dirname } from "node:path"; export interface BotConfig { webPort: number; - locale: string; - theme: string; + locale: "zh" | "en"; + theme: "dark" | "light"; commandPrefix: string; commandAliases: Record; neteaseApiPort: number; diff --git a/src/data/database.test.ts b/src/data/database.test.ts index cdf026e..51da01a 100644 --- a/src/data/database.test.ts +++ b/src/data/database.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect, beforeEach, afterEach } from "vitest"; -import { createDatabase, type BotDatabase, type BotInstance } from "./database.js"; +import { createDatabase, type BotDatabase, type BotInstance, type PlayHistoryEntry } from "./database.js"; describe("database", () => { let botDb: BotDatabase; @@ -32,7 +32,6 @@ describe("database", () => { album: "Test Album", platform: "netease", coverUrl: "https://example.com/cover.jpg", - playedAt: new Date().toISOString(), }); botDb.addPlayHistory({ @@ -43,7 +42,6 @@ describe("database", () => { album: "Another Album", platform: "qq", coverUrl: "https://example.com/cover2.jpg", - playedAt: new Date().toISOString(), }); const history = botDb.getPlayHistory("bot1", 10); diff --git a/src/data/database.ts b/src/data/database.ts index 1de1356..75f02d7 100644 --- a/src/data/database.ts +++ b/src/data/database.ts @@ -1,14 +1,17 @@ import Database from "better-sqlite3"; -export interface PlayHistoryRecord { - id?: number; +export interface PlayHistoryEntry { botId: string; songId: string; songName: string; artist: string; album: string; - platform: string; + platform: "netease" | "qq"; coverUrl: string; +} + +export interface PlayHistoryRecord extends PlayHistoryEntry { + id: number; playedAt: string; } @@ -25,7 +28,7 @@ export interface BotInstance { export interface BotDatabase { db: Database.Database; - addPlayHistory(record: Omit): void; + addPlayHistory(entry: PlayHistoryEntry): void; getPlayHistory(botId: string, limit: number): PlayHistoryRecord[]; saveBotInstance(instance: BotInstance): void; getBotInstances(): BotInstance[]; @@ -44,7 +47,7 @@ function initTables(db: Database.Database): void { album TEXT NOT NULL, platform TEXT NOT NULL, coverUrl TEXT NOT NULL, - playedAt TEXT NOT NULL + playedAt TEXT NOT NULL DEFAULT (datetime('now')) ); CREATE TABLE IF NOT EXISTS bot_instances ( @@ -66,8 +69,8 @@ export function createDatabase(dbPath: string): BotDatabase { initTables(db); const insertHistory = db.prepare(` - INSERT INTO play_history (botId, songId, songName, artist, album, platform, coverUrl, playedAt) - VALUES (@botId, @songId, @songName, @artist, @album, @platform, @coverUrl, @playedAt) + INSERT INTO play_history (botId, songId, songName, artist, album, platform, coverUrl) + VALUES (@botId, @songId, @songName, @artist, @album, @platform, @coverUrl) `); const selectHistory = db.prepare(`