STATICAI commited on
Commit
770a91a
·
verified ·
1 Parent(s): e2add7e

Restore test_preflight.py from Make-It-Animatable

Browse files
Files changed (1) hide show
  1. test_preflight.py +62 -0
test_preflight.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import struct
3
+ import tempfile
4
+ import unittest
5
+ from pathlib import Path
6
+ from preflight import inspect_glb
7
+
8
+ class PreflightTests(unittest.TestCase):
9
+ def write_fixture(self, root, faces, copies=1):
10
+ # Valid unindexed triangles. Repeated triangle vertices exercise the
11
+ # parser with real binary payload, without a 2M-face Python object graph.
12
+ count = faces * 3
13
+ doc = {"asset":{"version":"2.0"}, "buffers":[{"byteLength":count*12}],
14
+ "bufferViews":[{"buffer":0,"byteLength":count*12}],
15
+ "accessors":[{"bufferView":0,"componentType":5126,"count":count,
16
+ "type":"VEC3","min":[0,0,0],"max":[1,1,0]}],
17
+ "meshes":[{"primitives":[{"attributes":{"POSITION":0}}]}],
18
+ "nodes":[{"mesh":0} for _ in range(copies)],
19
+ "scenes":[{"nodes":list(range(copies))}],"scene":0}
20
+ payload = json.dumps(doc).encode()
21
+ payload += b" " * (-len(payload) % 4)
22
+ p = Path(root)/"fixture.glb"
23
+ with p.open("wb") as stream:
24
+ stream.write(struct.pack("<4sIIII", b"glTF",2,28+len(payload)+count*12,len(payload),0x4E4F534A))
25
+ stream.write(payload)
26
+ stream.write(struct.pack("<II",count*12,0x004E4942))
27
+ triangle = struct.pack("<9f",0,0,0,1,0,0,0,1,0)
28
+ block = triangle * 1000
29
+ for _ in range(faces//1000):
30
+ stream.write(block)
31
+ stream.write(triangle * (faces%1000))
32
+ return p
33
+
34
+ def test_normal(self):
35
+ with tempfile.TemporaryDirectory() as root:
36
+ result = inspect_glb(self.write_fixture(root,12))
37
+ self.assertTrue(result["inference_allowed"])
38
+ self.assertEqual(result["triangles"],12)
39
+
40
+ def test_two_million_faces_preserved_and_blocked(self):
41
+ with tempfile.TemporaryDirectory() as root:
42
+ path = self.write_fixture(root,2_000_000)
43
+ before = path.stat()
44
+ result = inspect_glb(path)
45
+ self.assertEqual(result["triangles"],2_000_000)
46
+ self.assertFalse(result["inference_allowed"])
47
+ self.assertIn("decimation required",result["warning"])
48
+ self.assertEqual(path.stat().st_size,before.st_size)
49
+ self.assertEqual(path.stat().st_mtime_ns,before.st_mtime_ns)
50
+
51
+ def test_instances_count_towards_limit(self):
52
+ with tempfile.TemporaryDirectory() as root:
53
+ result = inspect_glb(self.write_fixture(root,100_000,6))
54
+ self.assertFalse(result["inference_allowed"])
55
+ self.assertEqual(result["instantiated_triangles"],600_000)
56
+
57
+ def test_truncated_rejected(self):
58
+ with tempfile.TemporaryDirectory() as root:
59
+ path=Path(root)/"bad.glb"
60
+ path.write_bytes(b"glTF")
61
+ with self.assertRaises(ValueError):
62
+ inspect_glb(path)