-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
12 changed files
with
239 additions
and
101 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 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 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 |
---|---|---|
@@ -1,27 +1,74 @@ | ||
import { afterEach, beforeEach } from "bun:test"; | ||
import { mockEnvironment } from "logos:test/mocks"; | ||
import { type Environment, loadEnvironment } from "logos:core/loaders/environment"; | ||
import { CacheStore } from "logos/stores/cache"; | ||
import { DatabaseStore } from "logos/stores/database"; | ||
import { DiscordConnection } from "logos/connection"; | ||
|
||
type DependencyProvider<T> = () => T; | ||
|
||
function useDatabaseStore(): DependencyProvider<DatabaseStore> { | ||
let database: DatabaseStore; | ||
function createProvider<T>({ | ||
create, | ||
destroy, | ||
}: { | ||
create: () => Promise<T> | T; | ||
destroy?: (object: T) => Promise<void> | void; | ||
}): () => DependencyProvider<T> { | ||
let object: T; | ||
|
||
beforeEach(async () => { | ||
database = DatabaseStore.create({ | ||
log: constants.loggers.silent, | ||
environment: mockEnvironment, | ||
cache: new CacheStore({ log: constants.loggers.silent }), | ||
}); | ||
await database.setup({ prefetchDocuments: false }); | ||
}); | ||
beforeEach(async () => (object = await create())); | ||
|
||
if (destroy !== undefined) { | ||
afterEach(() => destroy(object)); | ||
} | ||
|
||
return () => () => object; | ||
} | ||
|
||
function createEnvironment(): Environment { | ||
return loadEnvironment({ log: constants.loggers.silent }); | ||
} | ||
|
||
afterEach(async () => { | ||
await database.teardown(); | ||
function createDatabaseStore(): DatabaseStore { | ||
return DatabaseStore.create({ | ||
log: constants.loggers.silent, | ||
environment: createEnvironment(), | ||
cache: new CacheStore({ log: constants.loggers.silent }), | ||
}); | ||
} | ||
|
||
let connection: DiscordConnection; | ||
/** | ||
* This is the only dependency we do not re-create on every call. It takes at least 5 seconds to establish a connection | ||
* to Discord, which is definitely not the kind of overhead we want when running tests. | ||
*/ | ||
function createDiscordConnection(): DiscordConnection { | ||
if (connection !== undefined) { | ||
return connection; | ||
} | ||
|
||
connection = new DiscordConnection({ environment: loadEnvironment({ log: constants.loggers.silent }) }); | ||
|
||
return () => database; | ||
return connection; | ||
} | ||
|
||
export { useDatabaseStore }; | ||
const useEnvironment = createProvider({ create: () => createEnvironment() }); | ||
const useDatabaseStore = createProvider({ | ||
create: async () => { | ||
const database = createDatabaseStore(); | ||
await database.setup(); | ||
|
||
return database; | ||
}, | ||
destroy: (database) => database.teardown(), | ||
}); | ||
const useDiscordConnection = createProvider({ | ||
create: async () => { | ||
const connection = createDiscordConnection(); | ||
await connection.open(); | ||
|
||
return connection; | ||
}, | ||
destroy: (connection) => connection.close(), | ||
}); | ||
|
||
export { useEnvironment, useDatabaseStore, useDiscordConnection }; |
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,11 +1,43 @@ | ||
import { describe } from "bun:test"; | ||
import { describe, it } from "bun:test"; | ||
import { useEnvironment } from "logos:test/dependencies"; | ||
import { expect } from "chai"; | ||
import { DiscordConnection } from "logos/connection"; | ||
|
||
const TEST_TIMEOUT = constants.time.second * 10; | ||
|
||
describe("DiscordConnection", () => { | ||
const environment = useEnvironment(); | ||
|
||
describe("open()", () => { | ||
// TODO(vxern): Add tests. | ||
it( | ||
"opens a gateway connection to Discord.", | ||
async () => { | ||
const connection = new DiscordConnection({ environment: environment() }); | ||
|
||
expect(() => connection.bot.gateway.editBotStatus({ status: "offline" })).to.throw; | ||
|
||
await connection.open(); | ||
|
||
expect(() => connection.bot.gateway.editBotStatus({ status: "offline" })).to.not.throw; | ||
}, | ||
{ timeout: TEST_TIMEOUT }, | ||
); | ||
}); | ||
|
||
describe("close()", () => { | ||
// TODO(vxern): Add tests. | ||
it( | ||
"closes the gateway connection to Discord.", | ||
async () => { | ||
const connection = new DiscordConnection({ environment: environment() }); | ||
await connection.open(); | ||
|
||
expect(() => connection.bot.gateway.editBotStatus({ status: "offline" })).to.not.throw; | ||
|
||
await connection.close(); | ||
|
||
expect(() => connection.bot.gateway.editBotStatus({ status: "offline" })).to.throw; | ||
}, | ||
{ timeout: TEST_TIMEOUT }, | ||
); | ||
}); | ||
}); |
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,3 @@ | ||
import { describe } from "bun:test"; | ||
|
||
describe("CacheStore", () => {}); |
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
Oops, something went wrong.