-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d94f6cd
commit 167e620
Showing
3 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import { expect, describe, test } from 'vitest' | ||
|
||
import { PatreonStore } from '../../v2' | ||
|
||
const values = new Map<string, string>() | ||
.set('test', 'value') | ||
.set('key', 'test') | ||
|
||
describe('KV store', () => { | ||
let throwError = false | ||
|
||
const store = new PatreonStore.KV({ | ||
get: (key) => new Promise((resolve) => { | ||
const value = values.get(key) | ||
if (!value && throwError) throw new Error() | ||
return resolve(value ?? null) | ||
}), | ||
put: (key, value) => new Promise(resolve => { values.set(key, value); resolve() }), | ||
}, 'token') | ||
|
||
test('unset value', async () => { | ||
expect(await store.get()).toBeUndefined() | ||
throwError = true | ||
expect(await store.get()).toBeUndefined() | ||
}) | ||
|
||
test('update token', async () => { | ||
const token = { | ||
access_token: 'access', | ||
expires_in: '1200', | ||
expires_in_epoch: '10000', | ||
refresh_token: 'refresh', | ||
token_type: 'bearer', | ||
} | ||
|
||
await store.put(token) | ||
|
||
expect(await store.get()).toEqual(token) | ||
}) | ||
}) | ||
|
||
describe('Fetch store', () => { | ||
const store = new PatreonStore.Fetch('https://localhost:8000', async (url, options) => { | ||
if (options?.method === 'GET' || !options?.method) { | ||
const token = values.get('fetch_token') | ||
|
||
return new Response(token, { | ||
status: token ? 200 : 500, | ||
}) | ||
} else if (options.method === 'PUT') { | ||
values.set('fetch_token', options.body!) | ||
return new Response(null, { | ||
status: 201, | ||
}) | ||
} else return new Response(null, { status: 400 }) | ||
}) | ||
|
||
test('unset value', async () => { | ||
expect(await store.get()).toBeUndefined() | ||
}) | ||
|
||
test('update token', async () => { | ||
const token = { | ||
access_token: 'access', | ||
expires_in: '1200', | ||
expires_in_epoch: '10000', | ||
refresh_token: 'refresh', | ||
token_type: 'bearer', | ||
} | ||
|
||
await store.put(token) | ||
|
||
expect(await store.get()).toEqual(token) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters