Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-takei committed Dec 23, 2024
1 parent 9147398 commit 84fd05f
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IGrowiInfo } from '@growi/core/dist/interfaces';

import type { IGrowiAppAdditionalInfo } from './growi-app-info';
import type { IGrowiAppAdditionalInfo, IGrowiAppInfoLegacy } from './growi-app-info';
import type { IUserInfo } from './user-info';


Expand All @@ -14,3 +14,14 @@ export interface IProactiveQuestionnaireAnswer {
position?: string,
occupation?: string,
}

export interface IProactiveQuestionnaireAnswerLegacy {
satisfaction: number,
commentText: string,
growiInfo: IGrowiAppInfoLegacy,
userInfo: IUserInfo,
answeredAt: Date,
lengthOfExperience?: string,
position?: string,
occupation?: string,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IGrowiInfo } from '@growi/core/dist/interfaces';

import type { IAnswer } from './answer';
import type { IGrowiAppAdditionalInfo } from './growi-app-info';
import type { IGrowiAppAdditionalInfo, IGrowiAppInfoLegacy } from './growi-app-info';
import type { IUserInfo } from './user-info';

export interface IQuestionnaireAnswer<ID = string> {
Expand All @@ -11,3 +11,11 @@ export interface IQuestionnaireAnswer<ID = string> {
userInfo: IUserInfo
questionnaireOrder: ID
}

export interface IQuestionnaireAnswerLegacy<ID = string> {
answers: IAnswer[]
answeredAt: Date
growiInfo: IGrowiAppInfoLegacy,
userInfo: IUserInfo
questionnaireOrder: ID
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ class QuestionnaireCronService {

const resendQuestionnaireAnswers = async() => {
const questionnaireAnswers = await QuestionnaireAnswer.find()
.select('-_id -answers._id -growiInfo._id -userInfo._id');
.select('-_id -answers._id -growiInfo._id -userInfo._id')
.lean();
const proactiveQuestionnaireAnswers = await ProactiveQuestionnaireAnswer.find()
.select('-_id -growiInfo._id -userInfo._id');
.select('-_id -growiInfo._id -userInfo._id')
.lean();

axios.post(`${questionnaireServerOrigin}/questionnaire-answer/batch`, {
// convert to legacy format
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from 'assert';

import type { IGrowiAppInfoLegacy } from '../../interfaces/growi-app-info';


Expand All @@ -6,24 +8,24 @@ type IHasGrowiAppInfoLegacy<T> = T & {
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function hasGrowiAppInfoLegacy<T extends { growiInfo: any }>(data: T): data is IHasGrowiAppInfoLegacy<T> {
function isLegacy<T extends { growiInfo: any }>(data: T): data is IHasGrowiAppInfoLegacy<T> {
return !('additionalInfo' in data.growiInfo);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function convertToLegacyFormat<T extends { growiInfo: any }>(questionnaireAnswer: T): IHasGrowiAppInfoLegacy<T> {
if (!hasGrowiAppInfoLegacy(questionnaireAnswer)) {
const { additionalInfo, ...rest } = questionnaireAnswer.growiInfo;
assert(additionalInfo != null);

return {
...questionnaireAnswer,
growiInfo: {
...rest,
...additionalInfo,
},
};
if (isLegacy(questionnaireAnswer)) {
return questionnaireAnswer;
}

return questionnaireAnswer;
const { additionalInfo, ...rest } = questionnaireAnswer.growiInfo;
assert(additionalInfo != null);

return {
...questionnaireAnswer,
growiInfo: {
...rest,
...additionalInfo,
},
};
}
6 changes: 6 additions & 0 deletions apps/app/src/server/crowi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class Crowi {
/** @type {PassportService} */
passportService;

/** @type {QuestionnaireService} */
questionnaireService;

/** @type {QuestionnaireCronService} */
questionnaireCronService;

/** @type {SearchService} */
searchService;

Expand Down
77 changes: 69 additions & 8 deletions apps/app/test/integration/service/questionnaire-cron.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { GrowiDeploymentType, GrowiServiceType, GrowiWikiType } from '@growi/core';
// eslint-disable-next-line no-restricted-imports
import axios from 'axios';
import mongoose from 'mongoose';

import { IProactiveQuestionnaireAnswer } from '../../../src/features/questionnaire/interfaces/proactive-questionnaire-answer';
import { IQuestionnaireAnswer } from '../../../src/features/questionnaire/interfaces/questionnaire-answer';
import type { IProactiveQuestionnaireAnswer, IProactiveQuestionnaireAnswerLegacy } from '../../../src/features/questionnaire/interfaces/proactive-questionnaire-answer';

Check failure on line 6 in apps/app/test/integration/service/questionnaire-cron.test.ts

View workflow job for this annotation

GitHub Actions / ci-app-lint (20.x)

This line has a length of 168. Maximum allowed is 160

Check failure on line 6 in apps/app/test/integration/service/questionnaire-cron.test.ts

View workflow job for this annotation

GitHub Actions / ci-app-lint (20.x)

This line has a length of 168. Maximum allowed is 160
import type { IQuestionnaireAnswer, IQuestionnaireAnswerLegacy } from '../../../src/features/questionnaire/interfaces/questionnaire-answer';
import { StatusType } from '../../../src/features/questionnaire/interfaces/questionnaire-answer-status';
import ProactiveQuestionnaireAnswer from '../../../src/features/questionnaire/server/models/proactive-questionnaire-answer';
import QuestionnaireAnswer from '../../../src/features/questionnaire/server/models/questionnaire-answer';
import QuestionnaireAnswerStatus from '../../../src/features/questionnaire/server/models/questionnaire-answer-status';
import QuestionnaireOrder from '../../../src/features/questionnaire/server/models/questionnaire-order';
import { AttachmentMethodType } from '../../../src/interfaces/attachment';
import { getInstance } from '../setup-crowi';

const spyAxiosGet = jest.spyOn<typeof axios, 'get'>(
Expand Down Expand Up @@ -275,13 +277,42 @@ describe('QuestionnaireCronService', () => {
growiInfo: {
version: '1.0',
appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
type: GrowiServiceType.cloud,
wikiType: GrowiWikiType.open,
deploymentType: GrowiDeploymentType.others,
additionalInfo: {
installedAt: new Date('2000-01-01'),
installedAtByOldestUser: new Date('2020-01-01'),
currentUsersCount: 100,
currentActiveUsersCount: 50,
attachmentType: AttachmentMethodType.aws,
},
},
userInfo: {
userIdHash: '542bcc3bc5bc61b840017a18',
type: 'general',
userCreatedAt: new Date(),
},
questionnaireOrder: '63a8354837e7aa378e16f0b1',
};

const validQuestionnaireAnswerLegacy: IQuestionnaireAnswerLegacy = {
answers: [{
question: '63c6da88143e531d95346188',
value: '1',
}],
answeredAt: new Date(),
growiInfo: {
version: '1.0',
appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
type: GrowiServiceType.cloud,
wikiType: GrowiWikiType.open,
deploymentType: GrowiDeploymentType.others,
installedAt: new Date('2000-01-01'),
installedAtByOldestUser: new Date('2020-01-01'),
type: 'cloud',
currentUsersCount: 100,
currentActiveUsersCount: 50,
wikiType: 'open',
attachmentType: 'aws',
attachmentType: AttachmentMethodType.aws,
},
userInfo: {
userIdHash: '542bcc3bc5bc61b840017a18',
Expand All @@ -295,6 +326,8 @@ describe('QuestionnaireCronService', () => {
validQuestionnaireAnswer,
validQuestionnaireAnswer,
validQuestionnaireAnswer,
validQuestionnaireAnswerLegacy,
validQuestionnaireAnswerLegacy,
]);

const validProactiveQuestionnaireAnswer: IProactiveQuestionnaireAnswer = {
Expand All @@ -303,13 +336,39 @@ describe('QuestionnaireCronService', () => {
growiInfo: {
version: '1.0',
appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
type: GrowiServiceType.cloud,
wikiType: GrowiWikiType.open,
deploymentType: GrowiDeploymentType.others,
additionalInfo: {
installedAt: new Date('2000-01-01'),
installedAtByOldestUser: new Date('2020-01-01'),
currentUsersCount: 100,
currentActiveUsersCount: 50,
attachmentType: AttachmentMethodType.aws,
},
},
userInfo: {
userIdHash: '542bcc3bc5bc61b840017a18',
type: 'general',
userCreatedAt: new Date(),
},
answeredAt: new Date(),
};
const validProactiveQuestionnaireAnswerLegacy: IProactiveQuestionnaireAnswerLegacy = {
satisfaction: 1,
commentText: 'answer text',
growiInfo: {
version: '1.0',
appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
type: GrowiServiceType.cloud,
wikiType: GrowiWikiType.open,
deploymentType: GrowiDeploymentType.others,
// legacy properties
installedAt: new Date('2000-01-01'),
installedAtByOldestUser: new Date('2020-01-01'),
type: 'cloud',
currentUsersCount: 100,
currentActiveUsersCount: 50,
wikiType: 'open',
attachmentType: 'aws',
attachmentType: AttachmentMethodType.aws,
},
userInfo: {
userIdHash: '542bcc3bc5bc61b840017a18',
Expand All @@ -323,6 +382,8 @@ describe('QuestionnaireCronService', () => {
validProactiveQuestionnaireAnswer,
validProactiveQuestionnaireAnswer,
validProactiveQuestionnaireAnswer,
validProactiveQuestionnaireAnswerLegacy,
validProactiveQuestionnaireAnswerLegacy,
]);

crowi.setupCron();
Expand Down
21 changes: 13 additions & 8 deletions apps/app/test/integration/service/questionnaire.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import mongoose from 'mongoose';
import { StatusType } from '../../../src/features/questionnaire/interfaces/questionnaire-answer-status';
import QuestionnaireAnswerStatus from '../../../src/features/questionnaire/server/models/questionnaire-answer-status';
import QuestionnaireOrder from '../../../src/features/questionnaire/server/models/questionnaire-order';
import type Crowi from '../../../src/server/crowi';
import { getInstance } from '../setup-crowi';

describe('QuestionnaireService', () => {
let crowi;
let crowi: Crowi;
let user;

beforeAll(async() => {
Expand Down Expand Up @@ -55,15 +56,19 @@ describe('QuestionnaireService', () => {
delete growiInfo.osInfo;

expect(growiInfo).toEqual({
activeExternalAccountTypes: ['saml', 'github'],
version: crowi.version,
appSiteUrl: 'http://growi.test.jp',
installedAt: new Date('2000-01-01'),
installedAtByOldestUser: new Date('2000-01-01'),
attachmentType: 'aws',
deploymentType: 'growi-docker-compose',
type: 'on-premise',
version: crowi.version,
wikiType: 'closed',
deploymentType: 'growi-docker-compose',
additionalInfo: {
installedAt: new Date('2000-01-01'),
installedAtByOldestUser: new Date('2000-01-01'),
currentUsersCount: 7,
currentActiveUsersCount: 1,
attachmentType: 'aws',
activeExternalAccountTypes: ['saml', 'github'],
},
});
});

Expand All @@ -75,7 +80,7 @@ describe('QuestionnaireService', () => {

test('Should return app url string', async() => {
const growiInfo = await crowi.questionnaireService.getGrowiInfo();
expect(growiInfo.appSiteUrl).toBe(null);
expect(growiInfo.appSiteUrl).toBeUndefined();
expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
expect(growiInfo.appSiteUrlHashed).toBeTruthy();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/interfaces/growi-app-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface IGrowiAdditionalInfo {

export interface IGrowiInfo<A extends IGrowiAdditionalInfo> {
version: string
appSiteUrl: string | undefined
appSiteUrl?: string
appSiteUrlHashed: string
type: GrowiServiceType
wikiType: GrowiWikiType
Expand Down

0 comments on commit 84fd05f

Please sign in to comment.