Files
LihonT/tests/java/lihont/two/frame/FrameTest.java
cheb0chik 86b014e15a LihonT 2 Serena beta 2 (hotfix)
Fixes the freezes while scrolling the feed and opening a profile: reading a user name made the mod ask TikTok which account is
signed in, which read the name again through the same hooks, thousands of levels deep, every few seconds. The time of the next
question is now set before it is asked, and a nested call only hands the name back. Adds a light timer of the places where TikTok
calls into the mod (lines "perf" in the diary), the avatar frame closed packs on the server (grants by account id), the
winter pack of frames, the mirror page generator and the beat video script.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 10:10:53 +03:00

215 lines
12 KiB
Java

package lihont.two.frame;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
/** The frame catalog and the outline geometry on a plain JVM. Usage: FrameTest scenario; prints "PASS scenario". */
public final class FrameTest {
private static final String SHA = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
private FrameTest() {}
public static void main(String[] args) throws Exception {
String scenario = args[0];
switch (scenario) {
case "real": real(); break;
case "refusals": refusals(); break;
case "dropped": dropped(); break;
case "ids": ids(); break;
case "outline": outline(); break;
default: throw new IllegalArgumentException(scenario);
}
System.out.println("PASS " + scenario);
}
private static void check(boolean ok, String what) {
if (!ok) throw new AssertionError(what);
}
private static String catalog(String sprite, String frame) {
return "{\"version\":1,\"packs\":[{\"id\":\"p\",\"name\":\"Pack\",\"version\":1,\"sprites\":[" + sprite
+ "],\"frames\":[" + frame + "]}]}";
}
private static String sprite(String file, String sha, int bytes) {
return "{\"file\":\"" + file + "\",\"sha256\":\"" + sha + "\",\"bytes\":" + bytes + ",\"w\":100,\"h\":80}";
}
private static String frame(String id, String ring, String part) {
return "{\"id\":\"" + id + "\",\"name\":\"F\",\"ring\":" + ring + ",\"parts\":[" + part + "]}";
}
private static final String RING = "{\"width\":0.05,\"line\":0.01,\"fill\":[\"#ffffff\",\"#000000\"],\"ink\":\"#112233\"}";
private static final String PART = "{\"file\":\"p/a.png\",\"ax\":0.5,\"ay\":0.9,\"at\":-30,\"size\":0.3}";
private static void real() throws Exception {
String text = new String(Files.readAllBytes(Paths.get("frames", "index.json")), StandardCharsets.UTF_8);
FrameIndex index = FrameIndex.parse(text);
check(index.packs.size() == 2 && index.packs.get(0).id.equals("basic") && index.packs.get(1).id.equals("winter"),
"the packs basic and winter");
check(index.allFrames().size() == 9, "nine frames");
for (String id : new String[] {"winter.deer", "winter.snow", "winter.garland", "winter.holly"}) {
check(index.frame(id) != null && !index.frame(id).parts.isEmpty(), id);
}
check(index.frame("winter.garland").parts.size() == 15, "fifteen bulbs on the garland");
for (String id : new String[] {"basic.cat", "basic.helmet", "basic.flower", "basic.crown", "basic.flame"}) {
FrameIndex.Frame frame = index.frame(id);
check(frame != null && !frame.parts.isEmpty(), id);
check(FrameIndex.validFrameId(id), "valid id " + id);
}
check(index.allSprites().size() == 21, "21 sprites, each once: " + index.allSprites().size());
FrameIndex.Frame flower = index.frame("basic.flower");
check(flower.parts.size() == 24, "the flower hangs 24 petals on 2 sprites");
check(index.frame("basic.crown").parts.get(0).front, "the crown is drawn in front");
check(index.frame("basic.cat").parts.get(7).flip && !index.frame("basic.cat").parts.get(6).flip, "the right ear is mirrored");
check(index.frame("nope.cat") == null && index.frame(null) == null, "unknown ids give null");
}
private static void refusals() {
String[] bad = {
"", "[]", "{}", "{\"version\":2,\"packs\":[]}", "{\"version\":1}", "{\"version\":1,\"packs\":5}", "not json",
"{\"version\":1,\"packs\":[]} trailing",
};
for (String text : bad) {
try {
FrameIndex.parse(text);
throw new AssertionError("should be refused: " + text);
} catch (IllegalArgumentException expected) {
// refused
}
}
StringBuilder big = new StringBuilder("{\"version\":1,\"packs\":[],\"pad\":\"");
for (int i = 0; i < 270000; i++) big.append('x');
big.append("\"}");
try {
FrameIndex.parse(big.toString());
throw new AssertionError("a catalog over 256 KB is refused");
} catch (IllegalArgumentException expected) {
// refused
}
check(FrameIndex.parse("{\"version\":1,\"packs\":[]}").allFrames().isEmpty(), "an empty catalog is fine");
}
private static void dropped() {
String good = sprite("p/a.png", SHA, 1000);
FrameIndex ok = FrameIndex.parse(catalog(good, frame("f", RING, PART)));
check(ok.frame("p.f") != null, "the good frame is kept");
// each of these loses the frame (or the sprite) and nothing else: the catalog still parses
String[] sprites = {
sprite("p/../a.png", SHA, 1000), sprite("../p/a.png", SHA, 1000), sprite("/etc/a.png", SHA, 1000),
sprite("other/a.png", SHA, 1000), sprite("p/a.exe", SHA, 1000), sprite("p/A.png", SHA, 1000),
sprite("p/a.png", "xyz", 1000), sprite("p/a.png", SHA.toUpperCase(), 1000), sprite("p/a.png", SHA, 0),
sprite("p/a.png", SHA, FrameIndex.MAX_SPRITE_BYTES + 1), sprite("p/a.png", SHA, -5),
sprite("p/sub/a.png", SHA, 1000), sprite("p/a.png\\u0000", SHA, 1000),
};
for (String bad : sprites) {
FrameIndex index = FrameIndex.parse("{\"version\":1,\"packs\":[{\"id\":\"p\",\"name\":\"P\",\"version\":1,\"sprites\":[" + bad
+ "],\"frames\":[" + frame("f", RING, PART) + "]}]}");
check(index.allFrames().isEmpty(), "a bad sprite leaves no frame: " + bad);
}
String[] rings = {
"{\"width\":0.5,\"line\":0.01,\"fill\":[\"#ffffff\",\"#000000\"],\"ink\":\"#112233\"}",
"{\"width\":0.05,\"line\":0.5,\"fill\":[\"#ffffff\",\"#000000\"],\"ink\":\"#112233\"}",
"{\"width\":0.05,\"line\":0.01,\"fill\":[\"#FFFFFF\",\"#000000\"],\"ink\":\"#112233\"}",
"{\"width\":0.05,\"line\":0.01,\"fill\":[\"#ffffff\"],\"ink\":\"#112233\"}",
"{\"width\":0.05,\"line\":0.01,\"fill\":[\"#ffffff\",\"#000000\"],\"ink\":\"red\"}",
"{\"line\":0.01,\"fill\":[\"#ffffff\",\"#000000\"],\"ink\":\"#112233\"}",
};
for (String ring : rings) {
check(FrameIndex.parse(catalog(good, frame("f", ring, PART))).allFrames().isEmpty(), "bad ring: " + ring);
}
String[] parts = {
"{\"file\":\"p/missing.png\",\"ax\":0.5,\"ay\":0.9,\"at\":0,\"size\":0.3}",
"{\"file\":\"p/a.png\",\"ax\":0.5,\"ay\":0.9,\"at\":0,\"size\":5}",
"{\"file\":\"p/a.png\",\"ax\":0.5,\"ay\":0.9,\"at\":0,\"size\":0}",
"{\"file\":\"p/a.png\",\"ax\":0.5,\"ay\":0.9,\"at\":999,\"size\":0.3}",
"{\"file\":\"p/a.png\",\"ax\":9,\"ay\":0.9,\"at\":0,\"size\":0.3}",
"{\"file\":\"p/a.png\",\"ax\":0.5,\"ay\":0.9,\"at\":0,\"size\":0.3,\"turn\":500}",
"{\"file\":\"p/a.png\",\"ax\":0.5,\"ay\":0.9,\"at\":0,\"size\":0.3,\"radial\":3}",
"{\"file\":\"p/a.png\",\"ay\":0.9,\"at\":0,\"size\":0.3}",
"5",
};
for (String part : parts) {
check(FrameIndex.parse(catalog(good, frame("f", RING, part))).allFrames().isEmpty(), "bad part: " + part);
}
// too many parts: the frame is refused whole, not half drawn
StringBuilder many = new StringBuilder();
for (int i = 0; i <= FrameIndex.MAX_PARTS; i++) many.append(i == 0 ? "" : ",").append(PART);
check(FrameIndex.parse(catalog(good, frame("f", RING, many.toString()))).allFrames().isEmpty(), "81 parts");
// a good frame beside a bad one survives
FrameIndex mixed = FrameIndex.parse(catalog(good, frame("bad", RING, "5") + "," + frame("good", RING, PART)));
check(mixed.allFrames().size() == 1 && mixed.frame("p.good") != null, "the good one survives");
}
private static void ids() {
String[] good = {"basic.cat", "a.b", "pack-1.frame_2", "x9.y9"};
String[] bad = {"", "cat", "basic.", ".cat", "Basic.cat", "basic.cat.x", "a/b.c", "a b.c", "basic.cat\n",
"aaaaaaaaaaaaaaaaaaaaaaaaa.b", "a.bbbbbbbbbbbbbbbbbbbbbbbbb", "-a.b", "a._b"};
for (String id : good) check(FrameIndex.validFrameId(id), "valid " + id);
for (String id : bad) check(!FrameIndex.validFrameId(id), "invalid " + id);
check(!FrameIndex.validFrameId(null), "null");
String twice = "{\"version\":1,\"packs\":[{\"id\":\"p\",\"name\":\"P\",\"version\":1,\"sprites\":[" + sprite("p/a.png", SHA, 10)
+ "],\"frames\":[" + frame("f", RING, PART) + "," + frame("f", RING, PART) + "]}]}";
check(FrameIndex.parse(twice).allFrames().size() == 1, "a repeated id is kept once");
String names = FrameIndex.parse(catalog(sprite("p/a.png", SHA, 10), "{\"id\":\"f\",\"name\":\"A\\nB\\u0001C\",\"ring\":" + RING
+ ",\"parts\":[" + PART + "]}")).frame("p.f").name;
check(names.equals("ABC"), "control characters are dropped from names: " + names);
String mirrors = "{\"version\":1,\"packs\":[],\"mirrors\":[\"https://a.example.org/frames/\",\"http://a.example.org/frames/\","
+ "\"https://x/\",\"https://a.example.org/../x/\",5]}";
check(FrameIndex.parse(mirrors).mirrors.size() == 1, "only the https mirror with a sane address stays");
}
private static void outline() {
int n = 720;
float[] xs = new float[n];
float[] ys = new float[n];
float r = 100f;
for (int i = 0; i < n; i++) {
double a = 2 * Math.PI * i / n;
xs[i] = 150f + r * (float) Math.cos(a);
ys[i] = 150f + r * (float) Math.sin(a);
}
Outline circle = Outline.of(xs, ys, n, 150f, 150f);
float[] top = circle.point(0f);
check(Math.abs(top[0] - 150f) < 1f && Math.abs(top[1] - 50f) < 1f, "0 degrees is the top");
float[] right = circle.point(90f);
check(Math.abs(right[0] - 250f) < 1f && Math.abs(right[1] - 150f) < 1f, "90 degrees is the right");
float[] bottom = circle.point(180f);
check(Math.abs(bottom[1] - 250f) < 1f, "180 degrees is the bottom");
float[] left = circle.point(-90f);
check(Math.abs(left[0] - 50f) < 1f, "-90 degrees is the left");
check(Math.abs(circle.point(450f)[0] - 250f) < 1f, "angles wrap");
for (int degrees : new int[] {0, 45, 90, 135, 180, -90, -45}) {
float normal = circle.normalDegrees(degrees);
float diff = Math.abs(((normal - degrees + 540f) % 360f) - 180f);
check(diff < 2f, "on a circle the way out at " + degrees + " is " + degrees + " but was " + normal);
}
// a square: the top edge is flat, so its way out is straight up at the middle and the corners reach further
float s = 100f;
int per = 400;
float[] sx = new float[4 * per];
float[] sy = new float[4 * per];
for (int i = 0; i < per; i++) {
float t = s * i / per;
sx[i] = 100f + t; sy[i] = 100f;
sx[per + i] = 200f; sy[per + i] = 100f + t;
sx[2 * per + i] = 200f - t; sy[2 * per + i] = 200f;
sx[3 * per + i] = 100f; sy[3 * per + i] = 200f - t;
}
Outline square = Outline.of(sx, sy, 4 * per, 150f, 150f);
check(Math.abs(square.point(0f)[1] - 100f) < 1f, "the middle of the top edge");
check(Math.abs(square.normalDegrees(0f)) < 1f, "straight up from a flat top");
float[] corner = square.point(45f);
check(Math.abs(corner[0] - 200f) < 2f && Math.abs(corner[1] - 100f) < 2f, "the corner reaches further than the edge");
// gaps are filled from the neighbours: only a few points, still a closed table
Outline sparse = Outline.of(new float[] {250f, 150f, 50f, 150f}, new float[] {150f, 250f, 150f, 50f}, 4, 150f, 150f);
check(Math.abs(sparse.point(0f)[1] - 50f) < 1f && Math.abs(sparse.point(90f)[0] - 250f) < 1f, "known directions are exact");
float[] between = sparse.point(45f);
check(between[0] > 150f && between[1] < 150f, "unknown directions stay on the right side: " + between[0] + "," + between[1]);
// no points at all gives a point in the middle, not a crash
Outline none = Outline.of(new float[0], new float[0], 0, 10f, 10f);
check(Math.abs(none.point(30f)[0] - 10f) < 0.01f, "an empty outline is the middle");
}
}