Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 2, 2024
1 parent 5f5a0e4 commit 6e26cd2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 47 deletions.
7 changes: 1 addition & 6 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export interface Hook extends EventOptions {
}

class Lifecycle {
_tasks = new Set<Promise<void>>()
_hooks: Record<keyof any, Hook[]> = {}

constructor(private ctx: Context) {
Expand Down Expand Up @@ -127,11 +126,7 @@ class Lifecycle {
}, { global: true }))
}

async flush() {
while (this._tasks.size) {
await Promise.all(Array.from(this._tasks))
}
}
async flush() {}

filterHooks(hooks: Hook[], thisArg?: object) {
thisArg = getTraceable(this.ctx, thisArg)
Expand Down
20 changes: 1 addition & 19 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,6 @@ export abstract class EffectScope<C extends Context = Context> {
}
}

ensure(callback: () => Promise<void>) {
let resolve: () => void
const task = new Promise<void>((_resolve) => {
resolve = _resolve
})
this.updateStatus(() => this.tasks.add(task))
this.context.events._tasks.add(task)
callback()
.catch((reason) => {
this.context.emit(this.ctx, 'internal/error', reason)
this.cancel(reason)
})
.finally(() => {
resolve()
this.updateStatus(() => this.tasks.delete(task))
this.context.events._tasks.delete(task)
})
}

cancel(reason?: any) {
this.error = reason
this.updateStatus(() => this.hasError = true)
Expand Down Expand Up @@ -204,6 +185,7 @@ export abstract class EffectScope<C extends Context = Context> {
if (!this.isReady || this.isActive || this.uid === null) return true
this.isActive = true
await this.activate()
this.updateStatus()
}

accept(callback?: (config: C['config']) => void | boolean, options?: AcceptOptions): () => boolean
Expand Down
4 changes: 2 additions & 2 deletions packages/core/tests/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ describe('Decorator', () => {

class Foo extends Service {
constructor(ctx: Context) {
super(ctx, 'foo', true)
super(ctx, 'foo')
}
}

class Bar extends Service {
constructor(ctx: Context) {
super(ctx, 'bar', true)
super(ctx, 'bar')
}

@Inject(['foo'])
Expand Down
1 change: 0 additions & 1 deletion packages/core/tests/isolate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ describe('Isolation', () => {
ctx.on(event, inner)
ctx.plugin(Foo)

await ctx.start()
expect(outer.mock.calls).to.have.length(0)
expect(inner.mock.calls).to.have.length(1)
})
Expand Down
36 changes: 18 additions & 18 deletions packages/core/tests/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context, Service } from '../src'
import { noop } from 'cosmokit'
import { expect } from 'chai'
import { mock } from 'node:test'
import { checkError, Counter, getHookSnapshot } from './utils'
import { checkError, Counter, getHookSnapshot, sleep } from './utils'

describe('Service', () => {
it('non-service access', async () => {
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('Service', () => {
// root is a property
expect(root.get('root')).to.be.undefined

root.using({ optional: ['foo'] }, (ctx) => {
root.using({ foo: { required: false } }, (ctx) => {
warn.mock.resetCalls()
ctx.baz = 2
expect(warn.mock.calls).to.have.length(0)
Expand All @@ -100,9 +100,6 @@ describe('Service', () => {

const root = new Context()
root.plugin(Foo)
expect(root.foo).to.be.undefined

await root.start()
expect(root.foo).to.be.instanceof(Foo)

root.registry.delete(Foo)
Expand All @@ -112,7 +109,7 @@ describe('Service', () => {
it('immediate service', async () => {
class Foo extends Service {
constructor(ctx: Context) {
super(ctx, 'foo', true)
super(ctx, 'foo')
}
}

Expand All @@ -121,6 +118,7 @@ describe('Service', () => {
root.on('internal/service', callback)

root.plugin(Foo)
await sleep(0)
expect(root.foo).to.be.instanceof(Foo)
expect(callback.mock.calls).to.have.length(1)

Expand All @@ -133,7 +131,7 @@ describe('Service', () => {
static inject = ['counter']

constructor(ctx: Context) {
super(ctx, 'foo', true)
super(ctx, 'foo')
}

get value() {
Expand All @@ -151,6 +149,7 @@ describe('Service', () => {
root.set('counter', new Counter(root))

root.plugin(Foo)
await sleep(0)
root.foo.increase()
expect(root.foo.value).to.equal(1)
expect(warning.mock.calls).to.have.length(0)
Expand All @@ -172,7 +171,7 @@ describe('Service', () => {
it('traceable effect (without inject)', async () => {
class Foo extends Service {
constructor(ctx: Context) {
super(ctx, 'foo', true)
super(ctx, 'foo')
}

get value() {
Expand All @@ -190,6 +189,7 @@ describe('Service', () => {
root.set('counter', new Counter(root))

root.plugin(Foo)
await sleep(0)
root.foo.increase()
expect(root.foo.value).to.equal(1)
expect(warning.mock.calls).to.have.length(2)
Expand All @@ -212,7 +212,7 @@ describe('Service', () => {
const callback = mock.fn((foo: any) => {})
const dispose = mock.fn((foo: any) => {})
const plugin = mock.fn((ctx: Context) => {
ctx.on('ready', () => callback(ctx.foo))
callback(ctx.foo)
ctx.on('dispose', () => dispose(ctx.foo))
})

Expand Down Expand Up @@ -259,7 +259,7 @@ describe('Service', () => {
super(ctx, 'foo')
}

start = start
[Service.activate] = start
stop = stop
fork = fork
}
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('Service', () => {
it('memory leak test', async () => {
class Test extends Service {
constructor(ctx: Context) {
super(ctx, 'test', true)
super(ctx, 'test')
ctx.using(['test'], () => {})
}
}
Expand All @@ -314,32 +314,32 @@ describe('Service', () => {
class Foo extends Service {
static inject = ['qux']
constructor(ctx: Context) {
super(ctx, 'foo', true)
super(ctx, 'foo')
}
start = foo
[Service.activate] = foo
}

class Bar extends Service {
static inject = ['foo', 'qux']
constructor(ctx: Context) {
super(ctx, 'bar', true)
super(ctx, 'bar')
}
start = bar
[Service.activate] = bar
}

class Qux extends Service {
constructor(ctx: Context) {
super(ctx, 'qux', true)
super(ctx, 'qux')
}
start = qux
[Service.activate] = qux
}

const root = new Context()
root.plugin(Foo)
root.plugin(Bar)
root.plugin(Qux)

await root.start()
await sleep(0)
expect(foo.mock.calls).to.have.length(1)
expect(bar.mock.calls).to.have.length(1)
expect(qux.mock.calls).to.have.length(1)
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { Dict } from 'cosmokit'

use(promised)

export function sleep(ms = 0) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

export const event = 'custom-event'

export class Session {
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/config/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ export class Import extends ImportTree {
}
this.file = new LoaderFile(filename, LoaderFile.writable[ext])
this.file.ref(this)
await super.start()
await super[Service.activate]()
}
}

0 comments on commit 6e26cd2

Please sign in to comment.