Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 21, 2024
1 parent 5e1363b commit a60e5c6
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ namespace starsky.feature.desktop.Interfaces;

public interface IOpenEditorDesktopService
{
/// <summary>
/// Is supported and enabled in the feature toggle
/// </summary>
/// <returns>Should you use it?</returns>
bool IsEnabled();

/// <summary>
/// Open a file in the default editor or specific editor which is set in the app settings
/// </summary>
/// <param name="f">dot comma split list with subPaths</param>
/// <param name="collections">should pick raw/jpeg file even its not specified</param>
/// <returns>files done and list of results</returns>
Task<(bool?, string, List<PathImageFormatExistsAppPathModel>)> OpenAsync(string f,
bool collections);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public OpenEditorDesktopService(AppSettings appSettings,
_openEditorPreflight = openEditorPreflight;
}

public bool IsEnabled()
{
return _appSettings.UseLocalDesktop == true &&
_openApplicationNativeService.DetectToUseOpenApplication();

Check warning on line 33 in starsky/starsky.feature.desktop/Service/OpenEditorDesktopService.cs

View check run for this annotation

Codecov / codecov/patch

starsky/starsky.feature.desktop/Service/OpenEditorDesktopService.cs#L33

Added line #L33 was not covered by tests
}

public async Task<(bool?, string, List<PathImageFormatExistsAppPathModel>)> OpenAsync(string f,
bool collections)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ public interface IMoveToTrashService
Task<List<FileIndexItem>> MoveToTrashAsync(List<string> inputFilePaths,
bool collections);

/// <summary>
/// Is it supported to use the system trash
/// But it does NOT check if the feature toggle is enabled
/// </summary>
/// <returns>true if supported</returns>
bool DetectToUseSystemTrash();

/// <summary>
/// Is supported and enabled in the feature toggle
/// </summary>
Expand Down
31 changes: 12 additions & 19 deletions starsky/starsky.feature.trash/Services/MoveToTrashService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using starsky.foundation.worker.Interfaces;

[assembly: InternalsVisibleTo("starskytest")]

namespace starsky.feature.trash.Services;

[Service(typeof(IMoveToTrashService), InjectionLifetime = InjectionLifetime.Scoped)]
Expand Down Expand Up @@ -46,17 +47,7 @@ ITrashConnectionService connectionService
public bool IsEnabled()
{
return _appSettings.UseSystemTrash == true &&
_systemTrashService.DetectToUseSystemTrash();
}

/// <summary>
/// Is it supported to use the system trash
/// But it does NOT check if the feature toggle is enabled
/// </summary>
/// <returns>true if supported</returns>
public bool DetectToUseSystemTrash()
{
return _systemTrashService.DetectToUseSystemTrash();
_systemTrashService.DetectToUseSystemTrash();
}

/// <summary>
Expand All @@ -74,11 +65,13 @@ public async Task<List<FileIndexItem>> MoveToTrashAsync(
await _metaPreflight.PreflightAsync(inputModel, inputFilePaths,
false, collections, 0);

(fileIndexResultsList, changedFileIndexItemName) = await AppendChildItemsToTrashList(fileIndexResultsList, changedFileIndexItemName);
( fileIndexResultsList, changedFileIndexItemName ) =
await AppendChildItemsToTrashList(fileIndexResultsList, changedFileIndexItemName);

var moveToTrashList =
fileIndexResultsList.Where(p =>
p.Status is FileIndexItem.ExifStatus.Ok or FileIndexItem.ExifStatus.Deleted).ToList();
p.Status is FileIndexItem.ExifStatus.Ok or FileIndexItem.ExifStatus.Deleted)
.ToList();

var isSystemTrashEnabled = IsEnabled();

Expand All @@ -92,9 +85,8 @@ await _queue.QueueBackgroundWorkItemAsync(async _ =>
return;
}
await MetaTrashInQueue(changedFileIndexItemName!,
await MetaTrashInQueue(changedFileIndexItemName,
fileIndexResultsList, inputModel, collections);
}, "trash");

