Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 242 additions & 5 deletions packages/gunshi/src/plugin/core.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,245 @@
import { expectTypeOf, test } from 'vitest'
import { describe, expectTypeOf, test } from 'vitest'

import { PluginExtension } from './core.ts'
import type { Args } from '../types.ts'
import type {
ExtractDependencyId,
InferDependencyExtensions,
IsOptionalDependency,
OnPluginExtension,
PluginDependency,
PluginExtension,
PluginFunction,
PluginOptions
} from './core.ts'

test('PluginExtension', () => {
type T1 = PluginExtension<{ foo: number }>
expectTypeOf<Awaited<ReturnType<T1>>>().toEqualTypeOf<{ foo: number }>()
describe('ExtractDependencyId', () => {
test('PluginDependency', () => {
type T = ExtractDependencyId<PluginDependency>
expectTypeOf<T>().toEqualTypeOf<string>()
})

test('object with id', () => {
type T = ExtractDependencyId<{ id: 'foo' }>
expectTypeOf<T>().toEqualTypeOf<'foo'>()
})

test('object with id and optional', () => {
type T = ExtractDependencyId<{ id: 'foo'; optional: true }>
expectTypeOf<T>().toEqualTypeOf<'foo'>()
})
})

describe('IsOptionalDependency', () => {
test('PluginDependency', () => {
type T = IsOptionalDependency<PluginDependency>
expectTypeOf<T>().toEqualTypeOf<false>()
})

test('object with id', () => {
type T = IsOptionalDependency<{ id: 'foo' }>
expectTypeOf<T>().toEqualTypeOf<false>()
})

test('object with id and optional', () => {
type T1 = IsOptionalDependency<{ id: 'foo'; optional: true }>
expectTypeOf<T1>().toEqualTypeOf<true>()
type T2 = IsOptionalDependency<{ id: 'foo'; optional: false }>
expectTypeOf<T2>().toEqualTypeOf<false>()
})
})

type Ext1 = { foo: number }
type Ext2 = { bar: string }
type Ext3 = { baz: boolean }
type Ext4 = { qux: symbol }

describe('InferDependencyExtensions', () => {
test('required and optional dependencies', () => {
type T = InferDependencyExtensions<
[{ id: 'ext1' }, { id: 'ext2'; optional: true }],
{ ext1: Ext1; ext2: Ext2 }
>
expectTypeOf<T>().toEqualTypeOf<
{
ext1: Ext1
} & {
ext2: Ext2 | undefined
}
>()
})

test('un available dependencies', () => {
type T = InferDependencyExtensions<
[{ id: 'ext1'; optional: true }, { id: 'ext2' }],
{ ext3: Ext3; ext4: Ext4 }
>
expectTypeOf<T>().toMatchObjectType<{}>()
})

test('empty dependencies', () => {
type T = InferDependencyExtensions<[], { ext1: Ext1; ext2: Ext2 }>
expectTypeOf<T>().toMatchObjectType<{}>()
})

test('mixed dependencies', () => {
type T = InferDependencyExtensions<
['ext1', { id: 'ext2'; optional: false }],
{ ext1: Ext1 } & { ext2: Ext2 }
>
expectTypeOf<T>().toEqualTypeOf<
{
ext1: Ext1
} & {
ext2: Ext2
}
>()
})

test('long dependencies with mixed types', () => {
type T = InferDependencyExtensions<
[{ id: 'ext1'; optional: true }, 'ext2', { id: 'ext3' }, { id: 'ext4'; optional: false }],
{ ext1: Ext1; ext2: Ext2; ext3: Ext3; ext4: Ext4 }
>
expectTypeOf<T>().toEqualTypeOf<
{
ext1: Ext1 | undefined
} & {
ext2: Ext2
} & {
ext3: Ext3
} & {
ext4: Ext4
}
>()
})
})

describe('PluginOptions', () => {
test('default type parameters', () => {
type T = PluginOptions
expectTypeOf<T['id']>().toEqualTypeOf<string>()
expectTypeOf<T['name']>().toEqualTypeOf<string | undefined>()
expectTypeOf<T['dependencies']>().toEqualTypeOf<(PluginDependency | string)[] | undefined>()
expectTypeOf<T['setup']>().toEqualTypeOf<PluginFunction | undefined>()
expectTypeOf<T['extension']>().toEqualTypeOf<PluginExtension<{}> | undefined>()
expectTypeOf<T['onExtension']>().toEqualTypeOf<OnPluginExtension | undefined>()
// NOTE(kazupon): `toEqualTypeOf` does not work ...
// expectTypeOf<T>().toEqualTypeOf<{
// id: string
// name: string | undefined
// dependencies: ((PluginDependency | string)[]) | undefined
// setup: PluginFunction | undefined
// extension: PluginExtension | undefined
// onExtension: OnPluginExtension | undefined
// }>()
})

test('type parameters fully', () => {
type T = PluginOptions<{ ext1: { foo: number } }, 'plugin1', ['ext1'], { bar: string }>
expectTypeOf<T['id']>().toEqualTypeOf<'plugin1'>()
expectTypeOf<T['name']>().toEqualTypeOf<string | undefined>()
expectTypeOf<T['dependencies']>().toEqualTypeOf<['ext1'] | undefined>()
expectTypeOf<T['setup']>().toEqualTypeOf<
| PluginFunction<{
args: Args
extensions: { ext1: { foo: number } } & { plugin1: { bar: string } }
}>
| undefined
>()
expectTypeOf<T['extension']>().toEqualTypeOf<
| PluginExtension<{ bar: string }, { args: Args; extensions: { ext1: { foo: number } } }>
| undefined
>()
expectTypeOf<T['onExtension']>().toEqualTypeOf<
| OnPluginExtension<{
args: Args
extensions: { ext1: { foo: number } } & { plugin1: { bar: string } }
}>
| undefined
>()
})

test('required and optional dependencies', () => {
type T = PluginOptions<
{ ext1: Ext1; ext2: Ext2 },
'plugin1',
[{ id: 'ext1' }, { id: 'ext2'; optional: true }],
{ bar: string }
>
expectTypeOf<T['dependencies']>().toEqualTypeOf<
[{ id: 'ext1' }, { id: 'ext2'; optional: true }] | undefined
>()
expectTypeOf<T['setup']>().toEqualTypeOf<
| PluginFunction<{
args: Args
extensions: { ext1: Ext1 } & { ext2: Ext2 | undefined } & { plugin1: { bar: string } }
}>
| undefined
>()
expectTypeOf<T['extension']>().toEqualTypeOf<
| PluginExtension<
{ bar: string },
{ args: Args; extensions: { ext1: Ext1 } & { ext2: Ext2 | undefined } }
>
| undefined
>()
expectTypeOf<T['onExtension']>().toEqualTypeOf<
| OnPluginExtension<{
args: Args
extensions: { ext1: Ext1 } & { ext2: Ext2 | undefined } & { plugin1: { bar: string } }
}>
| undefined
>()
})

test('unmatch required and optional dependencies', () => {
type T = PluginOptions<
{ ext1: Ext1; ext2: Ext2 },
'plugin1',
[{ id: 'ext3' }, { id: 'ext3'; optional: true }],
{ bar: string }
>
expectTypeOf<T['dependencies']>().toEqualTypeOf<
[{ id: 'ext3' }, { id: 'ext3'; optional: true }] | undefined
>()
expectTypeOf<T['setup']>().toEqualTypeOf<
| PluginFunction<{
args: Args
extensions: { plugin1: { bar: string } }
}>
| undefined
>()
expectTypeOf<T['extension']>().toEqualTypeOf<
PluginExtension<{ bar: string }, { args: Args; extensions: {} }> | undefined
>()
expectTypeOf<T['onExtension']>().toEqualTypeOf<
| OnPluginExtension<{
args: Args
extensions: { plugin1: { bar: string } }
}>
| undefined
>()
})

test('no dependencies', () => {
type T = PluginOptions<{ ext1: Ext1; ext2: Ext2 }, 'plugin1', [], { bar: string }>
expectTypeOf<T['dependencies']>().toEqualTypeOf<[] | undefined>()
expectTypeOf<T['setup']>().toEqualTypeOf<
| PluginFunction<{
args: Args
extensions: { plugin1: { bar: string } }
}>
| undefined
>()
expectTypeOf<T['extension']>().toEqualTypeOf<
PluginExtension<{ bar: string }, { args: Args; extensions: {} }> | undefined
>()
expectTypeOf<T['onExtension']>().toEqualTypeOf<
| OnPluginExtension<{
args: Args
extensions: { plugin1: { bar: string } }
}>
| undefined
>()
})
})
22 changes: 9 additions & 13 deletions packages/gunshi/src/plugin/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createPluginContext } from './context.ts'
import { plugin } from './core.ts'

import type { Args } from 'args-tokens'
import type { Command, CommandContextCore, ExtendContext, GunshiParams } from '../types.ts'
import type { Command, CommandContextCore, GunshiParams } from '../types.ts'
import type { PluginContext } from './context.ts'
import type { Plugin } from './core.ts'

Expand Down Expand Up @@ -201,21 +201,17 @@ describe('plugin function', () => {
ctx.decorateHeaderRenderer(async (baseRenderer, cmdCtx) => {
const user = cmdCtx.extensions.auth.user

expectTypeOf(cmdCtx.extensions).toEqualTypeOf<
{
auth: ReturnType<typeof extensionFactory>
} & ExtendContext
>()
expectTypeOf(cmdCtx.extensions).toEqualTypeOf<{
auth: ReturnType<typeof extensionFactory>
}>()

console.log(`User: ${user.name} (${user.id})`)
return await baseRenderer(cmdCtx)
})
ctx.decorateCommand(baseRunner => async ctx => {
expectTypeOf(ctx.extensions).toEqualTypeOf<
{
auth: ReturnType<typeof extensionFactory>
} & ExtendContext
>()
expectTypeOf(ctx.extensions).toEqualTypeOf<{
auth: ReturnType<typeof extensionFactory>
}>()

const result = await baseRunner(ctx)
return `[AUTH] ${result}`
Expand All @@ -240,12 +236,12 @@ describe('plugin function', () => {
setup: async ctx => {
ctx.addGlobalOption('token', { type: 'string' })
ctx.decorateUsageRenderer(async (baseRenderer, cmdCtx) => {
expectTypeOf(cmdCtx.extensions).toEqualTypeOf<ExtendContext>()
expectTypeOf(cmdCtx.extensions).toEqualTypeOf<{ auth: {} }>()

return await baseRenderer(cmdCtx)
})
ctx.decorateValidationErrorsRenderer(async (baseRenderer, cmdCtx, error) => {
expectTypeOf(cmdCtx.extensions).toEqualTypeOf<ExtendContext>()
expectTypeOf(cmdCtx.extensions).toEqualTypeOf<{ auth: {} }>()

return await baseRenderer(cmdCtx, error)
})
Expand Down
Loading