Template
feat: fetch lyrics during music ingest
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.music.service;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LyricsServiceTest {
|
||||
private HttpServer server;
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
if (server != null) server.stop(0);
|
||||
System.clearProperty("mangtool.lyrics.base-url");
|
||||
System.clearProperty("mangtool.lyrics.auth");
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectsExactArtistInsteadOfFirstCoverCandidate() throws Exception {
|
||||
String json = "["
|
||||
+ "{\"title\":\"夜曲 (网易云音乐)\",\"artist\":\"Xai小爱\",\"lyrics\":\"[00:01]翻唱\",\"type\":\"lrc\",\"source\":\"netease\"},"
|
||||
+ "{\"title\":\"夜曲 (酷狗音乐)\",\"artist\":\"周杰伦\",\"lyrics\":\"[00:01]一群嗜血的蚂蚁\",\"type\":\"lrc\",\"source\":\"kugou\",\"isComplete\":true}]";
|
||||
start(json, 200);
|
||||
LyricsService.Candidate result = new LyricsService().lookup("夜曲", "周杰伦");
|
||||
assertNotNull(result);
|
||||
assertEquals("kugou", result.source);
|
||||
assertTrue(result.lyrics.contains("一群嗜血的蚂蚁"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresNonSuccessfulResponse() throws Exception {
|
||||
start("not found", 500);
|
||||
assertNull(new LyricsService().lookup("夜曲", "周杰伦"));
|
||||
}
|
||||
|
||||
private void start(String body, int status) throws Exception {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/lrc", exchange -> {
|
||||
assertEquals("secret", exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
exchange.getResponseBody().write(bytes);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
System.setProperty("mangtool.lyrics.base-url", "http://127.0.0.1:" + server.getAddress().getPort());
|
||||
System.setProperty("mangtool.lyrics.auth", "secret");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user