2023-12-14 12:50:41 +03:00
|
|
|
/// <reference types="../src/global.d.ts" />
|
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
import { INestApplication, Injectable } from '@nestjs/common';
|
2024-01-12 07:18:39 +03:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
2024-03-25 06:55:32 +03:00
|
|
|
import type { TestFn } from 'ava';
|
|
|
|
import ava from 'ava';
|
2023-12-14 12:50:41 +03:00
|
|
|
|
2024-01-22 10:40:28 +03:00
|
|
|
import { AuthService } from '../src/core/auth/service';
|
2023-12-14 12:50:41 +03:00
|
|
|
import {
|
|
|
|
FeatureManagementService,
|
|
|
|
FeatureModule,
|
|
|
|
FeatureService,
|
|
|
|
FeatureType,
|
2024-01-22 10:40:28 +03:00
|
|
|
} from '../src/core/features';
|
2024-03-12 13:00:09 +03:00
|
|
|
import { UserType } from '../src/core/user/types';
|
2024-01-22 10:40:28 +03:00
|
|
|
import { WorkspaceResolver } from '../src/core/workspaces/resolvers';
|
|
|
|
import { Permission } from '../src/core/workspaces/types';
|
|
|
|
import { ConfigModule } from '../src/fundamentals/config';
|
2024-01-11 09:40:55 +03:00
|
|
|
import { createTestingApp } from './utils';
|
2023-12-14 12:50:41 +03:00
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
@Injectable()
|
|
|
|
class WorkspaceResolverMock {
|
2024-01-12 07:18:39 +03:00
|
|
|
constructor(private readonly prisma: PrismaClient) {}
|
2024-01-05 07:13:47 +03:00
|
|
|
|
|
|
|
async createWorkspace(user: UserType, _init: null) {
|
|
|
|
const workspace = await this.prisma.workspace.create({
|
|
|
|
data: {
|
|
|
|
public: false,
|
|
|
|
permissions: {
|
|
|
|
create: {
|
|
|
|
type: Permission.Owner,
|
|
|
|
user: {
|
|
|
|
connect: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:50:41 +03:00
|
|
|
const test = ava as TestFn<{
|
|
|
|
auth: AuthService;
|
|
|
|
feature: FeatureService;
|
2024-01-05 07:13:47 +03:00
|
|
|
workspace: WorkspaceResolver;
|
|
|
|
management: FeatureManagementService;
|
2024-01-11 09:40:55 +03:00
|
|
|
app: INestApplication;
|
2023-12-14 12:50:41 +03:00
|
|
|
}>;
|
|
|
|
|
|
|
|
test.beforeEach(async t => {
|
2024-01-11 09:40:55 +03:00
|
|
|
const { app } = await createTestingApp({
|
2023-12-14 12:50:41 +03:00
|
|
|
imports: [
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
host: 'example.org',
|
|
|
|
https: true,
|
|
|
|
featureFlags: {
|
|
|
|
earlyAccessPreview: true,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
FeatureModule,
|
|
|
|
],
|
2024-01-05 07:13:47 +03:00
|
|
|
providers: [WorkspaceResolver],
|
2024-01-11 09:40:55 +03:00
|
|
|
tapModule: module => {
|
|
|
|
module
|
|
|
|
.overrideProvider(WorkspaceResolver)
|
|
|
|
.useClass(WorkspaceResolverMock);
|
|
|
|
},
|
|
|
|
});
|
2023-12-14 12:50:41 +03:00
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
t.context.app = app;
|
|
|
|
t.context.auth = app.get(AuthService);
|
|
|
|
t.context.feature = app.get(FeatureService);
|
|
|
|
t.context.workspace = app.get(WorkspaceResolver);
|
|
|
|
t.context.management = app.get(FeatureManagementService);
|
2023-12-14 12:50:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test.afterEach.always(async t => {
|
|
|
|
await t.context.app.close();
|
|
|
|
});
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
test('should be able to set user feature', async t => {
|
2023-12-14 12:50:41 +03:00
|
|
|
const { auth, feature } = t.context;
|
|
|
|
|
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
|
|
|
|
const f1 = await feature.getUserFeatures(u1.id);
|
|
|
|
t.is(f1.length, 0, 'should be empty');
|
|
|
|
|
2024-04-16 12:41:48 +03:00
|
|
|
await feature.addUserFeature(u1.id, FeatureType.EarlyAccess, 'test');
|
2023-12-14 12:50:41 +03:00
|
|
|
|
|
|
|
const f2 = await feature.getUserFeatures(u1.id);
|
2023-12-14 12:50:51 +03:00
|
|
|
t.is(f2.length, 1, 'should have 1 feature');
|
|
|
|
t.is(f2[0].feature.name, FeatureType.EarlyAccess, 'should be early access');
|
2023-12-14 12:50:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to check early access', async t => {
|
2024-01-05 07:13:47 +03:00
|
|
|
const { auth, feature, management } = t.context;
|
2023-12-14 12:50:41 +03:00
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
const f1 = await management.canEarlyAccess(u1.email);
|
2023-12-14 12:50:41 +03:00
|
|
|
t.false(f1, 'should not have early access');
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
await management.addEarlyAccess(u1.id);
|
|
|
|
const f2 = await management.canEarlyAccess(u1.email);
|
2023-12-14 12:50:41 +03:00
|
|
|
t.true(f2, 'should have early access');
|
|
|
|
|
|
|
|
const f3 = await feature.listFeatureUsers(FeatureType.EarlyAccess);
|
2023-12-14 12:50:51 +03:00
|
|
|
t.is(f3.length, 1, 'should have 1 user');
|
2023-12-14 12:50:41 +03:00
|
|
|
t.is(f3[0].id, u1.id, 'should be the same user');
|
|
|
|
});
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
test('should be able revert user feature', async t => {
|
|
|
|
const { auth, feature, management } = t.context;
|
2023-12-14 12:50:41 +03:00
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
const f1 = await management.canEarlyAccess(u1.email);
|
2023-12-14 12:50:41 +03:00
|
|
|
t.false(f1, 'should not have early access');
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
await management.addEarlyAccess(u1.id);
|
|
|
|
const f2 = await management.canEarlyAccess(u1.email);
|
2023-12-14 12:50:41 +03:00
|
|
|
t.true(f2, 'should have early access');
|
2024-01-05 07:13:47 +03:00
|
|
|
const q1 = await management.listEarlyAccess();
|
2023-12-14 12:50:51 +03:00
|
|
|
t.is(q1.length, 1, 'should have 1 user');
|
2023-12-14 12:50:41 +03:00
|
|
|
t.is(q1[0].id, u1.id, 'should be the same user');
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
await management.removeEarlyAccess(u1.id);
|
|
|
|
const f3 = await management.canEarlyAccess(u1.email);
|
2023-12-14 12:50:41 +03:00
|
|
|
t.false(f3, 'should not have early access');
|
2024-01-05 07:13:47 +03:00
|
|
|
const q2 = await management.listEarlyAccess();
|
2023-12-14 12:50:41 +03:00
|
|
|
t.is(q2.length, 0, 'should have no user');
|
|
|
|
|
|
|
|
const q3 = await feature.getUserFeatures(u1.id);
|
|
|
|
t.is(q3.length, 1, 'should have 1 feature');
|
2023-12-14 12:50:51 +03:00
|
|
|
t.is(q3[0].feature.name, FeatureType.EarlyAccess, 'should be early access');
|
2023-12-14 12:50:41 +03:00
|
|
|
t.is(q3[0].activated, false, 'should be deactivated');
|
|
|
|
});
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
test('should be same instance after reset the user feature', async t => {
|
|
|
|
const { auth, feature, management } = t.context;
|
2023-12-14 12:50:51 +03:00
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
await management.addEarlyAccess(u1.id);
|
2023-12-14 12:50:51 +03:00
|
|
|
const f1 = (await feature.getUserFeatures(u1.id))[0];
|
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
await management.removeEarlyAccess(u1.id);
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-01-05 07:13:47 +03:00
|
|
|
await management.addEarlyAccess(u1.id);
|
2023-12-14 12:50:51 +03:00
|
|
|
const f2 = (await feature.getUserFeatures(u1.id))[1];
|
|
|
|
|
|
|
|
t.is(f1.feature, f2.feature, 'should be same instance');
|
|
|
|
});
|
2024-01-05 07:13:47 +03:00
|
|
|
|
|
|
|
test('should be able to set workspace feature', async t => {
|
|
|
|
const { auth, feature, workspace } = t.context;
|
|
|
|
|
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
const w1 = await workspace.createWorkspace(u1, null);
|
|
|
|
|
|
|
|
const f1 = await feature.getWorkspaceFeatures(w1.id);
|
|
|
|
t.is(f1.length, 0, 'should be empty');
|
|
|
|
|
|
|
|
await feature.addWorkspaceFeature(w1.id, FeatureType.Copilot, 1, 'test');
|
|
|
|
|
|
|
|
const f2 = await feature.getWorkspaceFeatures(w1.id);
|
|
|
|
t.is(f2.length, 1, 'should have 1 feature');
|
|
|
|
t.is(f2[0].feature.name, FeatureType.Copilot, 'should be copilot');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to check workspace feature', async t => {
|
|
|
|
const { auth, feature, workspace, management } = t.context;
|
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
const w1 = await workspace.createWorkspace(u1, null);
|
|
|
|
|
|
|
|
const f1 = await management.hasWorkspaceFeature(w1.id, FeatureType.Copilot);
|
|
|
|
t.false(f1, 'should not have copilot');
|
|
|
|
|
|
|
|
await management.addWorkspaceFeatures(w1.id, FeatureType.Copilot, 1, 'test');
|
|
|
|
const f2 = await management.hasWorkspaceFeature(w1.id, FeatureType.Copilot);
|
|
|
|
t.true(f2, 'should have copilot');
|
|
|
|
|
|
|
|
const f3 = await feature.listFeatureWorkspaces(FeatureType.Copilot);
|
|
|
|
t.is(f3.length, 1, 'should have 1 workspace');
|
|
|
|
t.is(f3[0].id, w1.id, 'should be the same workspace');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able revert workspace feature', async t => {
|
|
|
|
const { auth, feature, workspace, management } = t.context;
|
|
|
|
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
|
|
|
|
const w1 = await workspace.createWorkspace(u1, null);
|
|
|
|
|
|
|
|
const f1 = await management.hasWorkspaceFeature(w1.id, FeatureType.Copilot);
|
|
|
|
t.false(f1, 'should not have feature');
|
|
|
|
|
|
|
|
await management.addWorkspaceFeatures(w1.id, FeatureType.Copilot, 1, 'test');
|
|
|
|
const f2 = await management.hasWorkspaceFeature(w1.id, FeatureType.Copilot);
|
|
|
|
t.true(f2, 'should have feature');
|
|
|
|
|
|
|
|
await management.removeWorkspaceFeature(w1.id, FeatureType.Copilot);
|
|
|
|
const f3 = await management.hasWorkspaceFeature(w1.id, FeatureType.Copilot);
|
|
|
|
t.false(f3, 'should not have feature');
|
|
|
|
|
|
|
|
const q3 = await feature.getWorkspaceFeatures(w1.id);
|
|
|
|
t.is(q3.length, 1, 'should have 1 feature');
|
|
|
|
t.is(q3[0].feature.name, FeatureType.Copilot, 'should be copilot');
|
|
|
|
t.is(q3[0].activated, false, 'should be deactivated');
|
|
|
|
});
|