Repository navigation
Expand file tree
/
Copy pathAndroidCompiler.test.ts
More file actions
390 lines (330 loc) · 13.8 KB
/
Copy pathAndroidCompiler.test.ts
File metadata and controls
390 lines (330 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* AndroidCompiler Tests
*
* Tests for the HoloScript → Android Kotlin ARCore compiler.
* Verifies correct Kotlin code generation for ARCore experiences.
*/
import { createHash } from 'crypto';
import { describe, it, expect, beforeEach } from 'vitest';
import { AndroidCompiler, type AndroidCompilerOptions } from './AndroidCompiler';
import type { HoloComposition, HoloObjectDecl } from '../parser/HoloCompositionTypes';
function hashRecordStrings(obj: Record<string, unknown>): string {
const parts: string[] = [];
for (const k of Object.keys(obj).sort()) {
const v = obj[k];
if (typeof v === 'string') {
parts.push(`${k}:${createHash('sha256').update(v, 'utf8').digest('hex')}`);
} else if (v && typeof v === 'object' && !Array.isArray(v)) {
parts.push(`${k}:${hashRecordStrings(v as Record<string, unknown>)}`);
}
}
return createHash('sha256').update(parts.join('|'), 'utf8').digest('hex');
}
describe('AndroidCompiler', () => {
let compiler: AndroidCompiler;
beforeEach(() => {
compiler = new AndroidCompiler();
});
// Helper to create a minimal composition
function createComposition(overrides: Partial<HoloComposition> = {}): HoloComposition {
return {
type: 'Composition',
name: 'TestARScene',
objects: [],
templates: [],
spatialGroups: [],
lights: [],
imports: [],
timelines: [],
audio: [],
zones: [],
transitions: [],
conditionals: [],
iterators: [],
npcs: [],
quests: [],
abilities: [],
dialogues: [],
stateMachines: [],
achievements: [],
talentTrees: [],
shapes: [],
...overrides,
};
}
// Helper to create an object declaration
function createObject(name: string, overrides: Partial<HoloObjectDecl> = {}): HoloObjectDecl {
return {
name,
properties: [],
traits: [],
...overrides,
} as HoloObjectDecl;
}
describe('Basic Compilation', () => {
it('should create a compiler instance', () => {
expect(compiler).toBeDefined();
expect(compiler).toBeInstanceOf(AndroidCompiler);
});
it('should compile an empty composition', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result).toBeDefined();
expect(result.activityFile).toBeDefined();
expect(result.stateFile).toBeDefined();
expect(result.nodeFactoryFile).toBeDefined();
expect(result.manifestFile).toBeDefined();
expect(result.buildGradle).toBeDefined();
});
it('should generate valid Kotlin package declaration', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.activityFile).toContain('package com.holoscript.generated');
});
it('should use custom package name', () => {
const customCompiler = new AndroidCompiler({ packageName: 'com.myapp.ar' });
const composition = createComposition();
const result = customCompiler.compile(composition);
expect(result.activityFile).toContain('package com.myapp.ar');
});
it('should use custom class name', () => {
const customCompiler = new AndroidCompiler({ className: 'CustomARActivity' });
const composition = createComposition();
const result = customCompiler.compile(composition);
expect(result.activityFile).toContain('CustomARActivity');
});
});
describe('SDK Versions', () => {
it('should use default SDK versions', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.buildGradle).toContain('minSdk');
expect(result.buildGradle).toContain('targetSdk');
});
it('should use custom min SDK version', () => {
const customCompiler = new AndroidCompiler({ minSdk: 24 });
const composition = createComposition();
const result = customCompiler.compile(composition);
expect(result.buildGradle).toContain('24');
});
it('should use custom target SDK version (above the SceneView 36 floor)', () => {
const customCompiler = new AndroidCompiler({ targetSdk: 37 });
const composition = createComposition();
const result = customCompiler.compile(composition);
expect(result.buildGradle).toContain('37');
});
it('floors compile/target SDK at 36 (SceneView 4.18.0 requires API 36)', () => {
const customCompiler = new AndroidCompiler({ targetSdk: 34 });
const composition = createComposition();
const result = customCompiler.compile(composition);
expect(result.buildGradle).toContain('compileSdk = 36');
expect(result.buildGradle).toContain('targetSdk = 36');
});
});
describe('Object Compilation', () => {
it('should compile objects with geometry', () => {
const composition = createComposition({
objects: [
createObject('TestCube', {
properties: [
{ key: 'geometry', value: 'cube' },
{ key: 'position', value: [0, 0, -1] },
],
}),
],
});
const result = compiler.compile(composition);
// SceneView emits one declarative node per object inside the Compose ARScene { }.
expect(result.activityFile).toContain('TestCube');
expect(result.activityFile).toContain('CubeNode');
});
it('should compile objects with colors', () => {
const composition = createComposition({
objects: [
createObject('ColoredSphere', {
properties: [
{ key: 'geometry', value: 'sphere' },
{ key: 'color', value: '#00ff00' },
],
}),
],
});
const result = compiler.compile(composition);
expect(result.activityFile).toContain('ColoredSphere');
expect(result.activityFile).toContain('SphereNode');
expect(result.activityFile).toContain('Color(0xFF00FF00)');
});
it('should compile cylinders with SceneView radius + height (never length)', () => {
const composition = createComposition({
objects: [
createObject('Post', {
properties: [
{ key: 'geometry', value: 'cylinder' },
{ key: 'scale', value: 0.05 },
],
}),
],
});
const result = compiler.compile(composition);
// SceneView 4.18.0's CylinderNode composable is (radius, height, …): a `length` argument
// fails compileDebugKotlin with "No parameter with name 'length' found".
expect(result.activityFile).toContain('import io.github.sceneview.node.CylinderNode');
expect(result.activityFile).toContain('CylinderNode(');
// Scale is size: a 0.05 cylinder is 0.05 across (radius 0.025) and 0.05 tall.
expect(result.activityFile).toContain('radius = 0.025f,');
expect(result.activityFile).toContain('height = 0.05f,');
expect(result.activityFile).not.toMatch(/^\s*length\s*=/m);
});
it('sizes every shape the same way when no scale is given: 0.1 across', () => {
const composition = createComposition({
objects: [
createObject('Box', { properties: [{ key: 'geometry', value: 'cube' }] }),
createObject('Ball', { properties: [{ key: 'geometry', value: 'sphere' }] }),
createObject('Post', { properties: [{ key: 'geometry', value: 'cylinder' }] }),
],
});
const kt = compiler.compile(composition).activityFile;
expect(kt).toContain('size = Size(0.1f),');
// Sphere and cylinder both radius 0.05 (0.1 across); the cylinder 0.1 tall.
expect(kt.match(/^\s*radius = 0\.05f,/gm)?.length).toBe(2);
expect(kt).toContain('height = 0.1f,');
});
it('should fall back to a cube for unknown geometry, even an inherited name', () => {
const composition = createComposition({
objects: [createObject('Odd', { properties: [{ key: 'geometry', value: 'constructor' }] })],
});
const result = compiler.compile(composition);
expect(result.activityFile).toContain('CubeNode(');
expect(result.activityFile).not.toContain('function Object');
});
it('should compile interactive objects', () => {
const composition = createComposition({
objects: [
createObject('TappableObject', {
traits: ['clickable'],
properties: [{ key: 'geometry', value: 'cube' }],
}),
],
});
const result = compiler.compile(composition);
expect(result.activityFile).toBeDefined();
});
});
describe('Jetpack Compose Integration', () => {
it('should use Jetpack Compose by default', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.buildGradle).toContain('compose');
});
it('should support disabling Jetpack Compose', () => {
const customCompiler = new AndroidCompiler({ useJetpackCompose: false });
const composition = createComposition();
const result = customCompiler.compile(composition);
expect(result.activityFile).toBeDefined();
});
});
describe('SceneView Support', () => {
it('uses SceneView (Apache 2.0), not the EOL Sceneform fork', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.buildGradle).toContain('io.github.sceneview:arsceneview');
expect(result.buildGradle).not.toContain('sceneform');
});
it('emits a Compose ARScene activity (Filament renderer via SceneView)', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.activityFile).toContain('ARScene');
expect(result.activityFile).toContain('ComponentActivity');
});
});
describe('Manifest Generation', () => {
it('should generate valid AndroidManifest.xml', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.manifestFile).toContain('<?xml version');
expect(result.manifestFile).toContain('manifest');
});
it('should include camera permission', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.manifestFile).toContain('android.permission.CAMERA');
});
it('should include ARCore metadata', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.manifestFile).toContain('com.google.ar.core');
});
});
describe('Build.gradle Generation', () => {
it('should generate valid build.gradle.kts', () => {
const composition = createComposition();
const result = compiler.compile(composition);
expect(result.buildGradle).toContain('plugins');
expect(result.buildGradle).toContain('android');
expect(result.buildGradle).toContain('dependencies');
});
it('emits caller-supplied Android release identity', () => {
const releaseCompiler = new AndroidCompiler({ versionCode: 1000, versionName: '0.1.0' });
const result = releaseCompiler.compile(createComposition());
expect(result.buildGradle).toContain('versionCode = 1000');
expect(result.buildGradle).toContain('versionName = "0.1.0"');
});
it('includes the SceneView dependency (pulls ARCore + Filament transitively)', () => {
const composition = createComposition();
const result = compiler.compile(composition);
// ARCore is no longer a direct dep — SceneView's arsceneview AAR brings it in.
expect(result.buildGradle).toContain('io.github.sceneview:arsceneview');
});
});
describe('Lights Compilation', () => {
it('should compile directional lights', () => {
const composition = createComposition({
lights: [
{
name: 'SunLight',
type: 'directional',
color: '#ffffff',
intensity: 1.0,
},
],
});
const result = compiler.compile(composition);
expect(result.nodeFactoryFile).toBeDefined();
});
});
describe('State Management', () => {
it('dissolves the Sceneform ViewModel — SceneView keeps state in the composable tree', () => {
const composition = createComposition({
objects: [
createObject('StatefulObject', {
properties: [
{ key: 'geometry', value: 'cube' },
{ key: 'state', value: { count: 0 } },
],
}),
],
});
const result = compiler.compile(composition);
// No separate SceneState ViewModel file under the declarative ARScene model.
expect(result.stateFile).toBe('');
expect(result.nodeFactoryFile).toBe('');
expect(result.activityFile).toContain('ARScene');
});
});
describe('W4-T3 Wave-1 characterization (split gate)', () => {
it('AndroidCompiler fingerprint for empty Wave1 gate composition', () => {
const composition = createComposition({ name: 'Wave1SplitCharacterization' });
const out = compiler.compile(composition);
// Re-locked 2026-06-21: FULL Sceneform→SceneView retarget. activityFile is now a Compose
// ComponentActivity hosting a declarative ARScene { }; stateFile + nodeFactoryFile are ''
// (the ViewModel/NodeFactory dissolve under the declarative model); manifest gains
// xmlns:tools + tools:replace; build.gradle.kts emits io.github.sceneview:arsceneview:4.18.0,
// compileSdk 36, the compose-compiler plugin + compilerOptions DSL. Proven GREEN end-to-end:
// golden-diff + real gradlew assembleDebug + live ARCore session on a Galaxy S23. Prior
// (Sceneform) lock: 85ff47fd.
expect(hashRecordStrings(out as unknown as Record<string, unknown>)).toBe(
'ba9aa76b5a7c0e510e9cdea7c4fbcfd6d7465c22f09459cbfd2224fa9d1583df'
);
});
});
});