return TrashConnectionService.StatusUpdate(moveToTrashList, isSystemTrashEnabled);
Expand All @@ -112,8 +104,9 @@ await _metaUpdateService.UpdateAsync(changedFileIndexItemName,
/// </summary>
/// <param name="moveToTrash"></param>
/// <param name="changedFileIndexItemName"></param>
internal async Task<(List<FileIndexItem>, Dictionary<string, List<string>>?)> AppendChildItemsToTrashList(List<FileIndexItem> moveToTrash,
Dictionary<string, List<string>> changedFileIndexItemName)
internal async Task<(List<FileIndexItem>, Dictionary<string, List<string>>)>
AppendChildItemsToTrashList(List<FileIndexItem> moveToTrash,
Dictionary<string, List<string>> changedFileIndexItemName)
{
var parentSubPaths = moveToTrash
.Where(p => !string.IsNullOrEmpty(p.FilePath) && p.IsDirectory == true)
Expand All @@ -122,7 +115,7 @@ await _metaUpdateService.UpdateAsync(changedFileIndexItemName,

if ( parentSubPaths.Count == 0 )
{
return (moveToTrash, changedFileIndexItemName);
return ( moveToTrash, changedFileIndexItemName );
}

var childItems = ( await _query.GetAllObjectsAsync(parentSubPaths) )
Expand All @@ -139,7 +132,7 @@ await _metaUpdateService.UpdateAsync(changedFileIndexItemName,
changedFileIndexItemName.TryAdd(childItem.FilePath!, new List<string> { "tags" });
}

return (moveToTrash, changedFileIndexItemName);
return ( moveToTrash, changedFileIndexItemName );
}

private async Task SystemTrashInQueue(List<FileIndexItem> moveToTrash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public class EnvFeaturesViewModel
/// Enable or disable some features on the frontend
/// </summary>
public bool UseLocalDesktop { get; set; }

/// <summary>
/// Is supported and enabled in the feature toggle
/// </summary>
public bool OpenEditorEnabled { get; set; }
}
5 changes: 5 additions & 0 deletions starsky/starsky/Controllers/AppSettingsFeaturesController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using starsky.feature.desktop.Interfaces;
using starsky.feature.trash.Interfaces;
using starsky.foundation.platform.Models;
using starsky.project.web.ViewModels;
Expand All @@ -10,11 +11,14 @@ public class AppSettingsFeaturesController : Controller
{
private readonly IMoveToTrashService _moveToTrashService;
private readonly AppSettings _appSettings;
private readonly IOpenEditorDesktopService _openEditorDesktopService;

public AppSettingsFeaturesController(IMoveToTrashService moveToTrashService,
IOpenEditorDesktopService openEditorDesktopService,
AppSettings appSettings)
{
_moveToTrashService = moveToTrashService;
_openEditorDesktopService = openEditorDesktopService;
_appSettings = appSettings;
}

Expand All @@ -38,6 +42,7 @@ public IActionResult FeaturesView()
{
SystemTrashEnabled = _moveToTrashService.IsEnabled(),
UseLocalDesktop = _appSettings.UseLocalDesktop == true,
OpenEditorEnabled = _openEditorDesktopService.IsEnabled()
};

return Json(shortAppSettings);
Expand Down
19 changes: 3 additions & 16 deletions starsky/starsky/Controllers/TrashController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,7 @@ public TrashController(IMoveToTrashService moveToTrashService)
}

/// <summary>
/// Is the system trash supported
/// </summary>
/// <returns>bool with json (IActionResult Result)</returns>
/// <response code="200">the item including the updated content</response>
/// <response code="401">User unauthorized</response>
[ProducesResponseType(typeof(bool), 200)]
[HttpGet("/api/trash/detect-to-use-system-trash")]
[Produces("application/json")]
public IActionResult DetectToUseSystemTrash()
{
return Json(_moveToTrashService.DetectToUseSystemTrash());
}

/// <summary>
/// (beta) Move a file to the trash
/// Move a file to the trash
/// </summary>
/// <param name="f">subPath filepath to file, split by dot comma (;)</param>
/// <param name="collections">stack collections</param>
Expand All @@ -56,7 +42,8 @@ public async Task<IActionResult> TrashMoveAsync(string f, bool collections = fal
return BadRequest("No input files");
}

var fileIndexResultsList = await _moveToTrashService.MoveToTrashAsync(inputFilePaths.ToList(), collections);
var fileIndexResultsList =
await _moveToTrashService.MoveToTrashAsync(inputFilePaths.ToList(), collections);

return Json(fileIndexResultsList);
}
Expand Down
2 changes: 1 addition & 1 deletion starsky/starsky/clientapp/src/shared/url-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class UrlQuery {
};

public UrlApiFeaturesAppSettings = (): string => {
return this.prefix + "/api/env/features";
return this.prefix + "/api/env/features?v=0.6.0-beta.2";
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void FeaturesViewTest()
// Arrange
var fakeIMoveToTrashService = new FakeIMoveToTrashService(new List<FileIndexItem>());
var appSettingsFeaturesController = new AppSettingsFeaturesController(
fakeIMoveToTrashService, new AppSettings());
fakeIMoveToTrashService, new FakeIOpenEditorDesktopService(), new AppSettings());

// Act
var result = appSettingsFeaturesController.FeaturesView() as JsonResult;
Expand All @@ -35,7 +35,8 @@ public void FeaturesViewTest_Disabled()
// Arrange
var fakeIMoveToTrashService = new FakeIMoveToTrashService(new List<FileIndexItem>(), false);
var appSettingsFeaturesController = new AppSettingsFeaturesController(
fakeIMoveToTrashService, new AppSettings { UseLocalDesktop = false });
fakeIMoveToTrashService, new FakeIOpenEditorDesktopService(),
new AppSettings { UseLocalDesktop = false });

