Skip to content

Commit

Permalink
update some automation, frontend code
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Jun 14, 2024
1 parent 0655f81 commit e972fcc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Bundler.Client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ function App() {

if (!MediaConverter.isValidFileType(file) && !isZipFile(file)) throw Error("Invalid file type.");

if (isZipFile(file)) return handleZipUpload(file);
if (isZipFile(file)) return await handleZipUpload(file);
}

if (MediaConverter.areFilesValid(files)) {
handleConversions(files);
await handleConversions(files);
}
} catch (exception) {
toast.error(handleUploadError((exception as Error).message));
Expand Down
2 changes: 1 addition & 1 deletion Bundler.Client/src/services/MediaConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class MediaConverter {
public static areFilesValid(files: Array<File>): boolean {
if (files.length === 0) false;

return files.every((file) => MediaConverter.isValidFileType(file));
return files.every((file) => file.size > 0 && MediaConverter.isValidFileType(file));
}

protected isMediaFile(file: unknown): file is MediaFile {
Expand Down
4 changes: 2 additions & 2 deletions Bundler.QA/Frontend/BundlerPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void UploadFile(string name, bool isAsset = true)
this._webdriver.Find(FileInput)?.SendKeys(file);
}

public List<object> GetIndexedDBData(string dbName, string storeName)
public T GetIndexedDBData<T>(string dbName, string storeName)
{
return this._webdriver.GetIndexedDBData(dbName, storeName);
return this._webdriver.GetIndexedDBData<T>(dbName, storeName);
}

public void AssertSuccessToast(string message)
Expand Down
98 changes: 89 additions & 9 deletions Bundler.QA/Frontend/BundlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,44 @@

namespace Bundler.QA.Frontend
{
#region Types

internal sealed class BinaryCache : Dictionary<string, object>
{
public BinaryCache(Dictionary<string, object> data)
{
this["ctr"] = data.TryGetValue("ctr", out object? ctr) ? ctr : default!;
this["hac"] = data.TryGetValue("hac", out object? hac) ? hac : default!;
this["cafe"] = data.TryGetValue("cafe", out object? cafe) ? cafe : default!;
this["timestamp"] = data.TryGetValue("timestamp", out object? value) ? value : default!;
}
};

internal sealed class AssetCache : Dictionary<string, object>
{
public AssetCache(Dictionary<string, object> data)
{
this["file"] = data.TryGetValue("file", out object? file) ? file : default!;
this["timestamp"] = data.TryGetValue("timestamp", out object? value) ? value : default!;
}
}


#endregion

internal class BundlerTest
{
private BundlerPage _page;

#region Toast Messages

private string InvalidTexture = "Error: Texture '{0}' dimensions invalid.";
private string InvalidFileType = "Error: Invalid file type.";
private string InvalidTextureFile = "Error: Texture '{0}' is invalid.";
private string InvalidFontFile = "Error: Font '{0}' is invalid.";
private readonly string InvalidTexture = "Error: Texture '{0}' dimensions invalid.";
private readonly string InvalidFileType = "Error: Invalid file type.";
private readonly string InvalidTextureFile = "Error: Texture '{0}' is invalid.";
private readonly string InvalidFontFile = "Error: Font '{0}' is invalid.";

private readonly List<string> DefaultBundleNames = new() { "SuperGame.3dsx", "SuperGame.nro", "SuperGame.wuhb" };
private readonly List<string> DefaultAssetNames = new() { "ctr-assets.zip", "hac-assets.zip", "cafe-assets.zip" };
private readonly List<string> DefaultBundleNames = ["SuperGame.3dsx", "SuperGame.nro", "SuperGame.wuhb"];
private readonly List<string> DefaultAssetNames = ["ctr-assets.zip", "hac-assets.zip", "cafe-assets.zip"];

#endregion

Expand All @@ -28,6 +53,18 @@ public void Setup()
public void Teardown()
=> this._page.Cleanup();

#region Helpers

private static void CreateBundle(string name, Span<(string name, string path)> files)
{
var assets = Assets.Instance();

using var bundle = ZipFile.Open(name, ZipArchiveMode.Create);
foreach (var file in files) bundle.CreateEntryFromFile(assets.GetFilepath(file.name), file.path);
}

#endregion

#region Texture Upload

[TestCase("cat_big_width.png")]
Expand Down Expand Up @@ -156,7 +193,7 @@ public void TestBasicBundle()

Assert.Multiple(() =>
{
Assert.That(download.Entries, Has.Count.EqualTo(5)); // all binaries, logs
Assert.That(download.Entries, Has.Count.EqualTo(4)); // all binaries, logs
Assert.That(download.Entries.Any(x => DefaultBundleNames.Contains(x.Name)));
foreach (var bundleName in DefaultBundleNames)
Expand Down Expand Up @@ -189,8 +226,51 @@ public void TestBundlerCachingSuccess()
this._page.UploadFile("bundle-Main.zip");
this._page.AssertSuccessToast("Success.");

var stores = this._page.GetIndexedDBData("bundler", "binaryCache");
Console.WriteLine(stores.Count);
var _stores = this._page.GetIndexedDBData<IReadOnlyCollection<object>>("bundler", "binaryCache");
Assert.That(_stores, Has.Count.EqualTo(1));

var _cache = new BinaryCache((Dictionary<string, object>)_stores.ElementAt(0));
Assert.That(_cache, Has.Count.EqualTo(4));
}

[TestCase]
public void TestBundlerAssetCachingSuccess()
{
try
{
Span<(string name, string path)> files = new[]
{
("lovebrew.toml", "lovebrew.toml"),
("Oneday.otf", "game/Oneday.otf"),
("lenny.png", "game/lenny.png"),
("dio.jpg", "game/dio.jpg"),
("main.lua", "game/main.lua"),
};

CreateBundle("bundle.zip", files);

this._page.UploadFile($"{Directory.GetCurrentDirectory()}\\bundle.zip", false);
this._page.AssertSuccessToast("Success.");

var _stores = this._page.GetIndexedDBData<IReadOnlyCollection<object>>("bundler", "assetCache");
Assert.That(_stores, Has.Count.EqualTo(1));

for (int index = 0; index < _stores.Count; index++)
{
var _cache = new AssetCache((Dictionary<string, object>)_stores.ElementAt(index));
Assert.That(_cache, Has.Count.EqualTo(2));
}
}
catch (InvalidDataException)
{
Console.WriteLine("Resulting bundle download did not append game archive.");
}
finally
{
File.Delete("bundle.zip");
}


}

#endregion
Expand Down
35 changes: 25 additions & 10 deletions Bundler.QA/Frontend/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,40 @@ public string Title()
return element;
}

public List<object> GetIndexedDBData(string name, string storeName)
public T GetIndexedDBData<T>(string name, string storeName)
{
var script = @"
const request = window.indexedDB.open(arguments[0]);
request.onsuccess = function(event) {
const database = event.target.result;
const storeName = arguments[1];
const transaction = database.transaction([arguments[1]], 'readonly');
const store = transaction.objectStore(arguments[1]);
const promised = new Promise((resolve, reject) => {
request.onsuccess = function(event) {
const database = event.target.result;
store.getAll().onsuccess = function(event) {
return event.target.result;
const transaction = database.transaction([storeName], 'readonly');
const store = transaction.objectStore(storeName);
const allItems = store.getAll();
allItems.onsuccess = function(event) {
console.log(event.target.result);
resolve(event.target.result);
};
};
request.onerror = function(event) {
console.log(event.target.result);
reject(event.target.error);
};
};
});
return promised.then((value) => { return value });
";

return (List<object>)((IJavaScriptExecutor)_driver).ExecuteAsyncScript(script, name, storeName);

var executor = (IJavaScriptExecutor)this._driver;
return (T)executor.ExecuteScript(script, name, storeName);
}


public IWebElement? WaitFor(By search, int seconds = 10)
{
IWebElement? element = null;
Expand Down

0 comments on commit e972fcc

Please sign in to comment.