README 外链图片转存 OSS

This commit is contained in:
lxp
2026-11-03 17:27:49 +08:00
parent 175c49e0e7
commit 46496e0ab9
@@ -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<String, String> branches = new HashMap<>(); branches.put("main", originalMain);
Map<String, byte[]> 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<String, String> 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<GiteaClient.Change> 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();
}
}