Skip to content

Commit

Permalink
feat: allow an action to have more than 2 states
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekyEggo committed Apr 22, 2024
1 parent 499db28 commit 940eb1e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@

# Change Log

## Unreleased

### ♻️ Changed

- `Actions[].States` can now contain more than 2 states.

## 0.3.1

### 🐞 Fix

- Fix missing `MinimumVersion` values.

## 0.3.0

### ♻️ Changed

- Renamed SCUF device type to `SCUFController`.

## 0.3.0

### ♻️ Changed
Expand Down
6 changes: 4 additions & 2 deletions src/streamdeck/plugins/manifest/__tests__/v6.4.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { validateStreamDeckPluginManifest } from "@tests";

const VERSION = "6.4";

describe("v6.4", () => {
/**
* Asserts a valid v6.4 manifest.
Expand All @@ -16,7 +18,7 @@ describe("v6.4", () => {
*/
test("Actions[].OS is not valid in v6.4", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("Actions[].OS.json", "6.4");
const errors = validateStreamDeckPluginManifest("Actions[].OS.json", (m) => (m.Software.MinimumVersion = VERSION));
expect(errors).toHaveError({
instancePath: "/Actions/0",
keyword: "additionalProperties",
Expand All @@ -29,7 +31,7 @@ describe("v6.4", () => {
*/
test("Profiles[].AutoInstall is not valid in v6.4", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("Profiles[].AutoInstall.json", "6.4");
const errors = validateStreamDeckPluginManifest("Profiles[].AutoInstall.json", (m) => (m.Software.MinimumVersion = VERSION));
expect(errors).toHaveError({
instancePath: "/Profiles/0",
keyword: "additionalProperties",
Expand Down
6 changes: 4 additions & 2 deletions src/streamdeck/plugins/manifest/__tests__/v6.5.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { validateStreamDeckPluginManifest } from "@tests";

const VERSION = "6.5";

describe("v6.5", () => {
/**
* Asserts a valid v6.5 manifest.
Expand All @@ -16,7 +18,7 @@ describe("v6.5", () => {
*/
test("Actions[].OS is not valid in v6.5", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("Actions[].OS.json", "6.5");
const errors = validateStreamDeckPluginManifest("Actions[].OS.json", (m) => (m.Software.MinimumVersion = VERSION));
expect(errors).toHaveError({
instancePath: "/Actions/0",
keyword: "additionalProperties",
Expand All @@ -29,7 +31,7 @@ describe("v6.5", () => {
*/
test("Profiles[].AutoInstall is not valid in v6.5", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("Profiles[].AutoInstall.json", "6.5");
const errors = validateStreamDeckPluginManifest("Profiles[].AutoInstall.json", (m) => (m.Software.MinimumVersion = VERSION));
expect(errors).toHaveError({
instancePath: "/Profiles/0",
keyword: "additionalProperties",
Expand Down
19 changes: 17 additions & 2 deletions src/streamdeck/plugins/manifest/__tests__/v6.6.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { validateStreamDeckPluginManifest } from "@tests";

const VERSION = "6.6";

describe("v6.6", () => {
/**
* Asserts a valid v6.6 manifest.
Expand All @@ -10,13 +12,26 @@ describe("v6.6", () => {
expect(errors).toHaveLength(0);
});

/**
* Asserts more than 2 states are allowed.
*/
test("allow more than 2 states", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("v6.6.json", (m) => {
m.Actions[0].States.push({ Image: "imgs/two" });
m.Actions[0].States.push({ Image: "imgs/three" });
m.Actions[0].States.push({ Image: "imgs/four" });
});
expect(errors).toHaveLength(0);
});

describe("v6.6 features", () => {
/**
* Asserts `Actions[].OS` is not valid for a v6.5 manifest.
*/
test("Actions[].OS is valid", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("Actions[].OS.json", "6.6");
const errors = validateStreamDeckPluginManifest("Actions[].OS.json", (m) => (m.Software.MinimumVersion = VERSION));
expect(errors).toHaveLength(0);
});

Expand All @@ -25,7 +40,7 @@ describe("v6.6", () => {
*/
test("Profiles[].AutoInstall is valid", () => {
// Arrange, act, assert.
const errors = validateStreamDeckPluginManifest("Profiles[].AutoInstall.json", "6.6");
const errors = validateStreamDeckPluginManifest("Profiles[].AutoInstall.json", (m) => (m.Software.MinimumVersion = VERSION));
expect(errors).toHaveLength(0);
});
});
Expand Down
3 changes: 1 addition & 2 deletions src/streamdeck/plugins/manifest/latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export type Action = {
Controllers?: [Controller, Controller?];

/**
* Determines whether the state of the action should automatically toggle when the user presses the action; only applies to actions that have two states defined. Default is
* Determines whether the state of the action should automatically toggle when the user presses the action; only applies to actions that have more than one state defined. Default is
* `false`.
* @example
* false
Expand Down Expand Up @@ -293,7 +293,6 @@ export type Action = {
*
* NB: Automatic toggling of the state on action activation can be disabled by setting `DisableAutomaticStates` to `true`.
* @minItems 1
* @maxItems 2
*/
States: State[];

Expand Down
15 changes: 7 additions & 8 deletions tests/validate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Ajv, { type ErrorObject } from "ajv";
import { join } from "node:path";
import { existsSync, readFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { keywordDefinitions } from "../src/index";
import type { Manifest } from "../src/streamdeck/plugins";

/**
* Validates the specified manifest file; when the version is specified the `Software.MinimumVersion` is updated.
* @param filename Name of the manifest file.
* @param version Optional software minimum version to apply prior to validating.
* @param modify Optional modifier to be applied to the manifest before validation.
* @returns Collection of errors as the result of validation.
*/
export function validateStreamDeckPluginManifest(filename: string, version?: string): ErrorObject<string, Record<string, unknown>, unknown>[] {
export function validateStreamDeckPluginManifest(filename: string, modify?: (manifest: Manifest) => void): ErrorObject<string, Record<string, unknown>, unknown>[] {
const schema = JSON.parse(getFileContents("../streamdeck/plugins/manifest.json"));
const validate = new Ajv()
.addKeyword(keywordDefinitions.errorMessage)
Expand All @@ -18,17 +20,14 @@ export function validateStreamDeckPluginManifest(filename: string, version?: str
.compile(schema);

const manifest = JSON.parse(getFileContents(join(`../src/streamdeck/plugins/manifest/__tests__/files/${filename}`)));
if (version) {
manifest.Software.MinimumVersion = version;
if (modify) {
modify(manifest);
}

validate(manifest);
return validate.errors ?? [];
}

import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";

/**
* Gets the file contents from the specified path relative to the tests folder.
* @param relativePath Path to the file, relative to the tests folder.
Expand Down

0 comments on commit 940eb1e

Please sign in to comment.