Skip to content

Commit

Permalink
add mixin protected properties tests (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Oct 16, 2021
1 parent cfb24ce commit 56ce088
Show file tree
Hide file tree
Showing 26 changed files with 356 additions and 114 deletions.
6 changes: 4 additions & 2 deletions src/agents/cache-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import type {
import type {
RoomInvitationPayload,
} from '../schemas/room-invitation.js'
import type { PuppetOptions } from '../schemas/puppet.js'
import type {
PuppetOptions,
} from '../schemas/puppet.js'

type PayloadCacheOptions = Required<PuppetOptions>['cache']

Expand All @@ -33,8 +35,8 @@ class CacheAgent {
readonly friendship : QuickLru<string, FriendshipPayload>
readonly message : QuickLru<string, MessagePayload>
readonly room : QuickLru<string, RoomPayload>
readonly roomMember : QuickLru<string, RoomMemberPayload>
readonly roomInvitation : QuickLru<string, RoomInvitationPayload>
readonly roomMember : QuickLru<string, RoomMemberPayload>

constructor (
protected options: PayloadCacheOptions = {},
Expand Down
18 changes: 18 additions & 0 deletions src/mixins/cache-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
CacheMixin,
ProtectedPropertyCacheMixin,
} from './cache-mixin.js'

test('ProtectedPropertyCacheMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyCacheMixin, keyof InstanceType<CacheMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
4 changes: 4 additions & 0 deletions src/mixins/cache-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ const cacheMixin = <MixinBase extends typeof PuppetSkelton>(mixinBase: MixinBase

type CacheMixin = ReturnType<typeof cacheMixin>

type ProtectedPropertyCacheMixin = never
| 'cache'

export type {
CacheMixin,
ProtectedPropertyCacheMixin,
}
export { cacheMixin }
18 changes: 18 additions & 0 deletions src/mixins/contact-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
ContactMixin,
ProtectedPropertyContactMixin,
} from './contact-mixin.js'

test('ProtectedPropertyContactMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyContactMixin, keyof InstanceType<ContactMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
7 changes: 7 additions & 0 deletions src/mixins/contact-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,16 @@ const contactMixin = <MixinBase extends CacheMixin & typeof PuppetSkelton>(mixin
return ContactMixin
}

type ProtectedPropertyContactMixin = never
| 'contactRawPayload'
| 'contactRawPayloadParser'
| 'contactQueryFilterFactory'
| 'contactPayloadCache'

type ContactMixin = ReturnType<typeof contactMixin>

export type {
ContactMixin,
ProtectedPropertyContactMixin,
}
export { contactMixin }
18 changes: 18 additions & 0 deletions src/mixins/friendship-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
FriendshipMixin,
ProtectedPropertyFriendshipMixin,
} from './friendship-mixin.js'

test('ProtectedPropertyFriendshipMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyFriendshipMixin, keyof InstanceType<FriendshipMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
11 changes: 11 additions & 0 deletions src/mixins/friendship-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,15 @@ const friendshipMixin = <MixinBase extends typeof PuppetSkelton & CacheMixin>(mi
return FriendshipMixin
}

type FriendshipMixin = ReturnType<typeof friendshipMixin>

type ProtectedPropertyFriendshipMixin = never
| 'friendshipRawPayload'
| 'friendshipRawPayloadParser'
| 'friendshipPayloadCache'

export type {
FriendshipMixin,
ProtectedPropertyFriendshipMixin,
}
export { friendshipMixin }
18 changes: 18 additions & 0 deletions src/mixins/login-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
LoginMixin,
ProtectedPropertyLoginMixin,
} from './login-mixin.js'

test('ProtectedPropertyLoginMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyLoginMixin, keyof InstanceType<LoginMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
4 changes: 4 additions & 0 deletions src/mixins/login-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ const loginMixin = <MixinBase extends typeof PuppetSkelton>(mixinBase: MixinBase

type LoginMixin = ReturnType<typeof loginMixin>

type ProtectedPropertyLoginMixin = never
| 'login'

export type {
LoginMixin,
ProtectedPropertyLoginMixin,
}
export { loginMixin }
18 changes: 18 additions & 0 deletions src/mixins/memory-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
MemoryMixin,
ProtectedPropertyMemoryMixin,
} from './memory-mixin.js'

test('ProtectedPropertyMemoryMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyMemoryMixin, keyof InstanceType<MemoryMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
9 changes: 9 additions & 0 deletions src/mixins/memory-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ const memoryMixin = <MixinBase extends typeof PuppetSkelton>(mixinBase: MixinBas
return MemoryMixin
}

type MemoryMixin = ReturnType<typeof memoryMixin>

type ProtectedPropertyMemoryMixin = never
| 'memory'

export type {
MemoryMixin,
ProtectedPropertyMemoryMixin,
}
export { memoryMixin }
18 changes: 18 additions & 0 deletions src/mixins/message-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
MessageMixin,
ProtectedPropertyMessageMixin,
} from './message-mixin.js'

test('ProtectedPropertyMessageMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyMessageMixin, keyof InstanceType<MessageMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
12 changes: 12 additions & 0 deletions src/mixins/message-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ const messageMixin = <MinxinBase extends typeof PuppetSkelton & CacheMixin>(base
return MessageMixin
}

type MessageMixin = ReturnType<typeof messageMixin>

type ProtectedPropertyMessageMixin = never
| 'messagePayloadCache'
| 'messageQueryFilterFactory'
| 'messageRawPayload'
| 'messageRawPayloadParser'

export type {
MessageMixin,
ProtectedPropertyMessageMixin,
}
export { messageMixin }
81 changes: 67 additions & 14 deletions src/mixins/mod.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,71 @@
import { cacheMixin } from '../mixins/cache-mixin.js'
import { contactMixin } from '../mixins/contact-mixin.js'
import { friendshipMixin } from '../mixins/friendship-mixin.js'
import { loginMixin } from '../mixins/login-mixin.js'
import { memoryMixin } from '../mixins/memory-mixin.js'
import { messageMixin } from '../mixins/message-mixin.js'
import { miscMixin } from './misc-mixin.js'
import { roomInvitationMixin } from '../mixins/room-invitation-mixin.js'
import { roomMemberMixin } from '../mixins/room-member-mixin.js'
import { roomMixin } from '../mixins/room-mixin.js'
import { stateMixin } from '../mixins/state-mixin.js'
import { tagMixin } from '../mixins/tag-mixin.js'
import { watchdogMixin } from '../mixins/watchdog-mixin.js'
import {
cacheMixin,
ProtectedPropertyCacheMixin,
} from '../mixins/cache-mixin.js'
import {
contactMixin,
ProtectedPropertyContactMixin,
} from '../mixins/contact-mixin.js'
import {
friendshipMixin,
ProtectedPropertyFriendshipMixin,
} from '../mixins/friendship-mixin.js'
import {
loginMixin,
ProtectedPropertyLoginMixin,
} from '../mixins/login-mixin.js'
import {
memoryMixin,
ProtectedPropertyMemoryMixin,
} from '../mixins/memory-mixin.js'
import {
messageMixin,
ProtectedPropertyMessageMixin,
} from '../mixins/message-mixin.js'
import { miscMixin } from './misc-mixin.js'
import {
roomInvitationMixin,
ProtectedPropertyRoomInvitationMixin,
} from '../mixins/room-invitation-mixin.js'
import {
roomMemberMixin,
ProtectedPropertyRoomMemberMixin,
} from '../mixins/room-member-mixin.js'
import {
roomMixin,
ProtectedPropertyRoomMixin,
} from '../mixins/room-mixin.js'
import {
stateMixin,
ProtectedPropertyStateMixin,
} from '../mixins/state-mixin.js'
import { tagMixin } from '../mixins/tag-mixin.js'

/**
* Issue #155 - Mixin: Property 'messageRawPayload' of exported class expression
* may not be private or protected.ts(4094)
* @see https://github.com/wechaty/puppet/issues/155
*
* We can not use `private` or `protected` to declare Mixins
* So we define a `ProtectedMethods` list to mark the protected methods
* And Omit them from the Puppet typing defination
* to build a new PuppetInterface
*/
type PuppetProtectedProperty = never
| ProtectedPropertyCacheMixin
| ProtectedPropertyContactMixin
| ProtectedPropertyFriendshipMixin
| ProtectedPropertyLoginMixin
| ProtectedPropertyMemoryMixin
| ProtectedPropertyMessageMixin
| ProtectedPropertyRoomInvitationMixin
| ProtectedPropertyRoomMemberMixin
| ProtectedPropertyRoomMixin
| ProtectedPropertyStateMixin

export type {
PuppetProtectedProperty,
}
export {
cacheMixin,
contactMixin,
Expand All @@ -25,5 +79,4 @@ export {
roomMixin,
stateMixin,
tagMixin,
watchdogMixin,
}
18 changes: 18 additions & 0 deletions src/mixins/room-invitation-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
RoomInvitationMixin,
ProtectedPropertyRoomInvitationMixin,
} from './room-invitation-mixin.js'

test('ProtectedPropertyRoomInvitationMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyRoomInvitationMixin, keyof InstanceType<RoomInvitationMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
11 changes: 11 additions & 0 deletions src/mixins/room-invitation-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ const roomInvitationMixin = <MixinBase extends typeof PuppetSkelton & CacheMixin
return RoomInvitationMixin
}

type RoomInvitationMixin = ReturnType<typeof roomInvitationMixin>

type ProtectedPropertyRoomInvitationMixin = never
| 'roomInvitationPayloadCache'
| 'roomInvitationRawPayload'
| 'roomInvitationRawPayloadParser'

export type {
RoomInvitationMixin,
ProtectedPropertyRoomInvitationMixin,
}
export { roomInvitationMixin }
18 changes: 18 additions & 0 deletions src/mixins/room-member-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
RoomMemberMixin,
ProtectedPropertyRoomMemberMixin,
} from './room-member-mixin.js'

test('ProtectedPropertyRoomMemberMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyRoomMemberMixin, keyof InstanceType<RoomMemberMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
5 changes: 5 additions & 0 deletions src/mixins/room-member-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ const roomMemberMixin = <MixinBase extends typeof PuppetSkelton & ContactMixin>(

type RoomMemberMixin = ReturnType<typeof roomMemberMixin>

type ProtectedPropertyRoomMemberMixin = never
| 'roomMemberRawPayload'
| 'roomMemberRawPayloadParser'

export type {
ProtectedPropertyRoomMemberMixin,
RoomMemberMixin,
}
export { roomMemberMixin }
18 changes: 18 additions & 0 deletions src/mixins/room-mixin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S node --loader ts-node/esm

import {
test,
} from 'tstest'

import type {
RoomMixin,
ProtectedPropertyRoomMixin,
} from './room-mixin.js'

test('ProtectedPropertyRoomMixin', async t => {
type NotExistInMixin = Exclude<ProtectedPropertyRoomMixin, keyof InstanceType<RoomMixin>>
type NotExistTest = NotExistInMixin extends never ? true : false

const noOneLeft: NotExistTest = true
t.ok(noOneLeft, 'should match Mixin properties for every protected property')
})
Loading

0 comments on commit 56ce088

Please sign in to comment.