All checks were successful
Dependabot Auto-Merge / dependabot (pull_request) Has been skipped
Dependabot Auto-Merge / devopsbot (pull_request) Has been skipped
Dependabot Auto-Merge / rennovatebot (pull_request) Has been skipped
COMMIT LINT / commitlint (pull_request) Successful in 38s
Unit Tests / unittest (pull_request) Successful in 1m19s
150 lines
3.8 KiB
JavaScript
150 lines
3.8 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const {
|
|
buildRequestPayload,
|
|
buildStoreText,
|
|
getConfig,
|
|
main,
|
|
sanitizeDocumentId,
|
|
} = require("./index");
|
|
|
|
test("getConfig defaults to the ingress API URL", () => {
|
|
const config = getConfig({
|
|
coreModule: {
|
|
getInput() {
|
|
return "";
|
|
},
|
|
},
|
|
githubModule: {
|
|
context: {
|
|
serverUrl: "https://github.example",
|
|
},
|
|
},
|
|
env: {},
|
|
});
|
|
|
|
assert.equal(config.apiUrl, "http://agent-api.k8s.private");
|
|
assert.equal(config.route, "agent-lake");
|
|
assert.equal(config.method, "embeddings_insert");
|
|
});
|
|
|
|
test("sanitizeDocumentId removes special characters and preserves periods", () => {
|
|
assert.equal(
|
|
sanitizeDocumentId("acme.docs-repo.guides/setup v2!.md"),
|
|
"acme.docsrepo.guidessetupv2.md",
|
|
);
|
|
});
|
|
|
|
test("buildStoreText prefixes repository metadata before the markdown content", () => {
|
|
const content = "# Title\n\n## First\nalpha\n\n## Second\nbeta\n";
|
|
const storeText = buildStoreText({
|
|
owner: "acme",
|
|
repo: "docs-repo",
|
|
file: "guides/setup.md",
|
|
content,
|
|
});
|
|
|
|
assert.equal(
|
|
storeText,
|
|
"repo_name: docs-repo\nrepo_owner: acme\npath: guides/setup.md\n\n# Title\n\n## First\nalpha\n\n## Second\nbeta\n",
|
|
);
|
|
});
|
|
|
|
test("buildRequestPayload sends metadata-prefixed store_text and an empty embed_text", () => {
|
|
const content = "# Title\n\n## First\nalpha\n\n## Second\nbeta\n";
|
|
const payload = buildRequestPayload({
|
|
route: "agent-lake",
|
|
method: "embeddings_insert",
|
|
segmentId: "docs.acme",
|
|
owner: "acme",
|
|
repo: "docs-repo",
|
|
file: "guides/setup.md",
|
|
content,
|
|
});
|
|
|
|
assert.equal(payload.inputs.inputs.embed_text, "");
|
|
assert.equal(
|
|
payload.inputs.inputs.store_text,
|
|
"repo_name: docs-repo\nrepo_owner: acme\npath: guides/setup.md\n\n# Title\n\n## First\nalpha\n\n## Second\nbeta\n",
|
|
);
|
|
assert.equal(
|
|
payload.inputs.inputs.document_id,
|
|
"acme.docsrepo.guidessetup.md",
|
|
);
|
|
});
|
|
|
|
test("main sends one request per file even when the document contains multiple sections", async () => {
|
|
const calls = [];
|
|
const infos = [];
|
|
const failures = [];
|
|
const content = "# Title\n\n## First\nalpha\n\n## Second\nbeta\n";
|
|
|
|
await main({
|
|
coreModule: {
|
|
getInput(name) {
|
|
const inputs = {
|
|
api_url: "https://agents.example/api",
|
|
api_token: "",
|
|
route: "agent-lake",
|
|
method: "embeddings_insert",
|
|
debug: "false",
|
|
};
|
|
return inputs[name] || "";
|
|
},
|
|
info(message) {
|
|
infos.push(message);
|
|
},
|
|
setFailed(message) {
|
|
failures.push(message);
|
|
},
|
|
},
|
|
githubModule: {
|
|
context: {
|
|
serverUrl: "https://github.example",
|
|
},
|
|
},
|
|
env: {
|
|
GITHUB_REPOSITORY: "acme/docs-repo",
|
|
GITHUB_SERVER_URL: "https://github.example",
|
|
},
|
|
executor() {
|
|
return "README.md\n";
|
|
},
|
|
fsModule: {
|
|
readFileSync(file, encoding) {
|
|
assert.equal(file, "README.md");
|
|
assert.equal(encoding, "utf8");
|
|
return content;
|
|
},
|
|
},
|
|
fetchFn: async (url, options) => {
|
|
calls.push({
|
|
url,
|
|
options,
|
|
body: JSON.parse(options.body),
|
|
});
|
|
|
|
return {
|
|
ok: true,
|
|
text: async () => "ok",
|
|
};
|
|
},
|
|
waitFn: async () => {},
|
|
});
|
|
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].url, "https://agents.example/api");
|
|
assert.equal(calls[0].body.inputs.inputs.embed_text, "");
|
|
assert.equal(
|
|
calls[0].body.inputs.inputs.store_text,
|
|
"repo_name: docs-repo\nrepo_owner: acme\npath: README.md\n\n# Title\n\n## First\nalpha\n\n## Second\nbeta\n",
|
|
);
|
|
assert.equal(
|
|
calls[0].body.inputs.inputs.document_id,
|
|
"acme.docsrepo.README.md",
|
|
);
|
|
assert.deepEqual(failures, []);
|
|
assert.deepEqual(infos, ["Agent response: ok"]);
|
|
});
|