Skip to content

Commit c7dafcf

Browse files
authored
fix: require a custom 'api' url to send to instead of absolute urls as a 'method' (#420)
1 parent 4fc20be commit c7dafcf

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

src/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default class Client {
2424
}
2525
const client = new config.webapi.WebClient(config.inputs.token, {
2626
agent: this.proxies(config)?.httpsAgent,
27+
allowAbsoluteUrls: false,
2728
logger: config.logger,
2829
retryConfig: this.retries(config.inputs.retries),
2930
slackApiUrl: config.inputs.api || undefined,

test/client.spec.js

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,105 @@ describe("client", () => {
6060
}
6161
}
6262
});
63+
});
6364

64-
it("uses input arguments when constructing the web api client", async () => {
65-
const spy = sinon.spy(mocks.webapi, "WebClient");
65+
describe("api", async () => {
66+
it("uses arguments to send to a slack api method", async () => {
67+
const apis = sinon.stub().resolves({ ok: true });
68+
const constructors = sinon
69+
.stub(mocks.webapi, "WebClient")
70+
.returns({ apiCall: apis });
6671
/**
6772
* @type {Config}
6873
*/
6974
const config = {
7075
content: {
71-
values: {},
76+
values: {
77+
channel: "CHANNELHERE",
78+
timestamp: "1234567890.000000",
79+
},
7280
},
7381
core: core,
7482
logger: new Logger(core).logger,
7583
inputs: {
76-
api: "http://localhost:8080/api",
7784
method: "pins.add",
78-
retries: "10",
7985
token: "xoxb-example-002",
8086
},
8187
webapi: mocks.webapi,
8288
};
8389
await new Client().post(config);
84-
assert.isTrue(spy.calledWithNew());
90+
assert.isTrue(constructors.calledWithNew());
8591
assert.isTrue(
86-
spy.calledWith("xoxb-example-002", {
92+
constructors.calledWith("xoxb-example-002", {
8793
agent: undefined,
94+
allowAbsoluteUrls: false,
95+
logger: config.logger,
96+
retryConfig: webapi.retryPolicies.fiveRetriesInFiveMinutes,
97+
slackApiUrl: undefined,
98+
}),
99+
);
100+
assert.isTrue(apis.calledOnce);
101+
assert.isTrue(
102+
apis.calledWith("pins.add", {
103+
channel: "CHANNELHERE",
104+
timestamp: "1234567890.000000",
105+
}),
106+
);
107+
assert.isTrue(config.core.setOutput.calledWith("ok", true));
108+
});
109+
110+
it("uses arguments to send to a custom api method", async () => {
111+
const apis = sinon.stub().resolves({ done: true, response: "Infinite" });
112+
const constructors = sinon
113+
.stub(mocks.webapi, "WebClient")
114+
.returns({ apiCall: apis });
115+
/**
116+
* @type {Config}
117+
*/
118+
const config = {
119+
content: {
120+
values: {
121+
model: "llama3.2",
122+
prompt: "How many sides does a circle have?",
123+
stream: false,
124+
},
125+
},
126+
core: core,
127+
logger: new Logger(core).logger,
128+
inputs: {
129+
api: "http://localhost:11434/api/",
130+
method: "generate",
131+
retries: "10",
132+
token: "ollamapassword",
133+
},
134+
webapi: mocks.webapi,
135+
};
136+
await new Client().post(config);
137+
assert.isTrue(constructors.calledWithNew());
138+
assert.isTrue(
139+
constructors.calledWith("ollamapassword", {
140+
agent: undefined,
141+
allowAbsoluteUrls: false,
88142
logger: config.logger,
89143
retryConfig: webapi.retryPolicies.tenRetriesInAboutThirtyMinutes,
90-
slackApiUrl: "http://localhost:8080/api",
144+
slackApiUrl: "http://localhost:11434/api/",
145+
}),
146+
);
147+
assert.isTrue(apis.calledOnce);
148+
assert.isTrue(
149+
apis.calledWith("generate", {
150+
model: "llama3.2",
151+
prompt: "How many sides does a circle have?",
152+
stream: false,
91153
}),
92154
);
155+
assert.isTrue(config.core.setOutput.calledWith("ok", undefined));
156+
assert.isTrue(
157+
config.core.setOutput.calledWith(
158+
"response",
159+
JSON.stringify({ done: true, response: "Infinite" }),
160+
),
161+
);
93162
});
94163
});
95164

test/index.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Mock {
4444
constructor() {
4545
this.sandbox = sinon.createSandbox();
4646
this.axios = this.sandbox.stub(axios);
47-
this.calls = sinon.stub(webapi.WebClient.prototype, "apiCall");
47+
this.calls = this.sandbox.stub(webapi.WebClient.prototype, "apiCall");
4848
this.core = this.sandbox.stub(core);
4949
this.fs = this.sandbox.stub(fs);
5050
this.webapi = {
@@ -66,6 +66,13 @@ export class Mock {
6666
this.axios.post.resetHistory();
6767
this.calls.resetHistory();
6868
this.core.getInput.reset();
69+
this.webapi = {
70+
WebClient: function () {
71+
this.apiCall = () => ({
72+
ok: true,
73+
});
74+
},
75+
};
6976
this.core.getInput.withArgs("errors").returns("false");
7077
this.core.getInput.withArgs("retries").returns("5");
7178
process.env.SLACK_TOKEN = "";

0 commit comments

Comments
 (0)