Skip to content

Commit

Permalink
Vuexのコマンドのsnapshotテストを追加 (#2175)
Browse files Browse the repository at this point in the history
* stash

* stash

* Fix: コマンドのunixMillisecをUUID4にする

* ミス

* 完成

* メモが残っていた

* fix

* Update src/helpers/random.ts

Co-authored-by: Nanashi. <[email protected]>

* update

---------

Co-authored-by: Nanashi. <[email protected]>
  • Loading branch information
Hiroshiba and sevenc-nanashi authored Jul 24, 2024
1 parent b8a6b03 commit bb42a89
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/backend/browser/fileImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { openDB } from "./browserConfig";
import { SandboxKey } from "@/type/preload";
import { failure, success } from "@/type/result";
import { createLogger } from "@/domain/frontend/log";
import { uuid4 } from "@/helpers/random";

const log = createLogger("fileImpl");

Expand Down Expand Up @@ -200,7 +201,7 @@ export const showOpenFilePickerImpl = async (options: {
});
const paths = [];
for (const handle of handles) {
const fakePath = `<browser-dummy-${crypto.randomUUID()}>-${handle.name}`;
const fakePath = `<browser-dummy-${uuid4()}>-${handle.name}`;
fileHandleMap.set(fakePath, handle);
paths.push(fakePath);
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Sing/ScoreSequencer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ import {
import { applyGaussianFilter, linearInterpolation } from "@/sing/utility";
import { useLyricInput } from "@/composables/useLyricInput";
import { ExhaustiveError } from "@/type/utility";
import { uuid4 } from "@/helpers/random";
type PreviewMode =
| "ADD_NOTE"
Expand Down Expand Up @@ -720,7 +721,7 @@ const startPreview = (event: MouseEvent, mode: PreviewMode, note?: Note) => {
return;
}
note = {
id: NoteId(crypto.randomUUID()),
id: NoteId(uuid4()),
position: guideLineTicks,
duration: snapTicks.value,
noteNumber: cursorNoteNumber,
Expand Down
27 changes: 27 additions & 0 deletions src/helpers/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 乱数値を生成する。モックに対応している。
* モックモードでは呼ばれた回数に応じて固定の値を返す。
*/

let mockMode = false;
let mockCount = 0;

/**
* モックモードにし、呼ばれた回数をリセットする。
*/
export function resetMockMode(): void {
mockMode = true;
mockCount = 0;
}

/**
* v4 UUID を生成する。
*/
export function uuid4(): string {
if (!mockMode) {
return crypto.randomUUID();
} else {
mockCount++;
return `00000000-0000-4000-0000-${mockCount.toString().padStart(12, "0")}`;
}
}
3 changes: 2 additions & 1 deletion src/sing/utaformatixProject/toVoicevox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DEFAULT_TPQN, createDefaultTrack } from "@/sing/domain";
import { getDoremiFromNoteNumber } from "@/sing/viewHelper";
import { NoteId } from "@/type/preload";
import { Note, Tempo, TimeSignature, Track } from "@/store/type";
import { uuid4 } from "@/helpers/random";

/** UtaformatixのプロジェクトをVoicevoxの楽譜データに変換する */
export const ufProjectToVoicevox = (project: UfProject): VoicevoxScore => {
Expand Down Expand Up @@ -72,7 +73,7 @@ export const ufProjectToVoicevox = (project: UfProject): VoicevoxScore => {

const notes = trackNotes.map((value): Note => {
return {
id: NoteId(crypto.randomUUID()),
id: NoteId(uuid4()),
position: convertPosition(value.tickOn, projectTpqn, tpqn),
duration: convertDuration(
value.tickOn,
Expand Down
3 changes: 2 additions & 1 deletion src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ import { AudioQuery, AccentPhrase, Speaker, SpeakerInfo } from "@/openapi";
import { base64ImageToUri, base64ToUri } from "@/helpers/base64Helper";
import { getValueOrThrow, ResultError } from "@/type/result";
import { generateWriteErrorMessage } from "@/helpers/fileHelper";
import { uuid4 } from "@/helpers/random";

function generateAudioKey() {
return AudioKey(crypto.randomUUID());
return AudioKey(uuid4());
}

function parseTextFile(
Expand Down
3 changes: 2 additions & 1 deletion src/store/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MutationTree,
} from "@/store/vuex";
import { CommandId, EditorType } from "@/type/preload";
import { uuid4 } from "@/helpers/random";

enablePatches();
enableMapSet();
Expand Down Expand Up @@ -67,7 +68,7 @@ const recordPatches =
(draft: S) => recipe(draft, payload),
);
return {
id: CommandId(crypto.randomUUID()),
id: CommandId(uuid4()),
redoPatches: doPatches,
undoPatches: undoPatches,
};
Expand Down
3 changes: 2 additions & 1 deletion src/store/preset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createPartialStore } from "./vuex";
import { uuid4 } from "@/helpers/random";
import { PresetStoreState, PresetStoreTypes, State } from "@/store/type";
import { Preset, PresetKey, Voice, VoiceId } from "@/type/preload";

Expand Down Expand Up @@ -181,7 +182,7 @@ export const presetStore = createPartialStore<PresetStoreTypes>({

ADD_PRESET: {
async action(context, { presetData }: { presetData: Preset }) {
const newKey = PresetKey(crypto.randomUUID());
const newKey = PresetKey(uuid4());
const newPresetItems = {
...context.state.presetItems,
[newKey]: presetData,
Expand Down
5 changes: 3 additions & 2 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import { createLogger } from "@/domain/frontend/log";
import { noteSchema } from "@/domain/project/schema";
import { getOrThrow } from "@/helpers/mapHelper";
import { ufProjectToVoicevox } from "@/sing/utaformatixProject/toVoicevox";
import { uuid4 } from "@/helpers/random";

const logger = createLogger("store/singing");

Expand Down Expand Up @@ -1724,7 +1725,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
const track = tracks[trackIndex];
const notes = track.notes.map((note) => ({
...note,
id: NoteId(crypto.randomUUID()),
id: NoteId(uuid4()),
}));

if (tpqn !== state.tpqn) {
Expand Down Expand Up @@ -2144,7 +2145,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
const quantizedPastePos =
Math.round(pasteOriginPos / snapTicks) * snapTicks;
return {
id: NoteId(crypto.randomUUID()),
id: NoteId(uuid4()),
position: quantizedPastePos,
duration: Number(note.duration),
noteNumber: Number(note.noteNumber),
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/lib/selectPriorPhrase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@/store/type";
import { DEFAULT_TPQN, selectPriorPhrase } from "@/sing/domain";
import { NoteId } from "@/type/preload";
import { uuid4 } from "@/helpers/random";

const createPhrase = (
firstRestDuration: number,
Expand All @@ -18,7 +19,7 @@ const createPhrase = (
firstRestDuration: firstRestDuration * DEFAULT_TPQN,
notes: [
{
id: NoteId(crypto.randomUUID()),
id: NoteId(uuid4()),
position: start * DEFAULT_TPQN,
duration: (end - start) * DEFAULT_TPQN,
noteNumber: 60,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/lib/utaformatixProject/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
createDefaultTrack,
} from "@/sing/domain";
import { NoteId } from "@/type/preload";
import { uuid4 } from "@/helpers/random";

const createNoteId = () => NoteId(crypto.randomUUID());
const createNoteId = () => NoteId(uuid4());

it("トラックを変換できる", async () => {
const track = createDefaultTrack();
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/store/__snapshots__/command.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`コマンド実行で履歴が作られる 1`] = `
{
"audioKeys": [
"00000000-0000-4000-0000-000000000001",
],
"redoCommands": {
"song": [],
"talk": [],
},
"undoCommands": {
"song": [],
"talk": [
{
"id": "00000000-0000-4000-0000-000000000002",
"redoPatches": [
{
"op": "replace",
"path": [
"audioKeys",
],
"value": [
"00000000-0000-4000-0000-000000000001",
],
},
],
"undoPatches": [
{
"op": "replace",
"path": [
"audioKeys",
],
"value": [],
},
],
},
],
},
}
`;
19 changes: 19 additions & 0 deletions tests/unit/store/command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { toRaw } from "vue";
import { store } from "@/store";
import { AudioKey } from "@/type/preload";
import { resetMockMode, uuid4 } from "@/helpers/random";

const initialState = structuredClone(toRaw(store.state));
beforeEach(() => {
store.replaceState(initialState);

resetMockMode();
});

test("コマンド実行で履歴が作られる", async () => {
await store.dispatch("COMMAND_SET_AUDIO_KEYS", {
audioKeys: [AudioKey(uuid4())],
});
const { audioKeys, redoCommands, undoCommands } = store.state;
expect({ audioKeys, redoCommands, undoCommands }).toMatchSnapshot();
});
5 changes: 3 additions & 2 deletions tests/unit/store/utility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isOnCommandOrCtrlKeyDown,
filterCharacterInfosByStyleType,
} from "@/store/utility";
import { uuid4 } from "@/helpers/random";

function createDummyMora(text: string): Mora {
return {
Expand Down Expand Up @@ -305,13 +306,13 @@ describe("filterCharacterInfosByStyleType", () => {
const createCharacterInfo = (
styleTypes: (undefined | "talk" | "frame_decode" | "sing")[],
): CharacterInfo => {
const engineId = EngineId(crypto.randomUUID());
const engineId = EngineId(uuid4());
return {
portraitPath: "path/to/portrait",
metas: {
policy: "policy",
speakerName: "speakerName",
speakerUuid: SpeakerId(crypto.randomUUID()),
speakerUuid: SpeakerId(uuid4()),
styles: styleTypes.map((styleType) => ({
styleType,
styleName: "styleName",
Expand Down

0 comments on commit bb42a89

Please sign in to comment.