|
| 1 | +import { test, describe, before, beforeEach, after, mock } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import os from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | + |
| 7 | +let tmpHome; |
| 8 | +let configModule; |
| 9 | +let createServer; |
| 10 | +let sentEmails; |
| 11 | + |
| 12 | +before(async () => { |
| 13 | + tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "niftycli-mcp-test-")); |
| 14 | + process.env.HOME = tmpHome; |
| 15 | + |
| 16 | + const mailerUrl = new URL("../src/mailer.js", import.meta.url).href; |
| 17 | + sentEmails = []; |
| 18 | + mock.module(mailerUrl, { |
| 19 | + namedExports: { |
| 20 | + buildTaskEmail: ({ taskName, description, status = "To Do" }) => ({ |
| 21 | + subject: `${taskName} [${status}]`, |
| 22 | + text: description || "", |
| 23 | + }), |
| 24 | + sendTaskEmail: async (smtp, toEmail, email) => { |
| 25 | + sentEmails.push({ smtp, toEmail, email }); |
| 26 | + }, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + configModule = await import("../src/config.js"); |
| 31 | + ({ createServer } = await import("../src/mcp/server.js")); |
| 32 | +}); |
| 33 | + |
| 34 | +after(() => { |
| 35 | + fs.rmSync(tmpHome, { recursive: true, force: true }); |
| 36 | +}); |
| 37 | + |
| 38 | +function callTool(server, name, args = {}) { |
| 39 | + return server._registeredTools[name].handler(args); |
| 40 | +} |
| 41 | + |
| 42 | +describe("mcp server", () => { |
| 43 | + test("niftycli_hello greets by name", async () => { |
| 44 | + const server = createServer(); |
| 45 | + const result = await callTool(server, "niftycli_hello", { name: "World" }); |
| 46 | + assert.match(result.content[0].text, /Hello, World!/); |
| 47 | + }); |
| 48 | + |
| 49 | + test("niftycli_hello greets without a name", async () => { |
| 50 | + const server = createServer(); |
| 51 | + const result = await callTool(server, "niftycli_hello"); |
| 52 | + assert.match(result.content[0].text, /^Hello! niftycli-mcp/); |
| 53 | + }); |
| 54 | + |
| 55 | + test("niftycli_status reports not configured", async () => { |
| 56 | + const server = createServer(); |
| 57 | + const result = await callTool(server, "niftycli_status"); |
| 58 | + assert.match(result.content[0].text, /not configured yet/); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("with a saved config", () => { |
| 62 | + beforeEach(() => { |
| 63 | + configModule.saveConfig({ |
| 64 | + projects: [{ name: "Website", email: "web@example.com" }], |
| 65 | + defaultProject: "Website", |
| 66 | + smtp: { host: "smtp.example.com", user: "me@example.com" }, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + test("niftycli_status reports configured projects", async () => { |
| 71 | + const server = createServer(); |
| 72 | + const result = await callTool(server, "niftycli_status"); |
| 73 | + assert.match(result.content[0].text, /niftycli is configured/); |
| 74 | + assert.match(result.content[0].text, /Website/); |
| 75 | + }); |
| 76 | + |
| 77 | + test("niftycli_list_projects lists saved projects", async () => { |
| 78 | + const server = createServer(); |
| 79 | + const result = await callTool(server, "niftycli_list_projects"); |
| 80 | + assert.match(result.content[0].text, /Website <web@example.com> \(default\)/); |
| 81 | + }); |
| 82 | + |
| 83 | + test("niftycli_add_project adds and defaults the new project", async () => { |
| 84 | + const server = createServer(); |
| 85 | + const result = await callTool(server, "niftycli_add_project", { |
| 86 | + name: "Mobile", |
| 87 | + email: "mobile@example.com", |
| 88 | + }); |
| 89 | + assert.match(result.content[0].text, /Added project "Mobile"/); |
| 90 | + const config = configModule.loadConfig(); |
| 91 | + assert.equal(config.defaultProject, "Mobile"); |
| 92 | + assert.equal(config.projects.length, 2); |
| 93 | + }); |
| 94 | + |
| 95 | + test("niftycli_add_project rejects duplicate names", async () => { |
| 96 | + const server = createServer(); |
| 97 | + await assert.rejects( |
| 98 | + callTool(server, "niftycli_add_project", { name: "Website", email: "dup@example.com" }), |
| 99 | + /already exists/, |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + test("niftycli_edit_project renames and updates email", async () => { |
| 104 | + const server = createServer(); |
| 105 | + const result = await callTool(server, "niftycli_edit_project", { |
| 106 | + currentName: "Website", |
| 107 | + newName: "Website Revamp", |
| 108 | + newEmail: "revamp@example.com", |
| 109 | + }); |
| 110 | + assert.match(result.content[0].text, /Website Revamp/); |
| 111 | + const config = configModule.loadConfig(); |
| 112 | + assert.equal(config.projects[0].name, "Website Revamp"); |
| 113 | + assert.equal(config.projects[0].email, "revamp@example.com"); |
| 114 | + assert.equal(config.defaultProject, "Website Revamp"); |
| 115 | + }); |
| 116 | + |
| 117 | + test("niftycli_edit_project throws for an unknown project", async () => { |
| 118 | + const server = createServer(); |
| 119 | + await assert.rejects( |
| 120 | + callTool(server, "niftycli_edit_project", { currentName: "Nope" }), |
| 121 | + /No project named/, |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + test("niftycli_remove_project removes the project", async () => { |
| 126 | + const server = createServer(); |
| 127 | + const result = await callTool(server, "niftycli_remove_project", { name: "Website" }); |
| 128 | + assert.match(result.content[0].text, /Removed project "Website"/); |
| 129 | + const config = configModule.loadConfig(); |
| 130 | + assert.equal(config.projects.length, 0); |
| 131 | + }); |
| 132 | + |
| 133 | + test("niftycli_create_task sends an email to the project", async () => { |
| 134 | + const server = createServer(); |
| 135 | + const result = await callTool(server, "niftycli_create_task", { |
| 136 | + project: "Website", |
| 137 | + name: "Fix bug", |
| 138 | + description: "Details here", |
| 139 | + status: "In Progress", |
| 140 | + }); |
| 141 | + assert.match(result.content[0].text, /Task "Fix bug" sent to project "Website"/); |
| 142 | + assert.equal(sentEmails.length, 1); |
| 143 | + assert.equal(sentEmails[0].toEmail, "web@example.com"); |
| 144 | + assert.equal(sentEmails[0].email.subject, "Fix bug [In Progress]"); |
| 145 | + }); |
| 146 | + |
| 147 | + test("niftycli_create_task throws for an unknown project", async () => { |
| 148 | + const server = createServer(); |
| 149 | + await assert.rejects( |
| 150 | + callTool(server, "niftycli_create_task", { project: "Nope", name: "Task" }), |
| 151 | + /No project named/, |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments