From 46496e0ab9051b4c3324103f709b895aab46844a Mon Sep 17 00:00:00 2001 From: lxp <2770281812@qq.com> Date: Tue, 3 Nov 2026 17:27:49 +0800 Subject: [PATCH] =?UTF-8?q?README=20=E5=A4=96=E9=93=BE=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=BD=AC=E5=AD=98=20OSS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReadmeMigrationApplicationTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 vibemake-import/src/test/java/com/linkerbot/vibemake/importer/readme/ReadmeMigrationApplicationTest.java diff --git a/vibemake-import/src/test/java/com/linkerbot/vibemake/importer/readme/ReadmeMigrationApplicationTest.java b/vibemake-import/src/test/java/com/linkerbot/vibemake/importer/readme/ReadmeMigrationApplicationTest.java new file mode 100644 index 0000000..f0930ee --- /dev/null +++ b/vibemake-import/src/test/java/com/linkerbot/vibemake/importer/readme/ReadmeMigrationApplicationTest.java @@ -0,0 +1,90 @@ +package com.linkerbot.vibemake.importer.readme; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.linkerbot.vibemake.bean.Contracts.CommitView; +import com.linkerbot.vibemake.bean.Contracts.TreeEntry; +import com.linkerbot.vibemake.bean.entity.Project; +import com.linkerbot.vibemake.service.config.*; +import com.linkerbot.vibemake.service.gitea.*; +import com.linkerbot.vibemake.service.mapper.ProjectMapper; +import com.linkerbot.vibemake.service.readme.*; +import com.linkerbot.vibemake.service.support.ProjectLocks; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.time.LocalDateTime; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.jdbc.core.JdbcTemplate; + +class ReadmeMigrationApplicationTest { + @TempDir Path directory; + @ParameterizedTest + @ValueSource(strings = {"PUBLISHED", "UNPUBLISHED", "BLOCKED"}) + void preservesPublishedSnapshotWithoutPublishingNewCodeAndResumesIdempotently(String publicationStatus) throws Exception { + ObjectMapper json = new ObjectMapper(); + Project project = new Project(); + project.setProjectCode("abcd12345678"); project.setGiteaOwner("org"); project.setGiteaRepoName("repo"); project.setGiteaRepoId(1L); + project.setRepoStatus("READY"); project.setRepoWriteStatus("IDLE"); project.setPublishStatus(publicationStatus); project.setDeleted(false); + String originalMain = "a".repeat(40), originalPublished = "b".repeat(40); + project.setPublishedCommitSha(originalPublished); + LocalDateTime publishedAt = LocalDateTime.of(2026, 10, 1, 12, 0); + project.setPublishedAt(publishedAt); + GiteaClient gitea = mock(GiteaClient.class); + GiteaClient.Repository repository = new GiteaClient.Repository(1, "org", "repo"); + Map branches = new HashMap<>(); branches.put("main", originalMain); + Map contents = new HashMap<>(); + contents.put(originalMain, "# main\n![a](https://example.com/a.png)\n".getBytes(StandardCharsets.UTF_8)); + contents.put(originalPublished, "# published\n![a](https://example.com/a.png)\n".getBytes(StandardCharsets.UTF_8)); + Map parents = new HashMap<>(); + AtomicInteger writes = new AtomicInteger(); + when(gitea.inspect("repo")).thenReturn(new GiteaClient.RemoteRepository(1,"org","repo",true,false,false,"https://github.com/source/repo","main")); + when(gitea.head(repository)).thenAnswer(call -> branches.get("main")); + when(gitea.branch(eq(repository), anyString())).thenAnswer(call -> { + String sha = branches.get(call.getArgument(1)); + if (sha == null) throw new GiteaFailure(404,"NOT_FOUND","missing",false); + return sha; + }); + doAnswer(call -> { branches.put(call.getArgument(1),call.getArgument(2)); return null; }).when(gitea).createBranch(eq(repository),anyString(),anyString()); + when(gitea.file(eq(repository), anyString(), eq("README.md"))).thenAnswer(call -> new GiteaClient.StoredFile("README.md","blob",contents.get(call.getArgument(1)))); + when(gitea.tree(eq(repository), anyString(), eq(""))).thenAnswer(call -> { + String sha = call.getArgument(1); + String base = parents.getOrDefault(sha,sha); + return List.of(new TreeEntry("README.md","README.md","file",contents.get(sha).length,"blob"), + new TreeEntry("code","code","dir",0,base.equals(originalPublished)?"published-code":"unpublished-code")); + }); + when(gitea.commit(eq(repository),anyString(),anyList(),anyString(),anyString(),anyString())).thenAnswer(call -> { + String branch = call.getArgument(1), base = branches.get(branch), next = String.format("%040d", writes.incrementAndGet()); + List changes = call.getArgument(2); + contents.put(next,changes.getFirst().bytes()); parents.put(next,base); branches.put(branch,next); + return new CommitView(next,"repair"); + }); + OssProperties oss = new OssProperties(); oss.setPublicBaseUrl("https://assets.example.com"); oss.setRootDirectory("vibemake"); + ReadmeRepository readmes = new ReadmeRepository(gitea,new ReadmeImages(new ReadmeImageProperties(),oss,url -> "https://assets.example.com/readme/a.png")); + ProjectMapper projects = mock(ProjectMapper.class); + when(projects.selectList(any())).thenReturn(List.of(project)); when(projects.selectOne(any())).thenReturn(project); + JdbcTemplate jdbc = mock(JdbcTemplate.class); + when(jdbc.queryForObject(anyString(),eq(Long.class),eq(project.getProjectCode()))).thenReturn(0L); + doAnswer(call -> { Object[] values = (Object[]) call.getRawArguments()[1]; project.setPublishedCommitSha((String)values[0]); return 1; }) + .when(jdbc).update(anyString(),any(Object[].class)); + ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class); + when(context.getBean(ObjectMapper.class)).thenReturn(json); when(context.getBean(JdbcTemplate.class)).thenReturn(jdbc); + when(context.getBean(ProjectMapper.class)).thenReturn(projects); when(context.getBean(GiteaClient.class)).thenReturn(gitea); + when(context.getBean(ProjectLocks.class)).thenReturn(new ProjectLocks()); when(context.getBean(ReadmeRepository.class)).thenReturn(readmes); + ReadmeMigrationApplication app = new ReadmeMigrationApplication(new ReadmeMigrationApplication.Options(true,10,Set.of(),directory),context); + VibemakeProperties config = new VibemakeProperties(); + assertThat(app.run(config,oss)).isZero(); + assertThat(writes).hasValue(2); + assertThat(project.getPublishedCommitSha()).isNotEqualTo(branches.get("main")); + assertThat(new String(contents.get(project.getPublishedCommitSha()),StandardCharsets.UTF_8)).startsWith("# published"); + assertThat(project.getPublishStatus()).isEqualTo(publicationStatus); assertThat(project.getPublishedAt()).isEqualTo(publishedAt); + assertThat(app.run(config,oss)).isZero(); assertThat(writes).hasValue(2); + assertThat(directory.resolve(project.getProjectCode()+"-published-original.md")).exists(); + } +}