Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
add last of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed Jul 7, 2024
1 parent 3b3e722 commit 68f3a9c
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jobs:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.42.0
deno-version: v1.44.4
- run: deno fmt --check .
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.42.0
deno-version: v1.44.4
- uses: actions/setup-node@v4
with:
node-version: '20.x'
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Test
on:
push:
pull_request:
workflow_dispatch:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.44.4
- run: deno test
8 changes: 8 additions & 0 deletions tests/internal/rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rest_api } from '../../src/api/rest.ts';
import { regular_user } from './user.ts';

export const client = new rest_api({
token: 'test',
api_url: 'http://localhost:8000',
account: regular_user,
});
268 changes: 268 additions & 0 deletions tests/rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
import { rest_api } from '../src/api/rest.ts';
import { chat_is_api_chat, regular_chat } from './internal/chat.ts';
import { assertEquals, assertRejects, mockFetch } from './internal/deps.ts';
import { client } from './internal/rest.ts';
import { post_is_api_post, regular_post } from './internal/post.ts';
import { regular_user, user_is_api_user } from './internal/user.ts';

Deno.test('login and signup', async (i) => {
await i.step('login (successful)', async () => {
mockFetch('http://localhost:8000/auth/login', {
status: 200,
body: JSON.stringify({
token: 'test',
account: regular_user,
}),
});

const r = await rest_api.login('test', 'test', 'http://localhost:8000');

assertEquals(r.api_token, 'test');
});

await i.step('login (failed)', async () => {
mockFetch('http://localhost:8000/auth/login', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await rest_api.login('test', 'test', 'http://localhost:8000');
});
});

await i.step('signup (successful)', async () => {
mockFetch('http://localhost:8000/signup', {
status: 200,
body: JSON.stringify({
token: 'test',
account: regular_user,
}),
});

const r = await rest_api.signup(
'test',
'test',
'test',
'http://localhost:8000',
);

assertEquals(r.api_token, 'test');
});

await i.step('signup (failed)', async () => {
mockFetch('http://localhost:8000/signup', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await rest_api.signup(
'test',
'test',
'test',
'http://localhost:8000',
);
});
});
});

Deno.test('api statistics', async (i) => {
await i.step('get statistics (successful)', async () => {
mockFetch('http://localhost:8000/statistics', {
status: 200,
body: JSON.stringify({
users: 1,
chats: 1,
posts: 1,
}),
});

const stats = await client.get_statistics();

assertEquals(stats.users, 1);
assertEquals(stats.chats, 1);
assertEquals(stats.posts, 1);
});

await i.step('get statistics (failed)', async () => {
mockFetch('http://localhost:8000/statistics', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.get_statistics();
});
});
});

Deno.test('api chats', async (i) => {
await i.step('get chats (successful)', async () => {
mockFetch('http://localhost:8000/chats', {
status: 200,
body: JSON.stringify({
autoget: [regular_chat],
}),
});

const chats = await client.get_chats();

assertEquals(chats.length, 3); // we add home and livechat
chat_is_api_chat(chats[0], regular_chat);
});

await i.step('get chats (failed)', async () => {
mockFetch('http://localhost:8000/chats', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.get_chats();
});
});

await i.step('get single chat (successful)', async () => {
mockFetch('http://localhost:8000/chats/test', {
status: 200,
body: JSON.stringify(regular_chat),
});

chat_is_api_chat(await client.get_chat('test'), regular_chat);
});

await i.step('get single chat (failed)', async () => {
// @ts-ignore: clear chat cache
client.chat_cache.set('test', undefined)

mockFetch('http://localhost:8000/chats/test', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.get_chat('test');
});
});

await i.step('create chat (successful)', async () => {
mockFetch('http://localhost:8000/chats', {
status: 200,
body: JSON.stringify(regular_chat),
});

chat_is_api_chat(await client.create_chat(regular_chat), regular_chat);
});

await i.step('create chat (failed)', async () => {
mockFetch('http://localhost:8000/chats', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.create_chat(regular_chat);
});
});
});

Deno.test('api get post', async (i) => {
await i.step('get post (successful)', async () => {
mockFetch('http://localhost:8000/posts?id=test', {
status: 200,
body: JSON.stringify(regular_post),
});

post_is_api_post(await client.get_post('test'), regular_post);
});

await i.step('get post (failed)', async () => {
// @ts-ignore: clear post cache
client.post_cache.set('test', undefined)

mockFetch('http://localhost:8000/posts?id=test', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.get_post('test');
});
});
});

Deno.test('api users', async (i) => {
await i.step('get user (successful)', async () => {
mockFetch('http://localhost:8000/users/test', {
status: 200,
body: JSON.stringify(regular_user),
});

user_is_api_user(await client.get_user('test'), regular_user);
});

await i.step('get user (failed)', async () => {
// @ts-ignore: clear user cache
client.user_cache.set('test', undefined)

mockFetch('http://localhost:8000/users/test', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.get_user('test');
});
});

await i.step('search users (successful)', async () => {
mockFetch('http://localhost:8000/search/users/?autoget&q=test&page=1', {
status: 200,
body: JSON.stringify({ autoget: [regular_user] }),
});

const r = await client.search_users('test');

assertEquals(r.length, 1);
user_is_api_user(r[0], regular_user);
});

await i.step('search users (failed)', async () => {
mockFetch('http://localhost:8000/search/users/?autoget&q=test&page=1', {
status: 404,
body: JSON.stringify({
error: true,
type: 'notFound',
}),
});

await assertRejects(async () => {
await client.search_users('test');
});
});
});

0 comments on commit 68f3a9c

Please sign in to comment.