From 76b376c2fae10eabc184b7e41366bde5e2bf4bcd Mon Sep 17 00:00:00 2001 From: Sauvage666 Date: Fri, 19 Jun 2026 23:05:32 +0200 Subject: [PATCH 1/2] Add count command to display total number of notes Adds `node notes.js count` which prints "You have N notes." (with correct singular/plural). Also updates the default help message to include the new command. Co-Authored-By: Claude Sonnet 4.6 --- notes.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/notes.js b/notes.js index df64a18..df1c212 100644 --- a/notes.js +++ b/notes.js @@ -45,8 +45,13 @@ function main() { console.log(ok ? `Deleted note #${id}` : `No note #${id} found`); break; } + case "count": { + const total = store.all().length; + console.log(`You have ${total} note${total === 1 ? "" : "s"}.`); + break; + } default: - console.log("Commands: add | list | search | delete "); + console.log("Commands: add | list | search | delete | count"); console.log(`(Session locks after ${config.SESSION_TIMEOUT_MINUTES} minutes of inactivity.)`); } } From af5fffdc5e073ec7f25991f089eea9b5d0945a4f Mon Sep 17 00:00:00 2001 From: Sauvage666 Date: Fri, 19 Jun 2026 23:16:18 +0200 Subject: [PATCH 2/2] Add edit command with input validation and tests Adds `node notes.js edit ` to update the text of an existing note. Guards against missing ID or empty text (usage hint), unknown IDs (returns null instead of crashing), and updates the help message. Includes two integration tests for the happy path and the not-found case. Co-Authored-By: Claude Sonnet 4.6 --- lib/store.js | 11 ++++++++++- notes.js | 13 ++++++++++++- tests/notes.test.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/store.js b/lib/store.js index 661b4b2..74c0932 100644 --- a/lib/store.js +++ b/lib/store.js @@ -45,4 +45,13 @@ function search(term) { return matches(load().notes, term); } -module.exports = { all, add, remove, search, matches }; +function edit(id, newText) { + const data = load(); + const note = data.notes.find(n => n.id === id); + if (!note) return null; + note.text = newText; + save(data); + return note; +} + +module.exports = { all, add, remove, search, matches, edit }; diff --git a/notes.js b/notes.js index df1c212..c0b128e 100644 --- a/notes.js +++ b/notes.js @@ -45,13 +45,24 @@ function main() { console.log(ok ? `Deleted note #${id}` : `No note #${id} found`); break; } + case "edit": { + const id = Number(rest[0]); + const text = rest.slice(1).join(" ").trim(); + if (!id || !text) { + console.log("Usage: notes edit "); + return; + } + const note = store.edit(id, text); + console.log(note ? `Updated note #${id}: ${note.text}` : `No note #${id} found`); + break; + } case "count": { const total = store.all().length; console.log(`You have ${total} note${total === 1 ? "" : "s"}.`); break; } default: - console.log("Commands: add | list | search | delete | count"); + console.log("Commands: add | list | search | delete | edit | count"); console.log(`(Session locks after ${config.SESSION_TIMEOUT_MINUTES} minutes of inactivity.)`); } } diff --git a/tests/notes.test.js b/tests/notes.test.js index f0a31d8..7998baa 100644 --- a/tests/notes.test.js +++ b/tests/notes.test.js @@ -1,7 +1,20 @@ const test = require("node:test"); const assert = require("node:assert"); +const fs = require("fs"); +const path = require("path"); -const { matches } = require("../lib/store"); +const { matches, edit } = require("../lib/store"); + +const NOTES_FILE = path.join(__dirname, "..", "notes.json"); + +function withCleanStore(notes, fn) { + const prev = fs.existsSync(NOTES_FILE) ? fs.readFileSync(NOTES_FILE) : null; + const nextId = notes.length ? Math.max(...notes.map(n => n.id)) + 1 : 1; + fs.writeFileSync(NOTES_FILE, JSON.stringify({ nextId, notes }, null, 2)); + try { return fn(); } finally { + prev ? fs.writeFileSync(NOTES_FILE, prev) : fs.unlinkSync(NOTES_FILE); + } +} const notes = [ { id: 1, text: "buy milk" }, @@ -24,3 +37,18 @@ test("search returns nothing when no note contains the term", () => { const result = matches(notes, "xyz"); assert.strictEqual(result.length, 0); }); + +test("edit updates the text of an existing note", () => { + withCleanStore([{ id: 1, text: "original" }], () => { + const updated = edit(1, "revised"); + assert.strictEqual(updated.id, 1); + assert.strictEqual(updated.text, "revised"); + }); +}); + +test("edit returns null when the note id does not exist", () => { + withCleanStore([{ id: 1, text: "original" }], () => { + const result = edit(999, "revised"); + assert.strictEqual(result, null); + }); +});