From 46e073a258259466e4b0d37b7c50dd17ceccc06c Mon Sep 17 00:00:00 2001 From: this-is-tobi Date: Mon, 16 Dec 2024 23:32:07 +0100 Subject: [PATCH 1/3] test: unskip some unit tests --- src/lib/fetch.spec.ts | 157 ++++++++++++++++++++++++++++-------------- 1 file changed, 107 insertions(+), 50 deletions(-) diff --git a/src/lib/fetch.spec.ts b/src/lib/fetch.spec.ts index 13b8a02..0a01a6c 100644 --- a/src/lib/fetch.spec.ts +++ b/src/lib/fetch.spec.ts @@ -1,7 +1,7 @@ import { writeFileSync } from 'node:fs' -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { checkHttpStatus, createDir } from '../utils/functions' -import { USER_INFOS, USER_REPOS_INFOS } from '../utils/const.js' +import { DOCPRESS_DIR, USER_INFOS, USER_REPOS_INFOS } from '../utils/const.js' import type { FetchOpts } from '../schemas/fetch.js' import type { EnhancedRepository } from './fetch.js' import { @@ -250,66 +250,123 @@ describe('generateInfos', () => { }) }) -describe.skip('fetchDoc', () => { - const mockUsername = 'testUser' - const mockBranch = 'main' - const mockToken = 'testToken' - const mockReposFilter = ['repo1', '!repo2'] - - const fetchOpts: FetchOpts = { - username: mockUsername, - gitProvider: 'github', - branch: mockBranch, - token: mockToken, - reposFilter: mockReposFilter, +describe('fetchDoc', () => { + const mockFetchOpts: FetchOpts = { + username: 'testUser', + branch: 'main', + reposFilter: ['repo1', 'repo2'], + token: 'fake-token', } - it('should create the DOCPRESS_DIR and fetch and process repositories', async () => { - const mockUser = { login: mockUsername } - const mockRepos = [ - { - name: 'repo1', - owner: mockUser, - clone_url: 'https://github.com/testUser/repo1', - fork: false, - private: false, - docpress: { projectPath: '/path/to/repo1', branch: 'main', includes: ['README.md'] }, - }, - { - name: 'repo2', - owner: mockUser, - clone_url: 'https://github.com/testUser/repo2', - fork: true, - private: false, - docpress: { projectPath: '/path/to/repo2', branch: 'main', includes: [] }, + const mockUser = { login: 'testUser' } + const mockRepos = [ + { + name: 'repo1', + clone_url: 'https://github.com/testUser/repo1', + owner: mockUser, + fork: false, + private: false, + size: 100, + docpress: { projectPath: '/path/to/repo1', branch: 'main', includes: ['README.md'] }, + }, + { + name: 'repo2', + clone_url: 'https://github.com/testUser/repo2', + owner: mockUser, + fork: false, + private: false, + size: 100, + docpress: { projectPath: '/path/to/repo2', branch: 'main', includes: ['README.md'] }, + }, + ] + + beforeEach(() => { + vi.clearAllMocks() + }) + + it('should create the docpress directory', async () => { + ;(getInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos, branch: 'main' }) + ;(generateInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos }) + ;(getDoc as any).mockResolvedValue(undefined) + + await fetchDoc(mockFetchOpts) + + expect(createDir).toHaveBeenCalledWith(DOCPRESS_DIR, { clean: true }) + }) + + it('should call getInfos with correct parameters', async () => { + ;(getInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos, branch: 'main' }) + ;(generateInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos }) + ;(getDoc as any).mockResolvedValue(undefined) + + await fetchDoc(mockFetchOpts) + + expect(getInfos).toHaveBeenCalledWith({ + username: mockFetchOpts.username, + token: mockFetchOpts.token, + branch: mockFetchOpts.branch, + }) + }) + + it.skip('should call generateInfos and getDoc with the correct data', async () => { + const enhancedRepos = mockRepos.map(repo => ({ + ...repo, + docpress: { + filtered: false, + branch: 'main', + includes: ['docs/*'], + projectPath: `/path/to/${repo.name}`, + raw_url: `https://raw.githubusercontent.com/${repo.owner.login}/${repo.name}/main`, + replace_url: `https://github.com/${repo.owner.login}/${repo.name}/tree/main`, }, - ] as unknown as EnhancedRepository[] - const mockEnhancedRepos = [ - { ...mockRepos[0], docpress: { branch: mockBranch, filtered: false, includes: [], projectPath: '', raw_url: '', replace_url: '' } }, - ] + })) - ;(getInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos, branch: mockBranch }) + ;(getInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos, branch: 'main' }) + ;(generateInfos as any).mockResolvedValue({ user: mockUser, repos: enhancedRepos }) + ;(getDoc as any).mockResolvedValue(undefined) - await fetchDoc(fetchOpts) + await fetchDoc(mockFetchOpts) - expect(createDir).toHaveBeenCalledWith(expect.any(String), { clean: true }) - expect(getInfos).toHaveBeenCalledWith({ username: mockUsername, token: mockToken, branch: mockBranch }) - expect(generateInfos).toHaveBeenCalledWith(mockUser, mockRepos, mockBranch, mockReposFilter) - expect(getDoc).toHaveBeenCalledWith(mockEnhancedRepos, mockReposFilter) + expect(generateInfos).toHaveBeenCalledWith(mockUser, mockRepos, 'main', mockFetchOpts.reposFilter) + expect(getDoc).toHaveBeenCalledWith(enhancedRepos, mockFetchOpts.reposFilter) }) - it('should handle errors if getInfos fails', async () => { - ;(getInfos as any).mockRejectedValue(new Error('getInfos failed')) - console.error = vi.fn() + it.skip('should handle cases with no matching repositories', async () => { + ;(getInfos as any).mockResolvedValue({ user: mockUser, repos: [], branch: 'main' }) + ;(generateInfos as any).mockResolvedValue({ user: mockUser, repos: [] }) + + await fetchDoc(mockFetchOpts) + + expect(generateInfos).toHaveBeenCalledWith(mockUser, [], 'main', mockFetchOpts.reposFilter) + expect(getDoc).toHaveBeenCalledWith([], mockFetchOpts.reposFilter) + }) - await expect(fetchDoc(fetchOpts)).rejects.toThrow('getInfos failed') - expect(console.error).toHaveBeenCalledWith(expect.any(Error)) - expect(createDir).toHaveBeenCalledWith(expect.any(String), { clean: true }) + it('should throw an error if getInfos fails', async () => { + ;(getInfos as any).mockRejectedValue(new Error('Failed to fetch infos')) - const generateInfos = (await import('./fetch.js')).generateInfos as any - const getDoc = (await import('./fetch.js')).getDoc as any + await expect(fetchDoc(mockFetchOpts)).rejects.toThrow('Failed to fetch infos') + expect(getInfos).toHaveBeenCalledWith({ + username: mockFetchOpts.username, + token: mockFetchOpts.token, + branch: mockFetchOpts.branch, + }) expect(generateInfos).not.toHaveBeenCalled() expect(getDoc).not.toHaveBeenCalled() }) + + it.skip('should throw an error if generateInfos fails', async () => { + ;(getInfos as any).mockResolvedValue({ user: mockUser, repos: mockRepos, branch: 'main' }) + ;(generateInfos as any).mockRejectedValue(new Error('Failed to generate infos')) + + await expect(fetchDoc(mockFetchOpts)).rejects.toThrow('Failed to generate infos') + + expect(getInfos).toHaveBeenCalledWith({ + username: mockFetchOpts.username, + token: mockFetchOpts.token, + branch: mockFetchOpts.branch, + }) + expect(generateInfos).toHaveBeenCalledWith(mockUser, mockRepos, 'main', mockFetchOpts.reposFilter) + expect(getDoc).not.toHaveBeenCalled() + }) }) From d2f525ebce7fff8f5e75582cfaa19cf30b0dc490 Mon Sep 17 00:00:00 2001 From: this-is-tobi Date: Wed, 18 Dec 2024 00:28:22 +0100 Subject: [PATCH 2/3] ci: upgrade github action --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index be7b504..d098602 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -109,7 +109,7 @@ jobs: - name: Build and push docker image id: build - uses: docker/build-push-action@v5 + uses: docker/build-push-action@v6 with: context: ${{ matrix.images.build.context }} file: ${{ matrix.images.build.dockerfile }} From a8e79173a7c7b42f5fa206755eeded2bb737629a Mon Sep 17 00:00:00 2001 From: this-is-tobi Date: Wed, 18 Dec 2024 00:30:55 +0100 Subject: [PATCH 3/3] chore: remove unused vscode extension --- .vscode/extensions.json | 1 - 1 file changed, 1 deletion(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 25e2886..38e285a 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -9,7 +9,6 @@ "dbaeumer.vscode-eslint", // Vue.js "vue.volar", - "vue.vscode-typescript-vue-plugin", // CSS "csstools.postcss", "stylelint.vscode-stylelint",