// Act
var result = appSettingsFeaturesController.FeaturesView() as JsonResult;
Expand All @@ -53,7 +54,8 @@ public void FeaturesViewTest_Enabled()
// Arrange
var fakeIMoveToTrashService = new FakeIMoveToTrashService(new List<FileIndexItem>());
var appSettingsFeaturesController = new AppSettingsFeaturesController(
fakeIMoveToTrashService, new AppSettings { UseLocalDesktop = true });
fakeIMoveToTrashService, new FakeIOpenEditorDesktopService(),
new AppSettings { UseLocalDesktop = true });

// Act
var result = appSettingsFeaturesController.FeaturesView() as JsonResult;
Expand Down
30 changes: 8 additions & 22 deletions starsky/starskytest/Controllers/TrashControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,31 @@ public async Task TrashControllerTest_BadInput()
var controller = new TrashController(
new FakeIMoveToTrashService(new List<FileIndexItem>()));
var result = await controller.TrashMoveAsync(null!, true) as BadRequestObjectResult;
Assert.AreEqual(400,result?.StatusCode);
Assert.AreEqual(400, result?.StatusCode);
}

[TestMethod]
public async Task TrashControllerTest_NotFound()
{
var controller = new TrashController(
new FakeIMoveToTrashService(new List<FileIndexItem>()));
var result = await controller.TrashMoveAsync("/test.jpg", true) as JsonResult;
var resultValue = result?.Value as List<FileIndexItem>;

Assert.AreEqual(1, resultValue?.Count);
}

[TestMethod]
public async Task TrashControllerTest_Ok()
{
var controller = new TrashController(
new FakeIMoveToTrashService(new List<FileIndexItem>{new FileIndexItem("/test.jpg")
new FakeIMoveToTrashService(new List<FileIndexItem>
{
Status = FileIndexItem.ExifStatus.Ok
}}));
new FileIndexItem("/test.jpg") { Status = FileIndexItem.ExifStatus.Ok }
}));
var result = await controller.TrashMoveAsync("/test.jpg", true) as JsonResult;
var resultValue = result?.Value as List<FileIndexItem>;

