gft-pharos-mcp 0.3.0 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +593 -188
- package/package.json +1 -1
- package/test/busca-paginas.test.js +216 -0
- package/test/index.test.js +45 -37
- package/test/mastercard.test.js +290 -0
- package/test/modo-teste.test.js +60 -0
- package/test/runbook.test.js +452 -0
- package/test/um-cliente.test.js +208 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
import { isTestEnv } from "../index.js";
|
|
7
|
+
|
|
8
|
+
const INDEX = fileURLToPath(new URL("../index.js", import.meta.url));
|
|
9
|
+
|
|
10
|
+
/** Sobe o servidor de verdade e espera a resposta do initialize pelo stdio. */
|
|
11
|
+
function initializeResponde(args, env) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const child = spawn(process.execPath, [INDEX, ...args], { env, stdio: ["pipe", "pipe", "ignore"] });
|
|
14
|
+
let out = "";
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
child.kill();
|
|
17
|
+
resolve(false);
|
|
18
|
+
}, 10_000);
|
|
19
|
+
child.on("error", reject);
|
|
20
|
+
child.stdout.on("data", (chunk) => {
|
|
21
|
+
out += chunk;
|
|
22
|
+
if (out.includes('"id":1')) {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
child.kill();
|
|
25
|
+
resolve(true);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
child.on("exit", () => {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
resolve(out.includes('"id":1'));
|
|
31
|
+
});
|
|
32
|
+
child.stdin.write(
|
|
33
|
+
JSON.stringify({
|
|
34
|
+
jsonrpc: "2.0",
|
|
35
|
+
id: 1,
|
|
36
|
+
method: "initialize",
|
|
37
|
+
params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "t", version: "0" } },
|
|
38
|
+
}) + "\n"
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe("Modo de teste", () => {
|
|
44
|
+
it("só NODE_ENV=test liga o modo de teste", () => {
|
|
45
|
+
assert.equal(isTestEnv({ NODE_ENV: "test" }), true);
|
|
46
|
+
assert.equal(isTestEnv({ NODE_ENV: "production" }), false);
|
|
47
|
+
assert.equal(isTestEnv({}), false);
|
|
48
|
+
assert.equal(isTestEnv(undefined), false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("argumento com \"test\" não deixa o servidor real sem stdio", async () => {
|
|
52
|
+
// Antes, qualquer argumento com "test" (como um caminho /home/dev/contest) ligava o modo de teste.
|
|
53
|
+
const env = { ...process.env, PHAROS_API_URL: "http://127.0.0.1:9", PHAROS_TIMEOUT_MS: "300" };
|
|
54
|
+
delete env.NODE_ENV;
|
|
55
|
+
delete env.NODE_TEST_CONTEXT;
|
|
56
|
+
delete env.PHAROS_CLIENT;
|
|
57
|
+
delete env.PHAROS_API_KEY;
|
|
58
|
+
assert.equal(await initializeResponde(["/home/dev/contest"], env), true);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* HB-6: leitura do runbook pelo MCP. As chamadas passam pelo callApi de verdade, com o fetch
|
|
3
|
+
* trocado por um stub que responde como o Pharos (inclusive o 404 de corpo fixo).
|
|
4
|
+
*/
|
|
5
|
+
import { describe, it, afterEach } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
|
|
8
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
9
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
10
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
registerPharosTools,
|
|
14
|
+
ClientSession,
|
|
15
|
+
CLIENT_HEADER,
|
|
16
|
+
RUNBOOK_UNAVAILABLE_MESSAGE,
|
|
17
|
+
RUNBOOK_EMPTY_MESSAGE,
|
|
18
|
+
runbookPageNotFoundMessage,
|
|
19
|
+
runbookLocaleUnsupportedMessage,
|
|
20
|
+
runbookPageLocaleNotice,
|
|
21
|
+
isRunbookUnavailable,
|
|
22
|
+
} from "../index.js";
|
|
23
|
+
|
|
24
|
+
const RASTRO_VISA = /\bvisa\b|VISA_|verifi/i;
|
|
25
|
+
const RASTRO_MASTERCARD = /mastercard|MDES|ETHOCA|SCOF|COMMERCIAL_DIRECT/i;
|
|
26
|
+
|
|
27
|
+
/** Corpo do 404 do Pharos (RunbookNotFoundException): o mesmo para qualquer ausência. */
|
|
28
|
+
const NAO_ENCONTRADO = { status: 404, body: { title: "Nao encontrado", detail: "Runbook ou página não encontrada." } };
|
|
29
|
+
|
|
30
|
+
const ARVORE = {
|
|
31
|
+
slug: "principal",
|
|
32
|
+
title: "Runbook do time",
|
|
33
|
+
description: "Como o time integra e opera.",
|
|
34
|
+
sections: [
|
|
35
|
+
{
|
|
36
|
+
slug: "integracao",
|
|
37
|
+
title: "Integração",
|
|
38
|
+
pages: [{ slug: "tokenizacao", title: "Tokenização", locale: "pt-BR", template: "integration", publishedAt: "2026-11-27T10:00:00Z" }],
|
|
39
|
+
sections: [
|
|
40
|
+
{
|
|
41
|
+
slug: "sandbox",
|
|
42
|
+
title: "Sandbox",
|
|
43
|
+
pages: [{ slug: "certificados", title: "Certificados", locale: "en", template: null, publishedAt: "2026-11-27T10:00:00Z" }],
|
|
44
|
+
sections: [],
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
pages: [{ slug: "go-live", title: "Go-live", locale: "pt-BR", template: null, publishedAt: "2026-11-27T10:00:00Z" }],
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const PAGINA = {
|
|
53
|
+
slug: "certificados",
|
|
54
|
+
title: "Certificados",
|
|
55
|
+
locale: "en",
|
|
56
|
+
template: null,
|
|
57
|
+
path: [
|
|
58
|
+
{ slug: "integracao", title: "Integração" },
|
|
59
|
+
{ slug: "sandbox", title: "Sandbox" },
|
|
60
|
+
],
|
|
61
|
+
bodyMd: "## Passo 1\n\nGere o CSR.",
|
|
62
|
+
revision: {
|
|
63
|
+
number: 3,
|
|
64
|
+
origin: "HUMAN",
|
|
65
|
+
author: "ana",
|
|
66
|
+
coauthors: ["bruno"],
|
|
67
|
+
reviewedBy: "carla",
|
|
68
|
+
reviewedAt: "2026-11-27T09:00:00Z",
|
|
69
|
+
publishedAt: "2026-11-27T10:00:00Z",
|
|
70
|
+
},
|
|
71
|
+
sources: [
|
|
72
|
+
{ kind: "ENTRY", label: "Erro no CSR", reference: null, entryId: "0b7e1c2a-1111-4222-8333-444455556666" },
|
|
73
|
+
{ kind: "LINK", label: "Guia do sandbox", reference: "docs/sandbox.md", entryId: null },
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const originalFetch = globalThis.fetch;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Troca o fetch: `rotas` mapeia caminho (sem /api/v1 e sem query) para { status, body }. Guarda as
|
|
81
|
+
* chamadas, com a query em `search`.
|
|
82
|
+
*/
|
|
83
|
+
function stubFetch(rotas) {
|
|
84
|
+
const chamadas = [];
|
|
85
|
+
globalThis.fetch = async (url, init) => {
|
|
86
|
+
const { pathname, search } = new URL(url);
|
|
87
|
+
const path = pathname.replace(/^\/api\/v1/, "");
|
|
88
|
+
chamadas.push({ path, search, headers: init.headers });
|
|
89
|
+
const rota = rotas[path];
|
|
90
|
+
if (!rota) return new Response(JSON.stringify(NAO_ENCONTRADO.body), { status: 404 });
|
|
91
|
+
return new Response(JSON.stringify(rota.body), { status: rota.status });
|
|
92
|
+
};
|
|
93
|
+
return chamadas;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
afterEach(() => {
|
|
97
|
+
globalThis.fetch = originalFetch;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const sessaoFixa = (client) =>
|
|
101
|
+
new ClientSession(client, async () => {
|
|
102
|
+
throw new Error("não deveria descobrir");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
/** Servidor com as tools da base, usando o callApi real (call padrão). */
|
|
106
|
+
async function conectar(client) {
|
|
107
|
+
const server = new McpServer({ name: "pharos-teste", version: "0.0.0" });
|
|
108
|
+
registerPharosTools(server, { client, clientSession: sessaoFixa(client) });
|
|
109
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
110
|
+
await server.connect(serverTransport);
|
|
111
|
+
const mcp = new Client({ name: "teste", version: "0.0.0" });
|
|
112
|
+
await mcp.connect(clientTransport);
|
|
113
|
+
return mcp;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function chamar(mcp, name, args) {
|
|
117
|
+
try {
|
|
118
|
+
const r = await mcp.callTool({ name, arguments: args });
|
|
119
|
+
return { isError: Boolean(r.isError), text: r.content.map((c) => c.text).join("\n") };
|
|
120
|
+
} catch (err) {
|
|
121
|
+
return { isError: true, text: err.message };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
describe("HB-6: runbook pelo MCP", () => {
|
|
126
|
+
it("list_runbook lista seções em árvore e páginas com o slug, no cliente da sessão", async () => {
|
|
127
|
+
const chamadas = stubFetch({ "/runbook": { status: 200, body: ARVORE } });
|
|
128
|
+
const mcp = await conectar("visa");
|
|
129
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
130
|
+
|
|
131
|
+
assert.equal(r.isError, false, r.text);
|
|
132
|
+
assert.deepEqual(chamadas.map((c) => c.path), ["/runbook"]);
|
|
133
|
+
assert.equal(chamadas[0].headers[CLIENT_HEADER], "visa");
|
|
134
|
+
assert.match(r.text, /# Runbook: Runbook do time/);
|
|
135
|
+
assert.match(r.text, /- Seção: Integração\n {2}- Tokenização \[pt-BR\] \(modelo integration\): slug `tokenizacao`/);
|
|
136
|
+
assert.match(r.text, / {2}- Seção: Sandbox\n {4}- Certificados \[en\]: slug `certificados`/);
|
|
137
|
+
assert.match(r.text, /^- Go-live \[pt-BR\]: slug `go-live`$/m);
|
|
138
|
+
assert.match(r.text, /read_runbook_page/);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("list_runbook com o runbook desligado (404): texto fixo, sem erro e sem o corpo do Pharos", async () => {
|
|
142
|
+
stubFetch({ "/runbook": NAO_ENCONTRADO });
|
|
143
|
+
const mcp = await conectar("visa");
|
|
144
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
145
|
+
|
|
146
|
+
assert.equal(r.isError, false);
|
|
147
|
+
assert.equal(r.text, RUNBOOK_UNAVAILABLE_MESSAGE);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("list_runbook com Pharos que ainda não libera a rota para a chave (403): mesmo texto fixo", async () => {
|
|
151
|
+
stubFetch({
|
|
152
|
+
"/runbook": { status: 403, body: { detail: "Esta rota não aceita chave de API. Use o portal do Pharos, com login." } },
|
|
153
|
+
});
|
|
154
|
+
const mcp = await conectar("mastercard");
|
|
155
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
156
|
+
assert.equal(r.text, RUNBOOK_UNAVAILABLE_MESSAGE);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("list_runbook com runbook sem páginas publicadas: avisa que está vazio", async () => {
|
|
160
|
+
stubFetch({ "/runbook": { status: 200, body: { slug: "principal", title: "Runbook", sections: [], pages: [] } } });
|
|
161
|
+
const mcp = await conectar("visa");
|
|
162
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
163
|
+
assert.equal(r.text, RUNBOOK_EMPTY_MESSAGE);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("list_runbook não esconde outro erro: 401 segue com a instrução da chave", async () => {
|
|
167
|
+
stubFetch({ "/runbook": { status: 401, body: { detail: "API Key invalida" } } });
|
|
168
|
+
const mcp = await conectar("visa");
|
|
169
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
170
|
+
assert.equal(r.isError, true);
|
|
171
|
+
assert.match(r.text, /Pharos respondeu 401/);
|
|
172
|
+
assert.notEqual(r.text, RUNBOOK_UNAVAILABLE_MESSAGE);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("read_runbook_page devolve título, idioma, caminho, corpo, revisão e fontes", async () => {
|
|
176
|
+
const chamadas = stubFetch({ "/runbook/pages/certificados": { status: 200, body: PAGINA } });
|
|
177
|
+
const mcp = await conectar("mastercard");
|
|
178
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "certificados" });
|
|
179
|
+
|
|
180
|
+
assert.equal(r.isError, false, r.text);
|
|
181
|
+
assert.deepEqual(chamadas.map((c) => c.path), ["/runbook/pages/certificados"]);
|
|
182
|
+
assert.equal(chamadas[0].headers[CLIENT_HEADER], "mastercard");
|
|
183
|
+
assert.match(r.text, /^# Certificados$/m);
|
|
184
|
+
assert.match(r.text, /Slug: certificados \| Idioma: en \| Caminho: Integração > Sandbox/);
|
|
185
|
+
assert.match(r.text, /Revisão 3 \| origem HUMAN \| autor ana \| coautores bruno \| revisada por carla/);
|
|
186
|
+
assert.match(r.text, /## Passo 1\n\nGere o CSR\./);
|
|
187
|
+
assert.match(r.text, /- \[ENTRY\] Erro no CSR \| entrada 0b7e1c2a-1111-4222-8333-444455556666/);
|
|
188
|
+
assert.match(r.text, /- \[LINK\] Guia do sandbox \| referência: docs\/sandbox\.md/);
|
|
189
|
+
assert.match(r.text, /slug certificados, revisão 3/);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("read_runbook_page com slug que não está publicado: aponta o list_runbook", async () => {
|
|
193
|
+
const chamadas = stubFetch({ "/runbook": { status: 200, body: ARVORE } });
|
|
194
|
+
const mcp = await conectar("visa");
|
|
195
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "nao-existe" });
|
|
196
|
+
|
|
197
|
+
assert.equal(r.isError, true);
|
|
198
|
+
assert.equal(r.text, runbookPageNotFoundMessage("nao-existe"));
|
|
199
|
+
assert.deepEqual(chamadas.map((c) => c.path), ["/runbook/pages/nao-existe", "/runbook"]);
|
|
200
|
+
assert.ok(chamadas.every((c) => c.headers[CLIENT_HEADER] === "visa"));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("read_runbook_page com o runbook desligado: texto fixo, sem dizer se a página existe", async () => {
|
|
204
|
+
stubFetch({});
|
|
205
|
+
const mcp = await conectar("visa");
|
|
206
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "go-live" });
|
|
207
|
+
assert.equal(r.isError, false);
|
|
208
|
+
assert.equal(r.text, RUNBOOK_UNAVAILABLE_MESSAGE);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("read_runbook_page com slug fora do formato é recusado sem chamar a API", async () => {
|
|
212
|
+
const chamadas = stubFetch({});
|
|
213
|
+
const mcp = await conectar("visa");
|
|
214
|
+
for (const slug of ["../admin", "Go-Live", "go%2Flive", "", "-x"]) {
|
|
215
|
+
const r = await chamar(mcp, "read_runbook_page", { slug });
|
|
216
|
+
assert.equal(r.isError, true, slug);
|
|
217
|
+
assert.match(r.text, /slug inválido/);
|
|
218
|
+
}
|
|
219
|
+
assert.equal(chamadas.length, 0);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("runbook com client de outro cliente é recusado sem chamar a API", async () => {
|
|
223
|
+
const chamadas = stubFetch({});
|
|
224
|
+
const mcp = await conectar("visa");
|
|
225
|
+
const r = await chamar(mcp, "list_runbook", { client: "mastercard" });
|
|
226
|
+
assert.equal(r.isError, true);
|
|
227
|
+
assert.match(r.text, /'visa'/);
|
|
228
|
+
assert.equal(chamadas.length, 0);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("regra N3: descrições e mensagens do runbook sem marca de cliente", async () => {
|
|
232
|
+
stubFetch({});
|
|
233
|
+
// Cada sessão só pode citar o próprio cliente (o enum do client); a neutra, nenhum.
|
|
234
|
+
const rastrosProibidos = { visa: [RASTRO_MASTERCARD], mastercard: [RASTRO_VISA], neutra: [RASTRO_VISA, RASTRO_MASTERCARD] };
|
|
235
|
+
for (const [nome, proibidos] of Object.entries(rastrosProibidos)) {
|
|
236
|
+
const mcp = await conectar(nome === "neutra" ? null : nome);
|
|
237
|
+
const { tools } = await mcp.listTools();
|
|
238
|
+
const runbook = tools.filter((t) => t.name.includes("runbook"));
|
|
239
|
+
assert.equal(runbook.length, 2);
|
|
240
|
+
for (const rastro of proibidos) assert.doesNotMatch(JSON.stringify(runbook), rastro, nome);
|
|
241
|
+
for (const tool of runbook) {
|
|
242
|
+
for (const rastro of [RASTRO_VISA, RASTRO_MASTERCARD]) assert.doesNotMatch(tool.description, rastro);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
for (const texto of [
|
|
246
|
+
RUNBOOK_UNAVAILABLE_MESSAGE,
|
|
247
|
+
RUNBOOK_EMPTY_MESSAGE,
|
|
248
|
+
runbookPageNotFoundMessage("x"),
|
|
249
|
+
runbookLocaleUnsupportedMessage("fr"),
|
|
250
|
+
]) {
|
|
251
|
+
assert.doesNotMatch(texto, RASTRO_VISA);
|
|
252
|
+
assert.doesNotMatch(texto, RASTRO_MASTERCARD);
|
|
253
|
+
assert.doesNotMatch(texto, /outro cliente/i);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("isRunbookUnavailable: só o 404 e o 403 da rota fechada para a chave", () => {
|
|
258
|
+
const erro = (status, message) => Object.assign(new Error(message), { status });
|
|
259
|
+
assert.equal(isRunbookUnavailable(erro(404, "Pharos respondeu 404: x")), true);
|
|
260
|
+
assert.equal(isRunbookUnavailable(erro(403, "Pharos respondeu 403: Esta rota não aceita chave de API.")), true);
|
|
261
|
+
assert.equal(isRunbookUnavailable(erro(403, "Pharos respondeu 403: Cliente não permitido.")), false);
|
|
262
|
+
assert.equal(isRunbookUnavailable(erro(409, "x")), false);
|
|
263
|
+
assert.equal(isRunbookUnavailable(new Error("sem status")), false);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
/** Página servida em inglês, traduzida da origem em português. */
|
|
268
|
+
const traduzida = (translation) => ({
|
|
269
|
+
...PAGINA,
|
|
270
|
+
title: "Certificates",
|
|
271
|
+
locale: "en",
|
|
272
|
+
sourceLocale: "pt-BR",
|
|
273
|
+
translation,
|
|
274
|
+
translationMissing: false,
|
|
275
|
+
availableLocales: ["pt-BR", "en"],
|
|
276
|
+
bodyMd: "## Step 1\n\nGenerate the CSR.",
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const ARVORE_EN = {
|
|
280
|
+
...ARVORE,
|
|
281
|
+
locale: "en",
|
|
282
|
+
locales: ["pt-BR", "en"],
|
|
283
|
+
sections: [
|
|
284
|
+
{
|
|
285
|
+
slug: "integracao",
|
|
286
|
+
title: "Integração",
|
|
287
|
+
pages: [
|
|
288
|
+
{ slug: "tokenizacao", title: "Tokenization", locale: "en", sourceLocale: "pt-BR", translated: true, template: "integration" },
|
|
289
|
+
],
|
|
290
|
+
sections: [],
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
pages: [{ slug: "go-live", title: "Go-live", locale: "pt-BR", sourceLocale: "pt-BR", translated: false, template: null }],
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
describe("Runbook em outro idioma pelo MCP", () => {
|
|
297
|
+
it("sem locale, nada de query: o caminho fica igual ao de antes", async () => {
|
|
298
|
+
const chamadas = stubFetch({
|
|
299
|
+
"/runbook": { status: 200, body: ARVORE },
|
|
300
|
+
"/runbook/pages/certificados": { status: 200, body: PAGINA },
|
|
301
|
+
});
|
|
302
|
+
const mcp = await conectar("visa");
|
|
303
|
+
await chamar(mcp, "list_runbook", {});
|
|
304
|
+
await chamar(mcp, "read_runbook_page", { slug: "certificados" });
|
|
305
|
+
assert.deepEqual(chamadas.map((c) => c.search), ["", ""]);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("read_runbook_page repassa o locale e marca a tradução automática", async () => {
|
|
309
|
+
const chamadas = stubFetch({
|
|
310
|
+
"/runbook/pages/certificados": { status: 200, body: traduzida({ status: "MACHINE", reviewed: false, reviewedAt: null }) },
|
|
311
|
+
});
|
|
312
|
+
const mcp = await conectar("mastercard");
|
|
313
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "certificados", locale: "en" });
|
|
314
|
+
|
|
315
|
+
assert.equal(r.isError, false, r.text);
|
|
316
|
+
assert.deepEqual(chamadas.map((c) => [c.path, c.search]), [["/runbook/pages/certificados", "?locale=en"]]);
|
|
317
|
+
assert.equal(chamadas[0].headers[CLIENT_HEADER], "mastercard");
|
|
318
|
+
assert.match(r.text, /^# Certificates$/m);
|
|
319
|
+
assert.match(r.text, /Slug: certificados \| Idioma: en \|/);
|
|
320
|
+
assert.match(r.text, /^Idioma de origem: pt-BR \| Idiomas disponíveis: pt-BR, en$/m);
|
|
321
|
+
assert.match(r.text, /^Tradução automática: texto em en traduzido do original em pt-BR, ainda sem revisão/m);
|
|
322
|
+
assert.match(r.text, /confira a origem com read_runbook_page sem locale\./);
|
|
323
|
+
assert.doesNotMatch(r.text, /locale "pt-BR"/);
|
|
324
|
+
assert.doesNotMatch(r.text, /Tradução revisada/);
|
|
325
|
+
assert.match(r.text, /slug certificados, revisão 3, tradução em en/);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("read_runbook_page marca a tradução revisada, com a data", async () => {
|
|
329
|
+
stubFetch({
|
|
330
|
+
"/runbook/pages/certificados": {
|
|
331
|
+
status: 200,
|
|
332
|
+
body: traduzida({ status: "REVIEWED", reviewed: true, reviewedAt: "2026-11-29T12:00:00Z" }),
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
const mcp = await conectar("visa");
|
|
336
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "certificados", locale: "en" });
|
|
337
|
+
|
|
338
|
+
assert.equal(r.isError, false, r.text);
|
|
339
|
+
assert.match(r.text, /^Tradução revisada: texto em en traduzido do original em pt-BR e revisado por um curador em 2026-11-29T12:00:00Z\.$/m);
|
|
340
|
+
assert.doesNotMatch(r.text, /Tradução automática/);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("read_runbook_page sem tradução no idioma pedido: diz que veio a origem", async () => {
|
|
344
|
+
const chamadas = stubFetch({
|
|
345
|
+
"/runbook/pages/certificados": {
|
|
346
|
+
status: 200,
|
|
347
|
+
body: { ...PAGINA, locale: "pt-BR", sourceLocale: "pt-BR", translation: null, translationMissing: true, availableLocales: ["pt-BR"] },
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
const mcp = await conectar("visa");
|
|
351
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "certificados", locale: "en" });
|
|
352
|
+
|
|
353
|
+
assert.equal(r.isError, false, r.text);
|
|
354
|
+
assert.equal(chamadas[0].search, "?locale=en");
|
|
355
|
+
assert.match(r.text, /^Sem tradução em en: veio o texto no idioma de origem \(pt-BR\)\.$/m);
|
|
356
|
+
assert.doesNotMatch(r.text, /Tradução (automática|revisada)/);
|
|
357
|
+
assert.match(r.text, /slug certificados, revisão 3\)/);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("read_runbook_page no idioma de origem: sem aviso de tradução", async () => {
|
|
361
|
+
stubFetch({
|
|
362
|
+
"/runbook/pages/certificados": {
|
|
363
|
+
status: 200,
|
|
364
|
+
body: { ...PAGINA, locale: "pt-BR", sourceLocale: "pt-BR", translation: null, translationMissing: false, availableLocales: ["pt-BR", "en"] },
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
const mcp = await conectar("visa");
|
|
368
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "certificados", locale: "pt-BR" });
|
|
369
|
+
assert.equal(r.isError, false, r.text);
|
|
370
|
+
assert.doesNotMatch(r.text, /Tradução|Sem tradução/);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("Pharos que ignora o locale (anterior à tradução): avisa que veio a origem", () => {
|
|
374
|
+
assert.equal(
|
|
375
|
+
runbookPageLocaleNotice({ ...PAGINA, locale: "pt-BR" }, "en"),
|
|
376
|
+
"Sem tradução em en: veio o texto no idioma de origem (pt-BR)."
|
|
377
|
+
);
|
|
378
|
+
assert.equal(runbookPageLocaleNotice(PAGINA, undefined), null);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("read_runbook_page com locale e página ausente: o desempate pelo índice vai sem locale", async () => {
|
|
382
|
+
const chamadas = stubFetch({ "/runbook": { status: 200, body: ARVORE } });
|
|
383
|
+
const mcp = await conectar("visa");
|
|
384
|
+
const r = await chamar(mcp, "read_runbook_page", { slug: "nao-existe", locale: "en" });
|
|
385
|
+
|
|
386
|
+
assert.equal(r.isError, true);
|
|
387
|
+
assert.equal(r.text, runbookPageNotFoundMessage("nao-existe"));
|
|
388
|
+
assert.deepEqual(chamadas.map((c) => [c.path, c.search]), [
|
|
389
|
+
["/runbook/pages/nao-existe", "?locale=en"],
|
|
390
|
+
["/runbook", ""],
|
|
391
|
+
]);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("list_runbook repassa o locale e marca os títulos traduzidos e os sem tradução", async () => {
|
|
395
|
+
const chamadas = stubFetch({ "/runbook": { status: 200, body: ARVORE_EN } });
|
|
396
|
+
const mcp = await conectar("visa");
|
|
397
|
+
const r = await chamar(mcp, "list_runbook", { locale: "en" });
|
|
398
|
+
|
|
399
|
+
assert.equal(r.isError, false, r.text);
|
|
400
|
+
assert.deepEqual(chamadas.map((c) => [c.path, c.search]), [["/runbook", "?locale=en"]]);
|
|
401
|
+
assert.match(r.text, /^Títulos pedidos em: en \| Idiomas disponíveis: pt-BR, en$/m);
|
|
402
|
+
assert.match(r.text, /- Tokenization \[en, traduzida de pt-BR\] \(modelo integration\): slug `tokenizacao`/);
|
|
403
|
+
assert.match(r.text, /^- Go-live \[pt-BR, sem tradução em en\]: slug `go-live`$/m);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("list_runbook sem locale mostra os idiomas disponíveis quando o Pharos os manda", async () => {
|
|
407
|
+
stubFetch({ "/runbook": { status: 200, body: { ...ARVORE, locale: null, locales: ["pt-BR", "en"] } } });
|
|
408
|
+
const mcp = await conectar("visa");
|
|
409
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
410
|
+
assert.match(r.text, /^Idiomas disponíveis: pt-BR, en$/m);
|
|
411
|
+
assert.doesNotMatch(r.text, /Títulos pedidos/);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("locale fora do formato é recusado sem chamar a API, nos dois tools", async () => {
|
|
415
|
+
const chamadas = stubFetch({});
|
|
416
|
+
const mcp = await conectar("visa");
|
|
417
|
+
for (const locale of ["EN", "pt_BR", "pt-br", "english", "en&x=1", "", "en-"]) {
|
|
418
|
+
for (const [tool, args] of [
|
|
419
|
+
["list_runbook", { locale }],
|
|
420
|
+
["read_runbook_page", { slug: "go-live", locale }],
|
|
421
|
+
]) {
|
|
422
|
+
const r = await chamar(mcp, tool, args);
|
|
423
|
+
assert.equal(r.isError, true, `${tool} ${locale}`);
|
|
424
|
+
assert.match(r.text, /locale inválido/, `${tool} ${locale}`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
assert.equal(chamadas.length, 0);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("idioma no formato certo mas não suportado (400 do Pharos): erro legível, sem o corpo cru", async () => {
|
|
431
|
+
const naoSuportado = { status: 400, body: { title: "Bad Request", detail: "Idioma não suportado." } };
|
|
432
|
+
const chamadas = stubFetch({ "/runbook": naoSuportado, "/runbook/pages/go-live": naoSuportado });
|
|
433
|
+
const mcp = await conectar("visa");
|
|
434
|
+
for (const [tool, args] of [
|
|
435
|
+
["list_runbook", { locale: "fr" }],
|
|
436
|
+
["read_runbook_page", { slug: "go-live", locale: "fr" }],
|
|
437
|
+
]) {
|
|
438
|
+
const r = await chamar(mcp, tool, args);
|
|
439
|
+
assert.equal(r.isError, true, tool);
|
|
440
|
+
assert.equal(r.text, runbookLocaleUnsupportedMessage("fr"), tool);
|
|
441
|
+
}
|
|
442
|
+
assert.deepEqual(chamadas.map((c) => c.search), ["?locale=fr", "?locale=fr"]);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("400 sem locale não vira mensagem de idioma", async () => {
|
|
446
|
+
stubFetch({ "/runbook": { status: 400, body: { detail: "outra coisa" } } });
|
|
447
|
+
const mcp = await conectar("visa");
|
|
448
|
+
const r = await chamar(mcp, "list_runbook", {});
|
|
449
|
+
assert.equal(r.isError, true);
|
|
450
|
+
assert.match(r.text, /Pharos respondeu 400/);
|
|
451
|
+
});
|
|
452
|
+
});
|