From 4478748748a174c3db75ae2221166eaa3bec842b Mon Sep 17 00:00:00 2001 From: Timothee Legros <62490329+timolegros@users.noreply.github.com> Date: Thu, 25 Apr 2024 19:14:43 +0200 Subject: [PATCH] `/libs/model` test type fixes (#7544) * update tsconfig configuration in `/model` * update tsconfig configuration in `/core` * update tsconfig configuration in `/chains` * update tsconfig configuration in `/adapters` * update tsconfig configuration in `shared` * update tsconfig configuration in `logging` * `packages/commonwealth` tsconfig update + copy `references` to build tsconfig (https://github.com/microsoft/TypeScript/issues/27098) * specify tsconfig.build.json in references * discobot and snapshot tsconfig.build.json updates * remove test from includes (follow-up PR) * merge resolution * merge resolution * remove "test" from include in snapshot * model test type fixes * leaner tsconfig --------- Co-authored-by: rotorsoft --- libs/core/src/schemas/entities.schemas.ts | 12 +++--- .../test/community/group-lifecycle.spec.ts | 2 +- .../community/stake-historical-price.spec.ts | 29 ++++++++++---- .../test/community/stake-lifecycle.spec.ts | 21 ++++------ .../test/community/stake-transaction.spec.ts | 4 +- libs/model/test/seed/model.spec.ts | 2 +- libs/model/test/seed/seed.spec.ts | 5 ++- .../comment-subscription-lifecycle.spec.ts | 20 +++++----- .../community-alerts-lifecycle.spec.ts | 40 ++++++++++--------- ...subscription-preferences-lifecycle.spec.ts | 15 ++++--- .../thread-subscription-lifecycle.spec.ts | 30 +++++++------- libs/model/tsconfig.json | 3 +- 12 files changed, 100 insertions(+), 83 deletions(-) diff --git a/libs/core/src/schemas/entities.schemas.ts b/libs/core/src/schemas/entities.schemas.ts index 09061501741..c5e4d2119f8 100644 --- a/libs/core/src/schemas/entities.schemas.ts +++ b/libs/core/src/schemas/entities.schemas.ts @@ -560,19 +560,19 @@ export const SubscriptionPreference = z.object({ }); export const ThreadSubscription = z.object({ - id: PG_INT, + id: PG_INT.optional(), user_id: PG_INT, thread_id: PG_INT, - created_at: z.date(), - updated_at: z.date(), + created_at: z.date().optional(), + updated_at: z.date().optional(), }); export const CommentSubscription = z.object({ - id: PG_INT, + id: PG_INT.optional(), user_id: PG_INT, comment_id: PG_INT, - created_at: z.date(), - updated_at: z.date(), + created_at: z.date().optional(), + updated_at: z.date().optional(), }); export const CommunityAlert = z.object({ diff --git a/libs/model/test/community/group-lifecycle.spec.ts b/libs/model/test/community/group-lifecycle.spec.ts index 211381adc68..0bfc5c824b3 100644 --- a/libs/model/test/community/group-lifecycle.spec.ts +++ b/libs/model/test/community/group-lifecycle.spec.ts @@ -13,7 +13,7 @@ chai.use(chaiAsPromised); const chance = Chance(); describe('Group lifecycle', () => { - let id; + let id: string; let actor: Actor; const payload = { diff --git a/libs/model/test/community/stake-historical-price.spec.ts b/libs/model/test/community/stake-historical-price.spec.ts index 6d2a8a1298e..e174db726c7 100644 --- a/libs/model/test/community/stake-historical-price.spec.ts +++ b/libs/model/test/community/stake-historical-price.spec.ts @@ -1,4 +1,4 @@ -import { dispose, query } from '@hicommonwealth/core'; +import { Actor, dispose, query } from '@hicommonwealth/core'; import { BalanceType } from '@hicommonwealth/shared'; import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; @@ -8,7 +8,8 @@ import { seed } from '../../src/tester/index'; chai.use(chaiAsPromised); describe('Stake Historical Price', () => { - let community_id; + let community_id: string; + let actor: Actor; before(async () => { const [node] = await seed('ChainNode', { @@ -36,7 +37,6 @@ describe('Stake Historical Price', () => { { stake_id: 2, stake_token: '', - stake_weight: 1, vote_weight: 1, stake_enabled: true, }, @@ -47,6 +47,11 @@ describe('Stake Historical Price', () => { discord_config_id: null, }); + actor = { + user: { id: user!.id!, email: user!.email! }, + address_id: community!.Addresses![0].address, + }; + community_id = community!.id!; await seed('StakeTransaction', { @@ -54,7 +59,7 @@ describe('Stake Historical Price', () => { stake_id: 2, community_id, timestamp: Math.floor(Date.now() / 1000), - stake_price: 88, + stake_price: '88', stake_direction: 'buy', stake_amount: 1, }); @@ -64,7 +69,7 @@ describe('Stake Historical Price', () => { stake_id: 2, community_id, timestamp: 1000, - stake_price: 10, + stake_price: '10', stake_direction: 'buy', stake_amount: 1, }); @@ -73,7 +78,7 @@ describe('Stake Historical Price', () => { stake_id: 2, community_id, timestamp: 1, - stake_price: 99, + stake_price: '99', stake_direction: 'buy', stake_amount: 1, }); @@ -85,18 +90,26 @@ describe('Stake Historical Price', () => { it('should return undefined if no historical price', async () => { const results = await query(GetStakeHistoricalPrice(), { - payload: { past_date_epoch: 1, community_id: 'non-existing' }, + actor, + payload: { + past_date_epoch: 1, + community_id: 'non-existing', + stake_id: 2, + }, }); expect(results).to.deep.equal([]); }); it('should return the historical price', async () => { const results = await query(GetStakeHistoricalPrice(), { + actor, payload: { past_date_epoch: Math.floor(Date.now() / 1000) - 24 * 60 * 60, // date 24 horus ago community_id, + stake_id: 2, }, }); - expect(results[0].old_price).to.equal('88'); + expect(results).to.exist; + expect(results![0]!.old_price).to.equal('88'); }); }); diff --git a/libs/model/test/community/stake-lifecycle.spec.ts b/libs/model/test/community/stake-lifecycle.spec.ts index 1851dee8eb9..bc45e097d08 100644 --- a/libs/model/test/community/stake-lifecycle.spec.ts +++ b/libs/model/test/community/stake-lifecycle.spec.ts @@ -17,9 +17,9 @@ import { seed } from '../../src/tester'; chai.use(chaiAsPromised); describe('Stake lifecycle', () => { - let id_with_stake; - let id_without_stake_to_set; - let id_without_stake; + let id_with_stake: string; + let id_without_stake_to_set: string; + let id_without_stake: string; let actor: Actor; const payload = { @@ -30,16 +30,11 @@ describe('Stake lifecycle', () => { }; before(async () => { - const [node] = await seed( - 'ChainNode', - { contracts: [] }, - // { mock: true, log: true }, - ); - const [user] = await seed( - 'User', - { isAdmin: true, selected_community_id: null }, - // { mock: true, log: true }, - ); + const [node] = await seed('ChainNode', { contracts: [] }); + const [user] = await seed('User', { + isAdmin: true, + selected_community_id: null, + }); const [community_with_stake] = await seed( 'Community', { diff --git a/libs/model/test/community/stake-transaction.spec.ts b/libs/model/test/community/stake-transaction.spec.ts index a0084e08618..bc928e02a9c 100644 --- a/libs/model/test/community/stake-transaction.spec.ts +++ b/libs/model/test/community/stake-transaction.spec.ts @@ -13,7 +13,7 @@ chai.use(chaiAsPromised); describe('Stake transactions', () => { const actor: Actor = { user: { email: '' } }; let payload; - let community_id; + let community_id: string; before(async () => { const [node] = await seed('ChainNode', { @@ -120,7 +120,7 @@ describe('Stake transactions', () => { payload, }); } catch (e) { - expect(e.message).to.equal( + expect((e as Error).message).to.equal( 'Transaction is not associated with provided community', ); return; diff --git a/libs/model/test/seed/model.spec.ts b/libs/model/test/seed/model.spec.ts index 741b5477e7c..84b60555b6e 100644 --- a/libs/model/test/seed/model.spec.ts +++ b/libs/model/test/seed/model.spec.ts @@ -67,7 +67,7 @@ const generateSchemas = async () => { }; describe('Model schema', () => { - let schemas; + let schemas: { [x: string]: { model: any; migration: any } }; before(async () => { schemas = await generateSchemas(); diff --git a/libs/model/test/seed/seed.spec.ts b/libs/model/test/seed/seed.spec.ts index 30d669cedf9..453e6c30842 100644 --- a/libs/model/test/seed/seed.spec.ts +++ b/libs/model/test/seed/seed.spec.ts @@ -29,12 +29,13 @@ async function testSeed( // perform schema validation on created entity (throws) const schema = schemas.entities[name]; const model = models[name]; - const data = await schema.parse(record); + const data: ReturnType = schema.parse(record); // attempt to find entity that was created const existingEntity = await (model as ModelStatic).findOne({ where: { - [model.primaryKeyAttribute]: data[model.primaryKeyAttribute], + [model.primaryKeyAttribute]: + data[model.primaryKeyAttribute as keyof typeof data], }, }); expect(existingEntity, 'failed to find created entity after creation').not.to diff --git a/libs/model/test/subscription/comment-subscription-lifecycle.spec.ts b/libs/model/test/subscription/comment-subscription-lifecycle.spec.ts index 19f8df54865..c0aed0d3cf2 100644 --- a/libs/model/test/subscription/comment-subscription-lifecycle.spec.ts +++ b/libs/model/test/subscription/comment-subscription-lifecycle.spec.ts @@ -1,6 +1,7 @@ -import { Actor, command, dispose, query } from '@hicommonwealth/core'; +import { Actor, command, dispose, query, schemas } from '@hicommonwealth/core'; import { BalanceType } from '@hicommonwealth/shared'; import { expect } from 'chai'; +import z from 'zod'; import { models } from '../../src/database'; import { CreateCommentSubscription, @@ -11,7 +12,8 @@ import { seed } from '../../src/tester'; describe('Comment subscription lifecycle', () => { let actor: Actor; - let commentOne, commentTwo; + let commentOne: z.infer | undefined; + let commentTwo: z.infer | undefined; before(async () => { const [user] = await seed('User', { isAdmin: false, @@ -71,7 +73,7 @@ describe('Comment subscription lifecycle', () => { it('should create a new comment subscription', async () => { const payload = { - comment_id: commentOne.id, + comment_id: commentOne!.id!, }; const res = await command(CreateCommentSubscription(), { payload, @@ -79,15 +81,15 @@ describe('Comment subscription lifecycle', () => { }); expect(res).to.deep.contains({ user_id: actor.user.id, - comment_id: commentOne.id, + comment_id: commentOne!.id, }); }); it('should get comment subscriptions', async () => { const [commentSubOne, commentSubTwo] = await models.CommentSubscription.bulkCreate([ - { user_id: actor.user.id, comment_id: commentOne.id }, - { user_id: actor.user.id, comment_id: commentTwo.id }, + { user_id: actor.user.id!, comment_id: commentOne!.id! }, + { user_id: actor.user.id!, comment_id: commentTwo!.id! }, ]); const res = await query(GetCommentSubscriptions(), { @@ -110,12 +112,12 @@ describe('Comment subscription lifecycle', () => { it('should delete a comment subscriptions', async () => { await models.CommentSubscription.bulkCreate([ - { user_id: actor.user.id, comment_id: commentOne.id }, - { user_id: actor.user.id, comment_id: commentTwo.id }, + { user_id: actor.user.id!, comment_id: commentOne!.id! }, + { user_id: actor.user.id!, comment_id: commentTwo!.id! }, ]); const payload = { - comment_ids: [commentOne.id, commentTwo.id], + comment_ids: [commentOne!.id!, commentTwo!.id!], }; const res = await command(DeleteCommentSubscription(), { diff --git a/libs/model/test/subscription/community-alerts-lifecycle.spec.ts b/libs/model/test/subscription/community-alerts-lifecycle.spec.ts index c3318a70eef..c084dcd5bab 100644 --- a/libs/model/test/subscription/community-alerts-lifecycle.spec.ts +++ b/libs/model/test/subscription/community-alerts-lifecycle.spec.ts @@ -1,6 +1,7 @@ -import { Actor, command, dispose, query } from '@hicommonwealth/core'; +import { Actor, command, dispose, query, schemas } from '@hicommonwealth/core'; import { BalanceType } from '@hicommonwealth/shared'; import { expect } from 'chai'; +import z from 'zod'; import { models } from '../../src/database'; import { CreateCommunityAlert, @@ -11,7 +12,8 @@ import { seed } from '../../src/tester'; describe('Community alerts lifecycle', () => { let actor: Actor; - let community, communityTwo; + let community: z.infer | undefined; + let communityTwo: z.infer | undefined; before(async () => { const [user] = await seed('User', { isAdmin: false, @@ -45,7 +47,7 @@ describe('Community alerts lifecycle', () => { }); actor = { user: { id: user!.id!, email: user!.email! }, - address_id: null, + address_id: '0x', }; }); @@ -59,7 +61,7 @@ describe('Community alerts lifecycle', () => { it('should create a new community alert', async () => { const payload = { - community_id: community.id, + community_id: community!.id!, }; const res = await command(CreateCommunityAlert(), { @@ -69,18 +71,18 @@ describe('Community alerts lifecycle', () => { expect(res).to.deep.contains({ user_id: actor.user.id, - community_id: community.id, + community_id: community!.id!, }); }); it('should delete a single community alert via id', async () => { const [alert] = await seed('CommunityAlert', { user_id: actor.user.id, - community_id: community.id, + community_id: community!.id!, }); const payload = { - community_ids: [community.id], - ids: [alert.id], + community_ids: [community!.id!], + ids: [alert!.id!], }; const res = await command(DeleteCommunityAlerts(), { payload, @@ -92,15 +94,15 @@ describe('Community alerts lifecycle', () => { it('should delete multiple community alerts via ids', async () => { const [alertOne] = await seed('CommunityAlert', { user_id: actor.user.id, - community_id: community.id, + community_id: community!.id!, }); const [alertTwo] = await seed('CommunityAlert', { user_id: actor.user.id, - community_id: communityTwo.id, + community_id: communityTwo!.id!, }); const payload = { - community_ids: [community.id, communityTwo.id], - ids: [alertOne.id, alertTwo.id], + community_ids: [community!.id!, communityTwo!.id!], + ids: [alertOne!.id!, alertTwo!.id!], }; const res = await command(DeleteCommunityAlerts(), { payload, @@ -112,10 +114,10 @@ describe('Community alerts lifecycle', () => { it('should delete a single community alert via community id', async () => { await seed('CommunityAlert', { user_id: actor.user.id, - community_id: community.id, + community_id: community!.id!, }); const payload = { - community_ids: [community.id], + community_ids: [community!.id!], }; const res = await command(DeleteCommunityAlerts(), { payload, @@ -127,14 +129,14 @@ describe('Community alerts lifecycle', () => { it('should delete multiple community alerts via community ids', async () => { await seed('CommunityAlert', { user_id: actor.user.id, - community_id: community.id, + community_id: community!.id!, }); await seed('CommunityAlert', { user_id: actor.user.id, - community_id: communityTwo.id, + community_id: communityTwo!.id!, }); const payload = { - community_ids: [community.id, communityTwo.id], + community_ids: [community!.id!, communityTwo!.id!], }; const res = await command(DeleteCommunityAlerts(), { payload, @@ -146,11 +148,11 @@ describe('Community alerts lifecycle', () => { it('should get community alerts', async () => { const [alertOne] = await seed('CommunityAlert', { user_id: actor.user.id, - community_id: community.id, + community_id: community!.id!, }); const [alertTwo] = await seed('CommunityAlert', { user_id: actor.user.id, - community_id: communityTwo.id, + community_id: communityTwo!.id!, }); const res = await query(GetCommunityAlerts(), { diff --git a/libs/model/test/subscription/subscription-preferences-lifecycle.spec.ts b/libs/model/test/subscription/subscription-preferences-lifecycle.spec.ts index 49ab154afcf..837d0989c30 100644 --- a/libs/model/test/subscription/subscription-preferences-lifecycle.spec.ts +++ b/libs/model/test/subscription/subscription-preferences-lifecycle.spec.ts @@ -1,5 +1,6 @@ -import { Actor, command, dispose, query } from '@hicommonwealth/core'; +import { Actor, command, dispose, query, schemas } from '@hicommonwealth/core'; import { expect } from 'chai'; +import z from 'zod'; import { models } from '../../src/database'; import { GetSubscriptionPreferences, @@ -9,7 +10,9 @@ import { seed } from '../../src/tester'; describe('Subscription preferences lifecycle', () => { let actor: Actor; - let subPreferences; + let subPreferences: + | z.infer + | undefined; before(async () => { const [user] = await seed('User', { isAdmin: false, @@ -17,7 +20,7 @@ describe('Subscription preferences lifecycle', () => { }); actor = { user: { id: user!.id!, email: user!.email! }, - address_id: null, + address_id: '0x', }; }); @@ -52,7 +55,7 @@ describe('Subscription preferences lifecycle', () => { }); expect(res).to.deep.contains({ - id: subPreferences.id, + id: subPreferences!.id, user_id: actor.user.id, digest_email_enabled: false, recap_email_enabled: false, @@ -75,7 +78,7 @@ describe('Subscription preferences lifecycle', () => { }); expect(res).to.deep.contains({ - id: subPreferences.id, + id: subPreferences!.id, user_id: actor.user.id, recap_email_enabled: false, mobile_push_notifications_enabled: false, @@ -92,7 +95,7 @@ describe('Subscription preferences lifecycle', () => { }); expect(res).to.deep.contains({ - id: subPreferences.id, + id: subPreferences!.id, user_id: actor.user.id, email_notifications_enabled: false, digest_email_enabled: false, diff --git a/libs/model/test/subscription/thread-subscription-lifecycle.spec.ts b/libs/model/test/subscription/thread-subscription-lifecycle.spec.ts index 2e880ed11c2..9b92aa1491a 100644 --- a/libs/model/test/subscription/thread-subscription-lifecycle.spec.ts +++ b/libs/model/test/subscription/thread-subscription-lifecycle.spec.ts @@ -1,6 +1,7 @@ -import { Actor, command, dispose, query } from '@hicommonwealth/core'; +import { Actor, command, dispose, query, schemas } from '@hicommonwealth/core'; import { BalanceType } from '@hicommonwealth/shared'; import { expect } from 'chai'; +import z from 'zod'; import { models } from '../../src/database'; import { CreateThreadSubscription, @@ -11,7 +12,8 @@ import { seed } from '../../src/tester'; describe('Thread subscription lifecycle', () => { let actor: Actor; - let threadOne, threadTwo; + let threadOne: z.infer | undefined; + let threadTwo: z.infer | undefined; before(async () => { const [user] = await seed('User', { isAdmin: false, @@ -40,18 +42,18 @@ describe('Thread subscription lifecycle', () => { }); [threadOne] = await seed('Thread', { - address_id: community.Addresses[0].id, + address_id: community!.Addresses![0].id, community_id: community?.id, - topic_id: community.topics[0].id, + topic_id: community!.topics![0].id, }); [threadTwo] = await seed('Thread', { - address_id: community.Addresses[0].id, + address_id: community!.Addresses![0].id, community_id: community?.id, - topic_id: community.topics[0].id, + topic_id: community!.topics![0].id, }); actor = { user: { id: user!.id!, email: user!.email! }, - address_id: null, + address_id: '0x', }; }); @@ -65,7 +67,7 @@ describe('Thread subscription lifecycle', () => { it('should create a new thread subscription', async () => { const payload = { - thread_id: threadOne.id, + thread_id: threadOne!.id!, }; const res = await command(CreateThreadSubscription(), { payload, @@ -73,15 +75,15 @@ describe('Thread subscription lifecycle', () => { }); expect(res).to.deep.contains({ user_id: actor.user.id, - thread_id: threadOne.id, + thread_id: threadOne!.id, }); }); it('should get thread subscriptions', async () => { const [threadSubOne, threadSubTwo] = await models.ThreadSubscription.bulkCreate([ - { user_id: actor.user.id, thread_id: threadOne.id }, - { user_id: actor.user.id, thread_id: threadTwo.id }, + { user_id: actor.user.id!, thread_id: threadOne!.id! }, + { user_id: actor.user.id!, thread_id: threadTwo!.id! }, ]); const res = await query(GetThreadSubscriptions(), { @@ -104,12 +106,12 @@ describe('Thread subscription lifecycle', () => { it('should delete a thread subscriptions', async () => { await models.ThreadSubscription.bulkCreate([ - { user_id: actor.user.id, thread_id: threadOne.id }, - { user_id: actor.user.id, thread_id: threadTwo.id }, + { user_id: actor.user.id!, thread_id: threadOne!.id! }, + { user_id: actor.user.id!, thread_id: threadTwo!.id! }, ]); const payload = { - thread_ids: [threadOne.id, threadTwo.id], + thread_ids: [threadOne!.id!, threadTwo!.id!], }; const res = await command(DeleteThreadSubscription(), { diff --git a/libs/model/tsconfig.json b/libs/model/tsconfig.json index 3cf966d7cf4..3c43903cfdd 100644 --- a/libs/model/tsconfig.json +++ b/libs/model/tsconfig.json @@ -1,4 +1,3 @@ { - "extends": "../tsconfig.json", - "include": ["src"] + "extends": "../tsconfig.json" }