The importer from t5de-hub's docs/imports.md: says hello to the hub with its key pair, follows the console's config (paused, rate, jobs at a time, IMVU accounts, whose sealed passwords it keeps), claims jobs, finds each product's newest revision on IMVU's CDN, converts it (originals for placeholders, no _contents.json, ProductRating from IMVU's rating), checks it by t5de-api's pack rules and uploads it to the catalog with an import token. Co-Authored-By: Claude Opus 5.5 <[email protected]>
126 lines
4.3 KiB
Go
126 lines
4.3 KiB
Go
package convert
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.t5de.dev/t5de/t5de-api/cfl"
|
|
)
|
|
|
|
func TestSetRating(t *testing.T) {
|
|
for _, c := range []struct{ in, rating, want string }{
|
|
{"<Template>\n<X/></Template>", "general", "<Template>\n<ProductRating>GA</ProductRating>\n<X/></Template>"},
|
|
{"<Template>\n<X/></Template>", "adult", "<Template>\n<ProductRating>AP</ProductRating>\n<X/></Template>"},
|
|
{`<?xml version="1.0"?><Template a="b"></Template>`, "general", `<?xml version="1.0"?><Template a="b">` + "\n<ProductRating>GA</ProductRating></Template>"},
|
|
{"<Template><ProductRating>AP</ProductRating></Template>", "general", "<Template><ProductRating>AP</ProductRating></Template>"},
|
|
{"<Template><ProductRating> GA </ProductRating></Template>", "adult", "<Template><ProductRating>AP</ProductRating></Template>"},
|
|
{"<Template><ProductRating>GA</ProductRating></Template>", "general", "<Template><ProductRating>GA</ProductRating></Template>"},
|
|
} {
|
|
got, err := SetRating([]byte(c.in), c.rating)
|
|
if err != nil || string(got) != c.want {
|
|
t.Errorf("%q, %s: got %q, %v; want %q", c.in, c.rating, got, err, c.want)
|
|
}
|
|
}
|
|
if _, err := SetRating([]byte("<NotATemplate/>"), "general"); err == nil {
|
|
t.Error("no <Template>: no error")
|
|
}
|
|
}
|
|
|
|
func TestFiles(t *testing.T) {
|
|
contents := `[{"name":"tex.png","url":"abc.png","tags":["original","thumbnail"]},` +
|
|
`{"name":"mat.xrf","url":"def.xrf"},{"name":"cdn-only.xrf","url":"ghi.xrf"},{"name":"small.png","tags":["original"]},{"name":"index.xml"}]`
|
|
entries := []cfl.Entry{
|
|
{Name: "index.xml", Data: []byte("<Template>\n</Template>")},
|
|
{Name: "tex.png", Data: []byte("placeholder")},
|
|
{Name: "mat.xrf", Data: []byte("material")},
|
|
{Name: "small.png", Data: []byte("small original")},
|
|
{Name: "_contents.json", Data: []byte(contents)},
|
|
}
|
|
cdn := map[string]string{"abc.png": "full-size original", "ghi.xrf": "only on the CDN"}
|
|
var asked []string
|
|
fetch := func(_ context.Context, url string) ([]byte, error) {
|
|
asked = append(asked, url)
|
|
if s, ok := cdn[url]; ok {
|
|
return []byte(s), nil
|
|
}
|
|
return nil, errors.New("missing")
|
|
}
|
|
files, err := Files(context.Background(), entries, "adult", fetch)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]string{}
|
|
var names []string
|
|
for _, f := range files {
|
|
got[f.Name] = string(f.Data)
|
|
names = append(names, f.Name)
|
|
}
|
|
want := map[string]string{
|
|
"index.xml": "<Template>\n<ProductRating>AP</ProductRating>\n</Template>",
|
|
"tex.png": "full-size original",
|
|
"mat.xrf": "material", // in the CFL already, not an original
|
|
"small.png": "small original",
|
|
"cdn-only.xrf": "only on the CDN",
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Errorf("files %v", names)
|
|
}
|
|
for n, w := range want {
|
|
if got[n] != w {
|
|
t.Errorf("%s: got %q, want %q", n, got[n], w)
|
|
}
|
|
}
|
|
if strings.Join(asked, ",") != "abc.png,ghi.xrf" {
|
|
t.Errorf("fetched %v", asked)
|
|
}
|
|
|
|
if _, err := Files(context.Background(), entries[1:], "general", fetch); err == nil {
|
|
t.Error("no index.xml: no error")
|
|
}
|
|
cdn = nil
|
|
if _, err := Files(context.Background(), entries, "general", fetch); err == nil {
|
|
t.Error("an original missing: no error")
|
|
}
|
|
if _, err := Files(context.Background(), entries, "", fetch); err == nil {
|
|
t.Error("no rating: no error")
|
|
}
|
|
}
|
|
|
|
func TestChknAndCheck(t *testing.T) {
|
|
index, _ := SetRating([]byte("<Template>\n</Template>"), "general")
|
|
files := []File{{Name: "index.xml", Data: index}, {Name: "Skin%20%5B03%5D.xrf", Data: []byte{1, 2, 3}}}
|
|
chkn, err := Chkn(files)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
z, err := zip.NewReader(bytes.NewReader(chkn), int64(len(chkn)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, f := range z.File {
|
|
r, _ := f.Open()
|
|
data, _ := io.ReadAll(r)
|
|
if f.Name != files[i].Name || !bytes.Equal(data, files[i].Data) {
|
|
t.Errorf("zip entry %d: %q", i, f.Name)
|
|
}
|
|
}
|
|
if err := Check(chkn, 42, "general", t.TempDir()); err != nil {
|
|
t.Errorf("check: %v", err)
|
|
}
|
|
if err := Check(chkn, 42, "adult", t.TempDir()); err == nil {
|
|
t.Error("rating disagreeing with index.xml: no error")
|
|
}
|
|
bad, _ := Chkn(append(files, File{Name: "_contents.json", Data: []byte("[]")}))
|
|
if err := Check(bad, 42, "general", t.TempDir()); err == nil {
|
|
t.Error("_contents.json: no error")
|
|
}
|
|
if Rating(index) != "general" {
|
|
t.Error("Rating of GA")
|
|
}
|
|
}
|