-
Notifications
You must be signed in to change notification settings - Fork 1
feat(diff): implement LCS-based line diff for prompt versions (closes #7) #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7f2c67d
cc343df
2366a31
2cad22c
822f8d5
4d998e4
491534f
472d929
952635e
b122eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module.exports = { | ||
| moduleFileExtensions: ['js', 'json', 'ts'], | ||
| rootDir: 'src', | ||
| testRegex: '.*\\.spec\\.ts$', | ||
| transform: { | ||
| '^.+\\.(t|j)s$': 'ts-jest', | ||
| }, | ||
| collectCoverageFrom: ['**/*.(t|j)s'], | ||
| coverageDirectory: '../coverage', | ||
| testEnvironment: 'node', | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { DiffService } from './diff.service'; | ||
|
|
||
| describe('DiffService', () => { | ||
| const mockPrisma = {} as any; | ||
| const service = new DiffService(mockPrisma); | ||
|
|
||
| it('identical content → all equal', () => { | ||
| const res = service['computeDiff']('a\nb', 'a\nb'); | ||
| expect(res.length).toBe(2); | ||
| expect(res.every(l => l.type === 'equal')).toBe(true); | ||
| }); | ||
|
|
||
| it('empty from → all inserts', () => { | ||
| const res = service['computeDiff']('', 'a\nb'); | ||
| expect(res.length).toBe(2); | ||
| expect(res.every(l => l.type === 'insert')).toBe(true); | ||
| }); | ||
|
|
||
| it('empty to → all deletes', () => { | ||
| const res = service['computeDiff']('a\nb', ''); | ||
| expect(res.length).toBe(2); | ||
| expect(res.every(l => l.type === 'delete')).toBe(true); | ||
| }); | ||
|
|
||
| it('completely different → delete + insert', () => { | ||
| const res = service['computeDiff']('a\nb', 'x\ny'); | ||
| expect(res.some(l => l.type === 'delete')).toBe(true); | ||
| expect(res.some(l => l.type === 'insert')).toBe(true); | ||
| }); | ||
|
|
||
| it('stats match line count', () => { | ||
| const res = service['computeDiff']('a\nb', 'a\nc'); | ||
|
|
||
| const stats = { | ||
| added: res.filter(l => l.type === 'insert').length, | ||
| removed: res.filter(l => l.type === 'delete').length, | ||
| unchanged: res.filter(l => l.type === 'equal').length, | ||
| }; | ||
|
|
||
| expect(stats.added + stats.removed + stats.unchanged).toBe(res.length); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { Controller, Get, Param, Query } from '@nestjs/common'; | ||
| import { DiffService } from './diff.service'; | ||
| import { IsString, IsNotEmpty } from 'class-validator'; | ||
|
|
||
| class DiffQueryDto { | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| from: string; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| to: string; | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocker — DTO validation will break every request. The global Quick fix: import { IsString, IsNotEmpty } from 'class-validator';
class DiffQueryDto {
@IsString()
@IsNotEmpty()
from: string;
@IsString()
@IsNotEmpty()
to: string;
}This will also give callers a proper |
||
|
|
||
| @Controller('prompts') | ||
| export class PromptsController { | ||
| constructor(private readonly diffService: DiffService) {} | ||
|
|
||
| @Get(':id/diff') | ||
| diff( | ||
| @Param('id') id: string, | ||
| @Query() query: DiffQueryDto, | ||
| ) { | ||
| return this.diffService.diffVersions(id, query.from, query.to); | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.