2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const bot_data = zrequire("bot_data");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2020-05-10 21:01:33 +02:00
|
|
|
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
// Bot types and service bot types can be found
|
|
|
|
// in zerver/models.py - UserProfile Class or
|
|
|
|
// zever/openapi/zulip.yaml
|
|
|
|
|
2020-05-10 21:01:33 +02:00
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@zulip.com",
|
|
|
|
full_name: "Me Myself",
|
2020-05-10 21:01:33 +02:00
|
|
|
user_id: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
const fred = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "fred@zulip.com",
|
|
|
|
full_name: "Fred Frederickson",
|
2020-05-10 21:01:33 +02:00
|
|
|
user_id: 3,
|
|
|
|
};
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const bot_data_params = {
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_bots: [
|
|
|
|
{
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
api_key: "1234567890qwertyuioop",
|
|
|
|
avatar_url: "",
|
|
|
|
bot_type: 1, // DEFAULT_BOT
|
|
|
|
default_all_public_streams: true,
|
|
|
|
default_events_register_stream: "register stream 42",
|
|
|
|
default_sending_stream: "sending stream 42",
|
|
|
|
email: "bot0@zulip.com",
|
|
|
|
full_name: "Bot 0",
|
|
|
|
is_active: true,
|
|
|
|
owner_id: 4,
|
|
|
|
user_id: 42,
|
|
|
|
services: [],
|
|
|
|
extra: "This field should be ignored",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
api_key: "1234567890zxcvbnm",
|
|
|
|
avatar_url: "",
|
|
|
|
bot_type: 3, // OUTGOING_WEBHOOK_BOT
|
|
|
|
default_all_public_streams: true,
|
|
|
|
default_events_register_stream: "register stream 314",
|
|
|
|
default_sending_stream: "sending stream 314",
|
2020-07-15 00:34:28 +02:00
|
|
|
email: "outgoingwebhook@zulip.com",
|
|
|
|
full_name: "Outgoing webhook",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
is_active: true,
|
|
|
|
owner_id: 5,
|
|
|
|
user_id: 314,
|
|
|
|
services: [{base_url: "http://foo.com", interface: 1, token: "basictoken12345"}],
|
|
|
|
extra: "This field should be ignored",
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
|
|
|
],
|
2014-02-27 18:27:31 +01:00
|
|
|
};
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2021-03-14 10:02:33 +01:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-03-14 10:02:33 +01:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
bot_data.initialize(bot_data_params);
|
|
|
|
// Our startup logic should have added Bot 0 from page_params.
|
|
|
|
assert.equal(bot_data.get(42).full_name, "Bot 0");
|
|
|
|
assert.equal(bot_data.get(314).full_name, "Outgoing webhook");
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-03-14 10:02:33 +01:00
|
|
|
});
|
|
|
|
}
|
2014-03-12 19:31:58 +01:00
|
|
|
|
2021-03-14 10:02:33 +01:00
|
|
|
test("test_basics", () => {
|
|
|
|
people.add_active_user(fred);
|
2018-04-18 22:08:00 +02:00
|
|
|
const test_bot = {
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
api_key: "qwertyuioop1234567890",
|
2020-07-15 01:29:15 +02:00
|
|
|
avatar_url: "",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
bot_type: 1,
|
|
|
|
default_all_public_streams: true,
|
|
|
|
default_events_register_stream: "register stream 43",
|
|
|
|
default_sending_stream: "sending stream 43",
|
|
|
|
email: "bot1@zulip.com",
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bot 1",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
is_active: true,
|
|
|
|
owner_id: 6,
|
|
|
|
user_id: 43,
|
|
|
|
services: [
|
|
|
|
{
|
|
|
|
base_url: "http://bar.com",
|
|
|
|
interface: 1,
|
|
|
|
token: "some Bot 1 token",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
extra: "This field should be ignored",
|
2014-02-27 00:01:18 +01:00
|
|
|
};
|
2018-04-18 22:08:00 +02:00
|
|
|
const test_embedded_bot = {
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
api_key: "zxcvbnm1234567890",
|
2020-07-15 01:29:15 +02:00
|
|
|
avatar_url: "",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
bot_type: 4, // EMBEDDED_BOT
|
|
|
|
default_all_public_streams: true,
|
|
|
|
default_events_register_stream: "register stream 143",
|
|
|
|
default_sending_stream: "sending stream 143",
|
|
|
|
email: "embedded-bot@zulip.com",
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Embedded bot 1",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
is_active: true,
|
|
|
|
owner_id: 7,
|
|
|
|
user_id: 143,
|
|
|
|
services: [
|
|
|
|
{
|
|
|
|
config_data: {key: "12345678"},
|
|
|
|
service_name: "giphy",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
extra: "This field should be ignored",
|
2018-01-30 19:24:18 +01:00
|
|
|
};
|
|
|
|
|
2014-02-27 00:01:18 +01:00
|
|
|
(function test_add() {
|
|
|
|
bot_data.add(test_bot);
|
2018-04-18 22:08:00 +02:00
|
|
|
const bot = bot_data.get(43);
|
|
|
|
const services = bot_data.get_services(43);
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
assert.equal("qwertyuioop1234567890", bot.api_key);
|
|
|
|
assert.equal("", bot.avatar_url);
|
|
|
|
assert.equal(1, bot.bot_type);
|
|
|
|
assert.equal(true, bot.default_all_public_streams);
|
|
|
|
assert.equal("register stream 43", bot.default_events_register_stream);
|
|
|
|
assert.equal("sending stream 43", bot.default_sending_stream);
|
|
|
|
assert.equal("bot1@zulip.com", bot.email);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
assert.equal(true, bot.is_active);
|
|
|
|
assert.equal(6, bot.owner_id);
|
|
|
|
assert.equal(43, bot.user_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("http://bar.com", services[0].base_url);
|
2018-01-16 20:40:03 +01:00
|
|
|
assert.equal(1, services[0].interface);
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
assert.equal("some Bot 1 token", services[0].token);
|
2014-02-27 00:01:18 +01:00
|
|
|
assert.equal(undefined, bot.extra);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 00:01:18 +01:00
|
|
|
|
|
|
|
(function test_update() {
|
|
|
|
bot_data.add(test_bot);
|
|
|
|
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot = bot_data.get(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
2020-07-15 00:34:28 +02:00
|
|
|
bot_data.update(43, {
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
...test_bot,
|
2020-07-15 00:34:28 +02:00
|
|
|
full_name: "New Bot 1",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
services: [{interface: 2, base_url: "http://baz.com", token: "zxcvbnm1234567890"}],
|
2020-07-15 00:34:28 +02:00
|
|
|
});
|
2018-01-22 18:36:53 +01:00
|
|
|
bot = bot_data.get(43);
|
2018-04-18 22:08:00 +02:00
|
|
|
const services = bot_data.get_services(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("New Bot 1", bot.full_name);
|
2018-01-16 20:40:03 +01:00
|
|
|
assert.equal(2, services[0].interface);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("http://baz.com", services[0].base_url);
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
assert.equal("zxcvbnm1234567890", services[0].token);
|
2020-05-10 19:21:08 +02:00
|
|
|
|
|
|
|
const change_owner_event = {
|
|
|
|
owner_id: fred.user_id,
|
|
|
|
};
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
bot_data.update(43, {...test_bot, ...change_owner_event});
|
2020-05-10 19:21:08 +02:00
|
|
|
|
|
|
|
bot = bot_data.get(43);
|
|
|
|
assert.equal(bot.owner_id, fred.user_id);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2018-01-30 19:24:18 +01:00
|
|
|
(function test_embedded_bot_update() {
|
|
|
|
bot_data.add(test_embedded_bot);
|
2018-04-18 22:08:00 +02:00
|
|
|
const bot_id = 143;
|
|
|
|
const services = bot_data.get_services(bot_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("12345678", services[0].config_data.key);
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
bot_data.update(bot_id, {
|
|
|
|
...test_embedded_bot,
|
|
|
|
services: [{config_data: {key: "87654321"}, service_name: "embedded bot service"}],
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("87654321", services[0].config_data.key);
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
assert.equal("embedded bot service", services[0].service_name);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-30 19:24:18 +01:00
|
|
|
|
2014-02-27 00:01:18 +01:00
|
|
|
(function test_remove() {
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot;
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
bot_data.add({...test_bot, is_active: true});
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2018-01-22 18:36:53 +01:00
|
|
|
bot = bot_data.get(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(bot.is_active);
|
2018-01-22 18:36:53 +01:00
|
|
|
bot_data.deactivate(43);
|
|
|
|
bot = bot_data.get(43);
|
2017-02-07 19:48:17 +01:00
|
|
|
assert.equal(bot.is_active, false);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 00:01:18 +01:00
|
|
|
|
2020-05-09 20:06:14 +02:00
|
|
|
(function test_all_user_ids() {
|
|
|
|
const all_ids = bot_data.all_user_ids();
|
|
|
|
all_ids.sort();
|
|
|
|
assert.deepEqual(all_ids, [143, 314, 42, 43]);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2020-05-09 20:06:14 +02:00
|
|
|
|
2018-03-08 16:28:37 +01:00
|
|
|
(function test_delete() {
|
2018-04-18 22:08:00 +02:00
|
|
|
let bot;
|
2018-03-08 16:28:37 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
bot_data.add({...test_bot, is_active: true});
|
2018-03-08 16:28:37 +01:00
|
|
|
|
|
|
|
bot = bot_data.get(43);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Bot 1", bot.full_name);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(bot.is_active);
|
2018-08-04 08:04:24 +02:00
|
|
|
bot_data.del(43);
|
2018-03-08 16:28:37 +01:00
|
|
|
bot = bot_data.get(43);
|
|
|
|
assert.equal(bot, undefined);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-03-08 16:28:37 +01:00
|
|
|
|
2014-02-27 18:27:31 +01:00
|
|
|
(function test_get_editable() {
|
2020-05-11 19:14:31 +02:00
|
|
|
bot_data.add({...test_bot, user_id: 44, owner_id: me.user_id, is_active: true});
|
2020-07-15 00:34:28 +02:00
|
|
|
bot_data.add({
|
|
|
|
...test_bot,
|
|
|
|
user_id: 45,
|
|
|
|
email: "bot2@zulip.com",
|
|
|
|
owner_id: me.user_id,
|
|
|
|
is_active: true,
|
|
|
|
});
|
|
|
|
bot_data.add({
|
|
|
|
...test_bot,
|
|
|
|
user_id: 46,
|
|
|
|
email: "bot3@zulip.com",
|
|
|
|
owner_id: fred.user_id,
|
|
|
|
is_active: true,
|
|
|
|
});
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const editable_bots = bot_data.get_editable().map((bot) => bot.email);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(["bot1@zulip.com", "bot2@zulip.com"], editable_bots);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2017-05-25 06:00:14 +02:00
|
|
|
(function test_get_all_bots_for_current_user() {
|
2018-04-18 22:08:00 +02:00
|
|
|
const bots = bot_data.get_all_bots_for_current_user();
|
2014-02-27 18:27:31 +01:00
|
|
|
|
2017-05-25 06:00:14 +02:00
|
|
|
assert.equal(bots.length, 2);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(bots[0].email, "bot1@zulip.com");
|
|
|
|
assert.equal(bots[1].email, "bot2@zulip.com");
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2022-04-14 09:15:06 +02:00
|
|
|
|
|
|
|
(function test_get_number_of_bots_owned_by_user() {
|
|
|
|
const bots_owned_by_user = bot_data.get_all_bots_owned_by_user(3);
|
|
|
|
|
|
|
|
assert.equal(bots_owned_by_user[0].email, "bot3@zulip.com");
|
|
|
|
})();
|
2018-06-02 01:37:58 +02:00
|
|
|
});
|