mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-06-19 09:09:52 +08:00
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.
48 lines
1.5 KiB
JavaScript
Vendored
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);
|
|
});
|
|
});
|