Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(cli): return HTTP 404 not_found instead of 500 when updating nonexistent note (#2348)
Henry Sue38c2ea29e6d
diff --git a/src/helpers/cliBridge.js b/src/helpers/cliBridge.jsindex b550a4879..b43ca99ff 100644--- a/src/helpers/cliBridge.js+++ b/src/helpers/cliBridge.js@@ -432,7 +432,19 @@ class CliBridge {),param("PATCH", "/v1/notes/", "", "id", ({ params, body }) => {const id = requireId(params, "note");+ const existing = db.getNote(id);+ if (!existing || existing.deleted_at) {+ const err = new Error(`Note ${id} not found`);+ err.code = "NOT_FOUND";+ throw err;+ }const result = db.updateNote(id, body || {});+ // The note exists, so a remaining error names a folder or space that doesn't.+ if (result.error) {+ const err = new Error(result.error);+ err.code = "VALIDATION";+ throw err;+ }const note = unwrapMutationResult(result, "note");setImmediate(() => broadcastToWindows("note-updated", note));ipc.notifyVectorChanges();diff --git a/test/helpers/cliBridgeDictionary.test.js b/test/helpers/cliBridgeDictionary.test.jsindex f47bed72e..7359f5970 100644--- a/test/helpers/cliBridgeDictionary.test.js+++ b/test/helpers/cliBridgeDictionary.test.js@@ -33,6 +33,7 @@ process.env.NODE_ENV = "test";const DatabaseManager = require("../../src/helpers/database.js");const CliBridge = require("../../src/helpers/cliBridge.js");+const { call } = require("./harness/cliBridge.js");function isNativeBindingUnavailable(error) {const message = String(error?.message || error);@@ -66,16 +67,6 @@ function createBridge(t) {return { bridge, db, broadcasts };}-function call(bridge, method, pathname, body) {- for (const route of bridge.routes) {- if (route.method !== method) continue;- const params = route.match(pathname);- if (!params) continue;- return route.handler({ params, query: new URLSearchParams(), body });- }- throw new Error(`No route for ${method} ${pathname}`);-}-test("GET /v1/dictionary/list returns the stored words", (t) => {const ctx = createBridge(t);if (!ctx) return;diff --git a/test/helpers/cliBridgeNotes.test.js b/test/helpers/cliBridgeNotes.test.jsnew file mode 100644index 000000000..4c0c52c3c--- /dev/null+++ b/test/helpers/cliBridgeNotes.test.js@@ -0,0 +1,88 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const Module = require("node:module");++const originalLoad = Module._load;++Module._load = function patchedLoad(request, parent, isMain) {+ if (request === "./windowBroadcast") {+ return { broadcastToWindows() {} };+ }+ return originalLoad.call(this, request, parent, isMain);+};++const { createDb } = require("./harness/db.js");+const CliBridge = require("../../src/helpers/cliBridge.js");+const { call } = require("./harness/cliBridge.js");++function createBridge(t) {+ const db = createDb(t);+ if (!db) return null;++ const bridge = new CliBridge({+ databaseManager: db,+ notifyVectorChanges() {},+ _asyncMirrorWrite() {},+ });+ return { bridge, db };+}++test("PATCH /v1/notes/:id is not_found when the note does not exist", (t) => {+ const ctx = createBridge(t);+ if (!ctx) return;++ assert.throws(() => call(ctx.bridge, "PATCH", "/v1/notes/999", { title: "New Title" }), {+ code: "NOT_FOUND",+ });+});++test("PATCH /v1/notes/:id is not_found for a deleted note and leaves it unchanged", (t) => {+ const ctx = createBridge(t);+ if (!ctx) return;++ const { id } = ctx.db.saveNote("Original", "content").note;+ ctx.db.deleteNote(id);++ assert.throws(() => call(ctx.bridge, "PATCH", `/v1/notes/${id}`, { title: "Edited" }), {+ code: "NOT_FOUND",+ });+ assert.equal(ctx.db.getNote(id).title, "Original");+});++test("PATCH /v1/notes/:id is a validation error when the folder or space does not exist", (t) => {+ const ctx = createBridge(t);+ if (!ctx) return;++ const { id } = ctx.db.saveNote("Original", "content").note;++ assert.throws(() => call(ctx.bridge, "PATCH", `/v1/notes/${id}`, { folder_id: 888 }), {+ code: "VALIDATION",+ message: "Folder not found",+ });+ assert.throws(() => call(ctx.bridge, "PATCH", `/v1/notes/${id}`, { space_id: 888 }), {+ code: "VALIDATION",+ message: "Space not found",+ });+});++test("PATCH /v1/notes/:id is not_found for a non-integer id", (t) => {+ const ctx = createBridge(t);+ if (!ctx) return;++ assert.throws(() => call(ctx.bridge, "PATCH", "/v1/notes/invalid-id", { title: "New Title" }), {+ code: "NOT_FOUND",+ message: "Invalid note id",+ });+});++test("PATCH /v1/notes/:id returns the updated note", (t) => {+ const ctx = createBridge(t);+ if (!ctx) return;++ const { id } = ctx.db.saveNote("Original", "Existing content").note;+ const result = call(ctx.bridge, "PATCH", `/v1/notes/${id}`, { title: "Updated Title" });++ assert.equal(result.data.id, id);+ assert.equal(result.data.title, "Updated Title");+ assert.equal(result.data.content, "Existing content");+});diff --git a/test/helpers/cliBridgeSnippets.test.js b/test/helpers/cliBridgeSnippets.test.jsindex d79d63d74..027c091a2 100644--- a/test/helpers/cliBridgeSnippets.test.js+++ b/test/helpers/cliBridgeSnippets.test.js@@ -31,6 +31,7 @@ process.env.NODE_ENV = "test";const DatabaseManager = require("../../src/helpers/database.js");const CliBridge = require("../../src/helpers/cliBridge.js");+const { call } = require("./harness/cliBridge.js");function isNativeBindingUnavailable(error) {const message = String(error?.message || error);@@ -62,16 +63,6 @@ function createBridge(t) {return { bridge, db, broadcasts };}-function call(bridge, method, pathname, body) {- for (const route of bridge.routes) {- if (route.method !== method) continue;- const params = route.match(pathname);- if (!params) continue;- return route.handler({ params, query: new URLSearchParams(), body });- }- throw new Error(`No route for ${method} ${pathname}`);-}-const signOff = { trigger: "sign-off", replacement: "Best,\nGabe" };const ask = { trigger: "investor ask", replacement: "We are raising a seed round." };diff --git a/test/helpers/cliBridgeTranscribe.test.js b/test/helpers/cliBridgeTranscribe.test.jsindex 47b739324..b18a26551 100644--- a/test/helpers/cliBridgeTranscribe.test.js+++ b/test/helpers/cliBridgeTranscribe.test.js@@ -15,6 +15,7 @@ Module._load = function patchedLoad(request, parent, isMain) {};const CliBridge = require("../../src/helpers/cliBridge.js");+const { call } = require("./harness/cliBridge.js");const MODELS = [{ provider: "whisper", model: "base", downloaded: true, default: true },@@ -36,16 +37,6 @@ function createBridge(models = MODELS) {return { bridge, calls };}-function call(bridge, method, pathname, body) {- for (const route of bridge.routes) {- if (route.method !== method) continue;- const params = route.match(pathname);- if (!params) continue;- return route.handler({ params, query: new URLSearchParams(), body });- }- throw new Error(`No route for ${method} ${pathname}`);-}-function audioFile() {const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-cli-transcribe-"));const file = path.join(dir, "memo.wav");diff --git a/test/helpers/harness/cliBridge.js b/test/helpers/harness/cliBridge.jsnew file mode 100644index 000000000..ca61cf6d1--- /dev/null+++ b/test/helpers/harness/cliBridge.js@@ -0,0 +1,12 @@+// Dispatches a request straight to the matching CliBridge route handler, skipping HTTP.+function call(bridge, method, pathname, body) {+ for (const route of bridge.routes) {+ if (route.method !== method) continue;+ const params = route.match(pathname);+ if (!params) continue;+ return route.handler({ params, query: new URLSearchParams(), body });+ }+ throw new Error(`No route for ${method} ${pathname}`);+}++module.exports = { call };