From ea10718125b78419b3cc3c96469161998d1117e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=A9=98?= Date: Thu, 28 May 2026 10:23:03 +0000 Subject: [PATCH] fix(server): apply biome formatting and replace console.log with logger - Applied biome auto-fix to resolve all 5 formatting/linting errors - Fixed import sorting in index.ts, protocol.ts, and migration.test.ts - Fixed typeof expression parentheses in protocol.ts - Created logger.ts utility with createLogger function - Replaced console.log with logger.info for production code - All biome checks passing, TypeScript compilation successful - All 28 tests passing Co-Authored-By: Claude Opus 4.6 --- .../server/src/__tests__/migration.test.ts | 36 +++++-------------- packages/server/src/index.ts | 27 +++++++------- packages/server/src/logger.ts | 17 +++++++++ packages/server/src/protocol.ts | 6 ++-- 4 files changed, 40 insertions(+), 46 deletions(-) create mode 100644 packages/server/src/logger.ts diff --git a/packages/server/src/__tests__/migration.test.ts b/packages/server/src/__tests__/migration.test.ts index 0a051ff..82667da 100644 --- a/packages/server/src/__tests__/migration.test.ts +++ b/packages/server/src/__tests__/migration.test.ts @@ -1,8 +1,8 @@ -import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest"; -import { readFile, writeFile, mkdir, rm } from "node:fs/promises"; -import { join } from "node:path"; +import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; -import type { Record, Worker, Device } from "../types"; +import { join } from "node:path"; +import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; +import type { Device, Record, Worker } from "../types"; describe("Server TypeScript Migration", () => { describe("Type Safety Tests", () => { @@ -136,12 +136,7 @@ describe("Server TypeScript Migration", () => { describe("Build Configuration", () => { it("should have tsconfig.json with strict mode", async () => { - const tsconfig = JSON.parse( - await readFile( - join(process.cwd(), "tsconfig.json"), - "utf-8" - ) - ); + const tsconfig = JSON.parse(await readFile(join(process.cwd(), "tsconfig.json"), "utf-8")); expect(tsconfig.compilerOptions.strict).toBe(true); expect(tsconfig.compilerOptions.noImplicitAny).toBe(true); @@ -149,36 +144,21 @@ describe("Server TypeScript Migration", () => { }); it("should have tsconfig.json with proper module settings", async () => { - const tsconfig = JSON.parse( - await readFile( - join(process.cwd(), "tsconfig.json"), - "utf-8" - ) - ); + const tsconfig = JSON.parse(await readFile(join(process.cwd(), "tsconfig.json"), "utf-8")); expect(tsconfig.compilerOptions.module).toBe("ESNext"); expect(tsconfig.compilerOptions.moduleResolution).toBe("bundler"); }); it("should have tsconfig.json with output directory", async () => { - const tsconfig = JSON.parse( - await readFile( - join(process.cwd(), "tsconfig.json"), - "utf-8" - ) - ); + const tsconfig = JSON.parse(await readFile(join(process.cwd(), "tsconfig.json"), "utf-8")); expect(tsconfig.compilerOptions.outDir).toBe("./dist"); expect(tsconfig.compilerOptions.rootDir).toBe("./src"); }); it("should generate declaration files", async () => { - const tsconfig = JSON.parse( - await readFile( - join(process.cwd(), "tsconfig.json"), - "utf-8" - ) - ); + const tsconfig = JSON.parse(await readFile(join(process.cwd(), "tsconfig.json"), "utf-8")); expect(tsconfig.compilerOptions.declaration).toBe(true); }); diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index a6541b1..7d47551 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -3,17 +3,20 @@ import { createServer } from "node:http"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import express, { type Request, type Response } from "express"; -import { WebSocketServer, type WebSocket } from "ws"; +import { type WebSocket, WebSocketServer } from "ws"; +import { createLogger } from "./logger.js"; import { MSG, WS } from "./protocol.js"; import type { - Record, - RecordSummary, - Worker, Device, - RegisterMessage, + Record, RecordMessage, + RecordSummary, + RegisterMessage, + Worker, } from "./types.js"; +const logger = createLogger("server"); + const __dirname = dirname(fileURLToPath(import.meta.url)); const DATA_DIR = join(__dirname, "../data/records"); const FRONTEND_DIR = join(__dirname, "../../frontend/dist"); @@ -32,9 +35,7 @@ async function loadRecords(): Promise { for (const f of files) { if (!f.endsWith(".json")) continue; try { - const data = JSON.parse( - await readFile(join(DATA_DIR, f), "utf8") - ) as Record; + const data = JSON.parse(await readFile(join(DATA_DIR, f), "utf8")) as Record; records.set(data.id, data); } catch { // Ignore invalid files @@ -129,9 +130,7 @@ function broadcastWorkers(): void { const workerList = [...workers.values()]; for (const client of dashboardClients) { if (client.readyState === 1) { - client.send( - JSON.stringify({ type: MSG.WORKERS, workers: workerList }) - ); + client.send(JSON.stringify({ type: MSG.WORKERS, workers: workerList })); } } } @@ -169,9 +168,7 @@ workerWss.on("connection", (ws: WebSocket) => { for (const client of dashboardClients) { if (client.readyState === 1) { const { stdout, stderr, ...summary } = rec; - client.send( - JSON.stringify({ type: MSG.NEW_RECORD, record: summary }) - ); + client.send(JSON.stringify({ type: MSG.NEW_RECORD, record: summary })); } } } @@ -194,4 +191,4 @@ dashboardWss.on("connection", (ws: WebSocket) => { ws.on("close", () => dashboardClients.delete(ws)); }); -server.listen(PORT, () => console.log(`Dashboard server on port ${PORT}`)); +server.listen(PORT, () => logger.info(`Dashboard server on port ${PORT}`)); diff --git a/packages/server/src/logger.ts b/packages/server/src/logger.ts new file mode 100644 index 0000000..1c5ee58 --- /dev/null +++ b/packages/server/src/logger.ts @@ -0,0 +1,17 @@ +// Simple logger utility for server startup messages +// Note: @uncaged/workflow-util does not exist in this monorepo, +// so we use a minimal logger that wraps console for consistency + +export interface Logger { + info: (message: string) => void; + error: (message: string) => void; + warn: (message: string) => void; +} + +export function createLogger(name: string): Logger { + return { + info: (message: string) => console.log(`[${name}] ${message}`), + error: (message: string) => console.error(`[${name}] ${message}`), + warn: (message: string) => console.warn(`[${name}] ${message}`), + }; +} diff --git a/packages/server/src/protocol.ts b/packages/server/src/protocol.ts index 65bd4d1..e1101e5 100644 --- a/packages/server/src/protocol.ts +++ b/packages/server/src/protocol.ts @@ -16,6 +16,6 @@ export const WS = { DASHBOARD: "/ws/dashboard", } as const; -export type MessageType = typeof MSG[keyof typeof MSG]; -export type ApiEndpoint = typeof API[keyof typeof API]; -export type WebSocketEndpoint = typeof WS[keyof typeof WS]; +export type MessageType = (typeof MSG)[keyof typeof MSG]; +export type ApiEndpoint = (typeof API)[keyof typeof API]; +export type WebSocketEndpoint = (typeof WS)[keyof typeof WS];