Skip to content

Commit

Permalink
feat: configure fakes
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 12, 2024
1 parent 36818ac commit deff46c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
AdonisFSDriverOptions,
ServiceConfigProvider,
ServiceWithLocalServer,
DriveManagerOptions,
} from './types.js'
import debug from './debug.js'

Expand All @@ -33,6 +34,7 @@ import debug from './debug.js'
type ResolvedConfig<Services extends Record<string, DriverFactory>> = {
config: {
default: keyof Services
fakes: DriveManagerOptions<Services>['fakes']
services: {
[K in keyof Services]: Services[K] extends ServiceConfigProvider<infer A> ? A : Services[K]
}
Expand All @@ -45,12 +47,13 @@ type ResolvedConfig<Services extends Record<string, DriverFactory>> = {
*/
export function defineConfig<Services extends Record<string, DriverFactory>>(config: {
default: keyof Services
fakes?: DriveManagerOptions<Services>['fakes']
services: {
[K in keyof Services]: ServiceConfigProvider<Services[K]> | Services[K]
}
}): ConfigProvider<ResolvedConfig<Services>> {
return configProvider.create(async (app) => {
const { services, default: defaultDisk } = config
const { services, fakes, default: defaultDisk } = config
const servicesNames = Object.keys(services)

/**
Expand Down Expand Up @@ -80,6 +83,18 @@ export function defineConfig<Services extends Record<string, DriverFactory>>(con
return {
config: {
default: defaultDisk,
fakes: {
location: app.tmpPath('drive-fakes'),
urlBuilder: {
async generateURL(key, _) {
return `/drive/fakes/${key}`
},
async generateSignedURL(key, _, __) {
return `/drive/fakes/signed/${key}`
},
},
...fakes,
},
services: disks,
},
locallyServed,
Expand Down
12 changes: 7 additions & 5 deletions tests/define_config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ test.group('Define config', () => {
},
})

const app = createAppWithRouter()
const app = await createAppWithRouter()
const config = await configProvider.resolve<any>(app, provider)
assert.deepEqual(config.locallyServed, [])
assert.containsSubset(config.config, {
default: 'fs',
services: {},
})
assert.exists(config.config.fakes)
assert.equal(config.config.fakes.location, app.tmpPath('drive-fakes'))
assert.isFunction(config.config.services.fs)
assert.instanceOf(config.config.services.fs(), FSDriver)
})
Expand All @@ -52,7 +54,7 @@ test.group('Define config', () => {
},
})

const app = createAppWithRouter()
const app = await createAppWithRouter()
await assert.rejects(
() => configProvider.resolve<any>(app, provider),
'Invalid drive config. Missing "routeBasePath" option in "services.fs" object'
Expand All @@ -72,7 +74,7 @@ test.group('Define config', () => {
},
})

const app = createAppWithRouter()
const app = await createAppWithRouter()
const config = await configProvider.resolve<any>(app, provider)
assert.deepEqual(config.locallyServed, [
{
Expand Down Expand Up @@ -100,7 +102,7 @@ test.group('Define config', () => {
},
})

const app = createAppWithRouter()
const app = await createAppWithRouter()
const config = await configProvider.resolve<any>(app, provider)
assert.containsSubset(config.config, {
default: 's3',
Expand All @@ -121,7 +123,7 @@ test.group('Define config', () => {
},
})

const app = createAppWithRouter()
const app = await createAppWithRouter()
const config = await configProvider.resolve<any>(app, provider)
assert.containsSubset(config.config, {
default: 'gcs',
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export function createRouter() {
return router
}

export function createAppWithRouter() {
export async function createAppWithRouter() {
const application = new AppFactory().create(BASE_URL) as unknown as ApplicationService
application.init()
await application.init()

application.container.bind('router', () => createRouter())
return application
Expand Down

0 comments on commit deff46c

Please sign in to comment.