Assert.AreEqual(1, resultValue?.Count);
}

[TestMethod]
public void DetectToUseSystemTrash_Ok()
{
var controller = new TrashController(
new FakeIMoveToTrashService(new List<FileIndexItem>()));

var result = controller.DetectToUseSystemTrash() as JsonResult;

var tryParseResult = bool.TryParse(result?.Value?.ToString(), out var resultValue);

Assert.AreEqual(true, tryParseResult);
Assert.AreEqual(true, resultValue);
}
}
43 changes: 43 additions & 0 deletions starsky/starskytest/FakeMocks/FakeIOpenEditorDesktopService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using starsky.feature.desktop.Interfaces;
using starsky.feature.desktop.Models;
using starsky.foundation.database.Models;
using starsky.foundation.platform.Helpers;

namespace starskytest.FakeMocks;

public class FakeIOpenEditorDesktopService : IOpenEditorDesktopService
{
private readonly bool _isEnabled;

public FakeIOpenEditorDesktopService(bool isEnabled = true)
{
_isEnabled = isEnabled;
}

public bool IsEnabled()
{
return _isEnabled;
}

public async Task<(bool?, string, List<PathImageFormatExistsAppPathModel>)> OpenAsync(string f,
bool collections)
{
await Task.Yield();

var list = new List<PathImageFormatExistsAppPathModel>
{
new PathImageFormatExistsAppPathModel
{
AppPath = "test",
Status = FileIndexItem.ExifStatus.Ok,
ImageFormat = ExtensionRolesHelper.ImageFormat.jpg,
SubPath = "/test.jpg",
FullFilePath = "/test.jpg"
}
};

return ( _isEnabled, "Opened", list );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class OpenEditorDesktopServiceTest
[TestMethod]
public async Task OpenAsync_stringInput_HappyFlow()
{
var fakeService =
new FakeIOpenApplicationNativeService(new List<string> { "/test.jpg" }, "test");
var fakeService = new FakeIOpenApplicationNativeService(
new List<string> { "/test.jpg" }, "test");

var appSettings = new AppSettings
{
Expand Down Expand Up @@ -153,15 +153,14 @@ public async Task OpenAsync_ListInput_UseLocalDesktop_Null()
[TestMethod]
public async Task OpenAsync_ListInput_UnSupportedPlatform()
{
var fakeService =
new FakeIOpenApplicationNativeService(new List<string>(), string.Empty, false);
var fakeService = new FakeIOpenApplicationNativeService(new List<string>(),
string.Empty, false);

var appSettings = new AppSettings { UseLocalDesktop = true };

var preflight = new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>());

var service =
new OpenEditorDesktopService(appSettings, fakeService, preflight);
var service = new OpenEditorDesktopService(appSettings, fakeService, preflight);

var (success, status, list) =
( await service.OpenAsync(new List<string> { "/test.jpg" }, true) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,6 @@ public async Task InMetaTrash_WithDbContext_Directory()
Assert.AreEqual(TrashKeyword.TrashKeywordString, result[1].Tags);
}

[TestMethod]
public void DetectToUseSystemTrash_False()
{
var trashService = new FakeITrashService() { IsSupported = false };
var moveToTrashService = new MoveToTrashService(new AppSettings(), new FakeIQuery(),
new FakeMetaPreflight(), new FakeIUpdateBackgroundTaskQueue(),
trashService, new FakeIMetaUpdateService(),
new FakeITrashConnectionService());

var result = moveToTrashService.DetectToUseSystemTrash();

Assert.AreEqual(false, result);
}

[TestMethod]
public async Task AppendChildItemsToTrashList_NoAny()
{
Expand Down

0 comments on commit a60e5c6

Please sign in to comment.