-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix race condition
- Loading branch information
Showing
29 changed files
with
517 additions
and
199 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,16 +1,15 @@ | ||
/* eslint-disable no-var */ | ||
import type { mastodon } from '../src'; | ||
import type { SessionPoolImpl, TokenPool } from './pools'; | ||
import type { SessionPoolImpl } from './pools'; | ||
|
||
declare global { | ||
var admin: mastodon.RestClient; | ||
var sessions: SessionPoolImpl; | ||
|
||
/** Should only be used inside /test-utils */ | ||
var __misc__: { | ||
url: string; | ||
tokens: TokenPool; | ||
instance: mastodon.v1.Instance; | ||
adminToken: mastodon.v1.Token; | ||
tokens: TokenPool; | ||
}; | ||
} |
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,13 @@ | ||
import NodeEnvironment from 'jest-environment-node'; | ||
|
||
import { createMisc } from './misc'; | ||
|
||
class CustomEnvironment extends NodeEnvironment { | ||
override async setup(): Promise<void> { | ||
await super.setup(); | ||
const misc = await createMisc(); | ||
this.global.__misc__ = misc; | ||
} | ||
} | ||
|
||
export default CustomEnvironment; |
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
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,55 @@ | ||
/* eslint-disable unicorn/prefer-module */ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import './jest-polyfills'; | ||
|
||
import { existsSync } from 'node:fs'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
import type { mastodon } from '../src'; | ||
import { createRestClient } from '../src'; | ||
import { TokenPoolFsImpl } from './pools'; | ||
import { TokenRepositoryFs } from './pools/token-repository-fs'; | ||
|
||
const readAdminToken = async ( | ||
baseCacheDir: string, | ||
): Promise<mastodon.v1.Token> => { | ||
const tokenFilePath = path.join(baseCacheDir, 'admin_token.json'); | ||
|
||
if (!existsSync(tokenFilePath)) { | ||
throw new Error('Admin token does not exist'); | ||
} | ||
|
||
const json = await fs.readFile(tokenFilePath, 'utf8'); | ||
return JSON.parse(json); | ||
}; | ||
|
||
type Misc = { | ||
readonly url: string; | ||
readonly instance: mastodon.v1.Instance; | ||
readonly tokens: TokenPoolFsImpl; | ||
readonly adminToken: mastodon.v1.Token; | ||
}; | ||
|
||
export const createMisc = async (): Promise<Misc> => { | ||
const url = 'http://localhost:3000'; | ||
const instance = await createRestClient({ url }).v1.instance.fetch(); | ||
|
||
const baseCacheDir = path.join(__dirname, '../node_modules/.cache/masto'); | ||
if (!existsSync(baseCacheDir)) { | ||
throw new Error('Cache directory does not exist'); | ||
} | ||
|
||
const adminToken = await readAdminToken(baseCacheDir); | ||
const repository = new TokenRepositoryFs( | ||
path.join(baseCacheDir, 'tokens.json'), | ||
); | ||
const tokenPool = new TokenPoolFsImpl(repository); | ||
|
||
return { | ||
url, | ||
instance, | ||
tokens: tokenPool, | ||
adminToken, | ||
}; | ||
}; |
Oops, something went wrong.