discourse/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
Gabriel Grubba 55a4534653
DEV: Change reply action in the discoveries controller to be a POST and added tests for discobot-discoveries service (#38338)
Updated the action to be a post as it does make side effects, in our
case, it enqueues a job, and updated the client accordingly – I've
checked if there is any other usage of this endpoint in plugins

Also, improved reply action in the client to allow retries when failed,
ensured duplicate requests are not sent, and added their respective
tests.
2026-03-09 14:50:10 -03:00

48 lines
1.5 KiB
JavaScript
Vendored

import { getOwner } from "@ember/owner";
import { cancel } from "@ember/runloop";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
module("Unit | Service | discobot-discoveries", function (hooks) {
setupTest(hooks);
hooks.afterEach(function () {
const service = getOwner(this).lookup("service:discobot-discoveries");
cancel(service.discoveryTimeout);
});
test("does not send duplicate requests for the same successful query", async function (assert) {
let requestsCount = 0;
pretender.post("/discourse-ai/discoveries/reply", () => {
requestsCount += 1;
return response(200, {});
});
const service = getOwner(this).lookup("service:discobot-discoveries");
await service.triggerDiscovery("What is Discourse?");
await service.triggerDiscovery("What is Discourse?");
cancel(service.discoveryTimeout);
assert.strictEqual(requestsCount, 1);
});
test("allows retrying the same query when the request fails", async function (assert) {
let requestsCount = 0;
pretender.post("/discourse-ai/discoveries/reply", () => {
requestsCount += 1;
return response(500, {});
});
const service = getOwner(this).lookup("service:discobot-discoveries");
await service.triggerDiscovery("What is Discourse?");
await service.triggerDiscovery("What is Discourse?");
cancel(service.discoveryTimeout);
assert.strictEqual(requestsCount, 2);
});
});