-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Order project list by edited at (#623)
* Order project list by edited at * Add tests
- Loading branch information
Showing
6 changed files
with
223 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 117 additions & 66 deletions
183
packages/core/src/repositories/projectsRepository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,161 @@ | ||
import { and, eq } from 'drizzle-orm' | ||
import { beforeEach, describe, expect, it } from 'vitest' | ||
|
||
import { Workspace } from '../browser' | ||
import { ProviderApiKey, User, Workspace } from '../browser' | ||
import { database } from '../client' | ||
import { Providers } from '../constants' | ||
import { createProject, createWorkspace, helpers } from '../tests/factories' | ||
import { documentVersions } from '../schema' | ||
import { mergeCommit } from '../services/commits' | ||
import { | ||
createDocumentVersion, | ||
createDraft, | ||
createProject, | ||
createProviderApiKey, | ||
createWorkspace, | ||
destroyDocumentVersion, | ||
helpers, | ||
updateDocumentVersion, | ||
} from '../tests/factories' | ||
import { ProjectsRepository } from './projectsRepository' | ||
|
||
describe('ProjectsRepository', async () => { | ||
let repository: ProjectsRepository | ||
let workspace: Workspace | ||
|
||
const provider = { type: Providers.OpenAI, name: 'OpenAI' } | ||
let user: User | ||
let provider: ProviderApiKey | ||
|
||
beforeEach(async () => { | ||
const { workspace: newWorkspace } = await createWorkspace() | ||
workspace = newWorkspace | ||
const { workspace: w, userData: u } = await createWorkspace() | ||
workspace = w | ||
user = u | ||
provider = await createProviderApiKey({ | ||
workspace, | ||
type: Providers.OpenAI, | ||
name: 'OpenAI', | ||
user, | ||
}) | ||
repository = new ProjectsRepository(workspace.id) | ||
}) | ||
|
||
describe('findAllActiveDocumentsWithAgreggatedData', () => { | ||
it('should return active projects with aggregated data', async () => { | ||
// Create test data | ||
const { project: project1 } = await createProject({ | ||
describe('findAllActiveWithAgreggatedData', () => { | ||
it('returns active projects ordered by lastEditedAt and createdAt', async () => { | ||
// When there are no projects | ||
let result = await repository.findAllActiveWithAgreggatedData() | ||
|
||
expect(result.ok).toBe(true) | ||
expect(result.unwrap()).toEqual([]) | ||
|
||
// After creating project0 and project1 | ||
let { project: project0, commit: commit0 } = await createProject({ | ||
name: 'project0', | ||
workspace, | ||
providers: [provider], | ||
documents: { | ||
foo: helpers.createPrompt({ provider: provider.name }), | ||
}, | ||
documents: {}, | ||
skipMerge: true, | ||
}) | ||
|
||
const { project: project2 } = await createProject({ | ||
let { project: project1, commit: commit1 } = await createProject({ | ||
name: 'project1', | ||
workspace, | ||
documents: { | ||
bar: helpers.createPrompt({ provider: provider.name }), | ||
}, | ||
documents: {}, | ||
skipMerge: true, | ||
}) | ||
|
||
// Execute the method | ||
const result = await repository.findAllActiveDocumentsWithAgreggatedData() | ||
result = await repository.findAllActiveWithAgreggatedData() | ||
|
||
// Assert the result | ||
expect(result.ok).toBe(true) | ||
const projects = result.unwrap() | ||
|
||
expect(projects).toHaveLength(2) | ||
|
||
const project1Result = projects.find((p) => p.id === project1.id) | ||
expect(project1Result).toBeDefined() | ||
expect(project1Result?.documentCount).toBe(1) | ||
expect(project1Result?.lastCreatedAtDocument).toBeDefined() | ||
expect(result.unwrap()).toEqual([ | ||
{ ...project1, lastEditedAt: null }, | ||
{ ...project0, lastEditedAt: null }, | ||
]) | ||
|
||
const project2Result = projects.find((p) => p.id === project2.id) | ||
expect(project2Result).toBeDefined() | ||
expect(project2Result?.documentCount).toBe(1) | ||
expect(project2Result?.lastCreatedAtDocument).toBeDefined() | ||
}) | ||
|
||
it('should return projects with zero document count when no documents exist', async () => { | ||
const { project } = await createProject({ | ||
// After adding a document to project1 and modifying it | ||
let { documentVersion: document1 } = await createDocumentVersion({ | ||
workspace, | ||
providers: [provider], | ||
user, | ||
commit: commit1, | ||
path: 'document1', | ||
content: helpers.createPrompt({ provider, content: 'content1' }), | ||
}) | ||
document1 = await updateDocumentVersion({ | ||
document: document1, | ||
commit: commit1, | ||
content: helpers.createPrompt({ provider, content: 'newContent1' }), | ||
}) | ||
|
||
const result = await repository.findAllActiveDocumentsWithAgreggatedData() | ||
result = await repository.findAllActiveWithAgreggatedData() | ||
|
||
expect(result.ok).toBe(true) | ||
const projects = result.unwrap() | ||
expect(result.unwrap()).toEqual([ | ||
{ ...project1, lastEditedAt: document1.updatedAt }, | ||
{ ...project0, lastEditedAt: null }, | ||
]) | ||
|
||
// After publishing project1 and adding a document to project0 | ||
commit1 = await mergeCommit(commit1).then((r) => r.unwrap()) | ||
document1 = await database | ||
.select() | ||
.from(documentVersions) | ||
.where( | ||
and( | ||
eq(documentVersions.documentUuid, document1.documentUuid), | ||
eq(documentVersions.commitId, commit1.id), | ||
), | ||
) | ||
.then((d) => d[0]!) | ||
let { documentVersion: document0 } = await createDocumentVersion({ | ||
workspace, | ||
user, | ||
commit: commit0, | ||
path: 'document0', | ||
content: helpers.createPrompt({ provider, content: 'content0' }), | ||
}) | ||
|
||
expect(projects).toHaveLength(1) | ||
expect(projects[0]?.id).toBe(project.id) | ||
expect(projects[0]?.documentCount).toBe(0) | ||
expect(projects[0]?.lastCreatedAtDocument).toBeNull() | ||
}) | ||
result = await repository.findAllActiveWithAgreggatedData() | ||
|
||
it('should include projects without merged commits', async () => { | ||
await createProject({ | ||
expect(result.ok).toBe(true) | ||
expect(result.unwrap()).toEqual([ | ||
{ ...project0, lastEditedAt: document0.updatedAt }, | ||
{ ...project1, lastEditedAt: document1.updatedAt }, | ||
]) | ||
|
||
// After creating project2 and deleting a document from project1 | ||
commit1 = await createDraft({ project: project1, user }).then( | ||
(c) => c.commit, | ||
) | ||
let { project: project2 } = await createProject({ | ||
name: 'project2', | ||
workspace, | ||
providers: [provider], | ||
documents: {}, | ||
skipMerge: true, | ||
}) | ||
document1 = await destroyDocumentVersion({ | ||
document: document1, | ||
commit: commit1, | ||
}).then((d) => d!) | ||
|
||
const result = await repository.findAllActiveDocumentsWithAgreggatedData() | ||
result = await repository.findAllActiveWithAgreggatedData() | ||
|
||
expect(result.ok).toBe(true) | ||
const projects = result.unwrap() | ||
|
||
expect(projects).toHaveLength(1) | ||
}) | ||
expect(result.unwrap()).toEqual([ | ||
{ ...project1, lastEditedAt: document1.updatedAt }, | ||
{ ...project2, lastEditedAt: null }, | ||
{ ...project0, lastEditedAt: document0.updatedAt }, | ||
]) | ||
|
||
it('should not include deleted projects', async () => { | ||
const { project: deletedProject } = await createProject({ | ||
// After creating project3 and deleting it | ||
await createProject({ | ||
name: 'project3', | ||
workspace, | ||
deletedAt: new Date(), | ||
}) | ||
|
||
const { project: activeProject } = await createProject({ | ||
workspace, | ||
}) | ||
|
||
const result = await repository.findAllActiveDocumentsWithAgreggatedData() | ||
result = await repository.findAllActiveWithAgreggatedData() | ||
|
||
expect(result.ok).toBe(true) | ||
const projects = result.unwrap() | ||
|
||
expect(projects).toHaveLength(1) | ||
expect(projects[0]?.id).toBe(activeProject.id) | ||
expect(projects.find((p) => p.id === deletedProject.id)).toBeUndefined() | ||
expect(result.unwrap()).toEqual([ | ||
{ ...project1, lastEditedAt: document1.updatedAt }, | ||
{ ...project2, lastEditedAt: null }, | ||
{ ...project0, lastEditedAt: document0.updatedAt }, | ||
]) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.