diff options
| author | Take-John <takejohn@takejohn.jp> | 2025-01-07 21:28:48 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-07 12:28:48 +0000 |
| commit | bbe80af1dde195ff0ac6713db967b556acebb30c (patch) | |
| tree | 075a07368d678125cc0fd0327271823102e3e332 /packages/frontend/src/scripts/aiscript/api.ts | |
| parent | fix(frontend): frontend / frontend-embedにあるtsconfig.jsonのmoduleをES2... (diff) | |
| download | misskey-bbe80af1dde195ff0ac6713db967b556acebb30c.tar.gz misskey-bbe80af1dde195ff0ac6713db967b556acebb30c.tar.bz2 misskey-bbe80af1dde195ff0ac6713db967b556acebb30c.zip | |
Fix: aiscriptディレクトリ内の型エラー解消と単体テスト (#15191)
* AiScript APIの型エラーに対処
* AiScript UI APIのテスト作成
* onInputなどがPromiseを返すように
* AiScript共通APIのテスト作成
* CHANGELOG記載
* 定数のテストをconcurrentに
* vi.mockを使用
* misskeyApiをmisskeyApiUntypedのエイリアスとする
* 期待されるエラーメッセージを修正
* Mk:removeのテスト
* misskeyApiの型を変更
Diffstat (limited to 'packages/frontend/src/scripts/aiscript/api.ts')
| -rw-r--r-- | packages/frontend/src/scripts/aiscript/api.ts | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/packages/frontend/src/scripts/aiscript/api.ts b/packages/frontend/src/scripts/aiscript/api.ts index 8afe88eec6..e203c51bba 100644 --- a/packages/frontend/src/scripts/aiscript/api.ts +++ b/packages/frontend/src/scripts/aiscript/api.ts @@ -3,14 +3,24 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { utils, values } from '@syuilo/aiscript'; +import { errors, utils, values } from '@syuilo/aiscript'; import * as Misskey from 'misskey-js'; +import { url, lang } from '@@/js/config.js'; +import { assertStringAndIsIn } from './common.js'; import * as os from '@/os.js'; import { misskeyApi } from '@/scripts/misskey-api.js'; import { $i } from '@/account.js'; import { miLocalStorage } from '@/local-storage.js'; import { customEmojis } from '@/custom-emojis.js'; -import { url, lang } from '@@/js/config.js'; + +const DIALOG_TYPES = [ + 'error', + 'info', + 'success', + 'warning', + 'waiting', + 'question', +] as const; export function aiScriptReadline(q: string): Promise<string> { return new Promise(ok => { @@ -22,15 +32,20 @@ export function aiScriptReadline(q: string): Promise<string> { }); } -export function createAiScriptEnv(opts) { +export function createAiScriptEnv(opts: { storageKey: string, token?: string }) { return { USER_ID: $i ? values.STR($i.id) : values.NULL, - USER_NAME: $i ? values.STR($i.name) : values.NULL, + USER_NAME: $i?.name ? values.STR($i.name) : values.NULL, USER_USERNAME: $i ? values.STR($i.username) : values.NULL, CUSTOM_EMOJIS: utils.jsToVal(customEmojis.value), LOCALE: values.STR(lang), SERVER_URL: values.STR(url), 'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => { + utils.assertString(title); + utils.assertString(text); + if (type != null) { + assertStringAndIsIn(type, DIALOG_TYPES); + } await os.alert({ type: type ? type.value : 'info', title: title.value, @@ -39,6 +54,11 @@ export function createAiScriptEnv(opts) { return values.NULL; }), 'Mk:confirm': values.FN_NATIVE(async ([title, text, type]) => { + utils.assertString(title); + utils.assertString(text); + if (type != null) { + assertStringAndIsIn(type, DIALOG_TYPES); + } const confirm = await os.confirm({ type: type ? type.value : 'question', title: title.value, @@ -48,14 +68,20 @@ export function createAiScriptEnv(opts) { }), 'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => { utils.assertString(ep); - if (ep.value.includes('://')) throw new Error('invalid endpoint'); + if (ep.value.includes('://')) { + throw new errors.AiScriptRuntimeError('invalid endpoint'); + } if (token) { utils.assertString(token); // バグがあればundefinedもあり得るため念のため if (typeof token.value !== 'string') throw new Error('invalid token'); } const actualToken: string|null = token?.value ?? opts.token ?? null; - return misskeyApi(ep.value, utils.valToJs(param), actualToken).then(res => { + if (param == null) { + throw new errors.AiScriptRuntimeError('expected param'); + } + utils.assertObject(param); + return misskeyApi(ep.value, utils.valToJs(param) as object, actualToken).then(res => { return utils.jsToVal(res); }, err => { return values.ERROR('request_failed', utils.jsToVal(err)); |