// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.core.module;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.terasology.gestalt.module.Module;
import org.terasology.gestalt.module.ModuleFactory;
import org.terasology.gestalt.module.ModuleMetadata;
import org.terasology.gestalt.module.sandbox.API;
import org.terasology.gestalt.naming.Name;
import org.terasology.gestalt.naming.Version;
import org.terasology.unittest.ExampleClass;

import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ClasspathCompromisingModuleFactoryTest {
    final static Class<?> someClassOutsideTheModule = ClasspathCompromisingModuleFactory.class;

    @Test
    public void directoryModuleContainsClass() {
        ModuleFactory factory = new ClasspathCompromisingModuleFactory();

        File engineTestDirectory = new File(System.getProperty("user.dir", "."));

        ModuleMetadata metadata = new ModuleMetadata(new Name("unittest"), new Version("1.0.0"));

        Module module = factory.createDirectoryModule(metadata, engineTestDirectory);

        assertTrue(module.getClassPredicate().test(ExampleClass.class));
        assertFalse(module.getClassPredicate().test(someClassOutsideTheModule));
    }

    @Test
    @Disabled("TODO: need a jar module containing classes")
    public void archiveModuleContainsClass() throws IOException {
        ModuleFactory factory = new ClasspathCompromisingModuleFactory();

        Module module = factory.createArchiveModule(new File("FIXME.jar"));

        Class<?> someClassInTheModule = module.getModuleManifest().getTypesAnnotatedWith(API.class).iterator().next();

        assertTrue(module.getClassPredicate().test(someClassInTheModule));
        assertFalse(module.getClassPredicate().test(someClassOutsideTheModule));
    }

    @Test
    @Disabled("TODO: need a jar module alongside a classes directory")
    public void directoryModuleContainsClassLoadedFromJar() {
        // Example:
        //   - m/build/classes/org/t/Foo.class
        //   - m/build/libs/foo.jar
        // load m as directory module while foo.jar is on classpath
    }
}

@@ -977,32 +977,142 @@
 duleFactory();%0A%0A
+        // This test assumes that the unittest module is under the current working directory (%60engine-test/%60)%0A
         File eng
@@ -1173,25 +1173,24 @@
 ir%22, %22.%22));%0A
-%0A
         Modu
@@ -1271,25 +1271,24 @@
 (%221.0.0%22));%0A
-%0A
         Modu
@@ -1358,24 +1358,82 @@
 irectory);%0A%0A
+        // and that ExampleClass is inside that directory%0A
         asse
@@ -1489,24 +1489,117 @@
 ss.class));%0A
+        // and that this other class (in engine, not engine-test) is outside that directory.%0A
         asse
@@ -1658,32 +1658,109 @@
 sideTheModule));
+%0A%0A        // These assumptions could break if things get moved around enough.
 %0A    %7D%0A%0A    @Tes

document the assumptions here