Skip to content

Commit

Permalink
refactor(backend/test): add interface UserToken
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Jun 26, 2023
1 parent 58a898d commit db1f560
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions packages/backend/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ import type * as misskey from 'misskey-js';

export { server as startServer } from '@/boot/common.js';

interface UserToken { token: string }

const config = loadConfig();
export const port = config.port;

export const cookie = (me: any): string => {
export const cookie = (me: UserToken): string => {
return `token=${me.token};`;
};

export const api = async (endpoint: string, params: any, me?: any) => {
export const api = async (endpoint: string, params: any, me?: UserToken) => {
const normalized = endpoint.replace(/^\//, '');
return await request(`api/${normalized}`, params, me);
};

export type ApiRequest = {
endpoint: string,
parameters: object,
user: object | undefined,
user: UserToken | undefined,
};

export const successfulApiCall = async <T, >(request: ApiRequest, assertion: {
Expand All @@ -55,7 +57,7 @@ export const failedApiCall = async <T, >(request: ApiRequest, assertion: {
return res.body;
};

const request = async (path: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
const request = async (path: string, params: any, me?: UserToken): Promise<{ body: any, status: number }> => {
const auth = me ? {
i: me.token,
} : {};
Expand Down Expand Up @@ -94,7 +96,7 @@ export const signup = async (params?: Partial<misskey.Endpoints['signup']['req']
return res.body;
};

export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
export const post = async (user: UserToken, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
const q = params;

const res = await api('notes/create', q, user);
Expand All @@ -117,21 +119,21 @@ export const hiddenNote = (note: any): any => {
return temp;
};

export const react = async (user: any, note: any, reaction: string): Promise<any> => {
export const react = async (user: UserToken, note: any, reaction: string): Promise<any> => {
await api('notes/reactions/create', {
noteId: note.id,
reaction: reaction,
}, user);
};

export const userList = async (user: any, userList: any = {}): Promise<any> => {
export const userList = async (user: UserToken, userList: any = {}): Promise<any> => {
const res = await api('users/lists/create', {
name: 'test',
}, user);
return res.body;
};

export const page = async (user: any, page: any = {}): Promise<any> => {
export const page = async (user: UserToken, page: any = {}): Promise<any> => {
const res = await api('pages/create', {
alignCenter: false,
content: [
Expand All @@ -154,7 +156,7 @@ export const page = async (user: any, page: any = {}): Promise<any> => {
return res.body;
};

export const play = async (user: any, play: any = {}): Promise<any> => {
export const play = async (user: UserToken, play: any = {}): Promise<any> => {
const res = await api('flash/create', {
permissions: [],
script: 'test',
Expand All @@ -165,7 +167,7 @@ export const play = async (user: any, play: any = {}): Promise<any> => {
return res.body;
};

export const clip = async (user: any, clip: any = {}): Promise<any> => {
export const clip = async (user: UserToken, clip: any = {}): Promise<any> => {
const res = await api('clips/create', {
description: null,
isPublic: true,
Expand All @@ -175,7 +177,7 @@ export const clip = async (user: any, clip: any = {}): Promise<any> => {
return res.body;
};

export const galleryPost = async (user: any, channel: any = {}): Promise<any> => {
export const galleryPost = async (user: UserToken, channel: any = {}): Promise<any> => {
const res = await api('gallery/posts/create', {
description: null,
fileIds: [],
Expand All @@ -186,7 +188,7 @@ export const galleryPost = async (user: any, channel: any = {}): Promise<any> =>
return res.body;
};

export const channel = async (user: any, channel: any = {}): Promise<any> => {
export const channel = async (user: UserToken, channel: any = {}): Promise<any> => {
const res = await api('channels/create', {
bannerId: null,
description: null,
Expand All @@ -196,7 +198,7 @@ export const channel = async (user: any, channel: any = {}): Promise<any> => {
return res.body;
};

export const role = async (user: any, role: any = {}, policies: any = {}): Promise<any> => {
export const role = async (user: UserToken, role: any = {}, policies: any = {}): Promise<any> => {
const res = await api('admin/roles/create', {
asBadge: false,
canEditMembersByModerator: false,
Expand Down Expand Up @@ -239,7 +241,7 @@ interface UploadOptions {
* Upload file
* @param user User
*/
export const uploadFile = async (user: any, { path, name, blob }: UploadOptions = {}): Promise<any> => {
export const uploadFile = async (user: UserToken, { path, name, blob }: UploadOptions = {}): Promise<any> => {
const absPath = path == null
? new URL('resources/Lenna.jpg', import.meta.url)
: isAbsolute(path.toString())
Expand Down Expand Up @@ -268,7 +270,7 @@ export const uploadFile = async (user: any, { path, name, blob }: UploadOptions
};
};

export const uploadUrl = async (user: any, url: string) => {
export const uploadUrl = async (user: UserToken, url: string) => {
let file: any;
const marker = Math.random().toString();

Expand All @@ -290,7 +292,7 @@ export const uploadUrl = async (user: any, url: string) => {
return file;
};

export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
export function connectStream(user: UserToken, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
return new Promise((res, rej) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}/streaming?i=${user.token}`);

Expand All @@ -317,7 +319,7 @@ export function connectStream(user: any, channel: string, listener: (message: Re
});
}

export const waitFire = async (user: any, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => {
export const waitFire = async (user: UserToken, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => {
return new Promise<boolean>(async (res, rej) => {
let timer: NodeJS.Timeout | null = null;

Expand Down

0 comments on commit db1f560

Please sign in to comment.