feat: 初始化DevOps平台后端
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { AppError } from '../common/errors/app-error';
|
||||
import { GiteaClient } from '../integrations/gitea/gitea.client';
|
||||
import { ProjectRepository } from './project.repository';
|
||||
import { ProjectSummary } from './project.types';
|
||||
import { ProjectsService } from './projects.service';
|
||||
|
||||
describe('ProjectsService', () => {
|
||||
const project: ProjectSummary = {
|
||||
id: 'project_access_manage',
|
||||
key: 'access-manage',
|
||||
name: 'access-manage',
|
||||
repositoryUrl: 'https://gitea.ops.mrzhan.top/my-project/access-manage.git',
|
||||
defaultBranch: 'develop',
|
||||
status: 'active',
|
||||
environments: [],
|
||||
};
|
||||
|
||||
function createService(options?: {
|
||||
giteaConfigured?: boolean;
|
||||
branches?: Array<{ name: string; commitSha: string }>;
|
||||
tags?: Array<{ name: string; commitSha: string }>;
|
||||
}) {
|
||||
const repository = {
|
||||
findMany: jest.fn(() => Promise.resolve([project])),
|
||||
findByKey: jest.fn((key: string) =>
|
||||
Promise.resolve(key === project.key ? project : null),
|
||||
),
|
||||
} satisfies Pick<ProjectRepository, 'findMany' | 'findByKey'>;
|
||||
const gitea = {
|
||||
healthSummary: jest.fn(() =>
|
||||
options?.giteaConfigured ? { status: 'ok' as const } : { status: 'not_configured' as const },
|
||||
),
|
||||
listBranches: jest.fn(() =>
|
||||
Promise.resolve(options?.branches ?? [{ name: 'develop', commitSha: 'abc' }]),
|
||||
),
|
||||
listTags: jest.fn(() =>
|
||||
Promise.resolve(options?.tags ?? [{ name: 'v1.0.0', commitSha: 'def' }]),
|
||||
),
|
||||
} satisfies Pick<GiteaClient, 'healthSummary' | 'listBranches' | 'listTags'>;
|
||||
|
||||
return {
|
||||
gitea,
|
||||
repository,
|
||||
service: new ProjectsService(
|
||||
repository as unknown as ProjectRepository,
|
||||
gitea as unknown as GiteaClient,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
it('returns fallback refs when Gitea is not configured', async () => {
|
||||
const { gitea, service } = createService({ giteaConfigured: false });
|
||||
|
||||
const refs = await service.listProjectRefs(project.key);
|
||||
|
||||
expect(refs.source).toBe('fallback');
|
||||
expect(refs.branches).toContainEqual({ name: 'develop' });
|
||||
expect(refs.tags).toContainEqual({ name: 'v2026.06.05-envsplit.7' });
|
||||
expect(gitea.listBranches).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns Gitea refs when Gitea is configured', async () => {
|
||||
const { gitea, service } = createService({
|
||||
giteaConfigured: true,
|
||||
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
|
||||
tags: [{ name: 'v2026.06.11.1', commitSha: 'def456' }],
|
||||
});
|
||||
|
||||
const refs = await service.listProjectRefs(project.key);
|
||||
|
||||
expect(refs).toEqual({
|
||||
source: 'gitea',
|
||||
branches: [{ name: 'feature/devops-platform', commitSha: 'abc123' }],
|
||||
tags: [{ name: 'v2026.06.11.1', commitSha: 'def456' }],
|
||||
});
|
||||
expect(gitea.listBranches).toHaveBeenCalledWith('my-project', 'access-manage');
|
||||
});
|
||||
|
||||
it('raises a not found error for unknown projects', async () => {
|
||||
const { service } = createService();
|
||||
|
||||
await expect(service.listProjectRefs('unknown')).rejects.toBeInstanceOf(AppError);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user