feat: synchronize account icons across game profiles
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { GamePrisma } from '@sammo-ts/infra';
|
||||
import type { TurnSchedule } from '@sammo-ts/logic';
|
||||
|
||||
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
||||
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||
import { createTurnDaemonCommandHandler } from '../src/turn/worldCommandHandler.js';
|
||||
|
||||
const revision = '2026-07-31T09:00:00.000Z';
|
||||
const requestId = `general:adjustIcon:user-1:${revision}`;
|
||||
const schedule: TurnSchedule = { entries: [{ startMinute: 0, tickMinutes: 10 }] };
|
||||
|
||||
const buildGeneral = (overrides: Partial<TurnGeneral> = {}): TurnGeneral => ({
|
||||
id: 1,
|
||||
userId: 'user-1',
|
||||
name: '아이콘장수',
|
||||
nationId: 0,
|
||||
cityId: 0,
|
||||
troopId: 0,
|
||||
stats: { leadership: 50, strength: 50, intelligence: 50 },
|
||||
turnTime: new Date('2026-07-31T09:00:00.000Z'),
|
||||
recentWarTime: null,
|
||||
role: {
|
||||
items: { horse: null, weapon: null, book: null, item: null },
|
||||
personality: null,
|
||||
specialDomestic: null,
|
||||
specialWar: null,
|
||||
},
|
||||
triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} },
|
||||
meta: { killturn: 24 },
|
||||
officerLevel: 0,
|
||||
experience: 0,
|
||||
dedication: 0,
|
||||
injury: 0,
|
||||
gold: 0,
|
||||
rice: 0,
|
||||
crew: 0,
|
||||
crewTypeId: 0,
|
||||
train: 0,
|
||||
atmos: 0,
|
||||
age: 20,
|
||||
npcState: 0,
|
||||
picture: 'old.jpg',
|
||||
imageServer: 0,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const buildWorld = (generals: TurnGeneral[]) => {
|
||||
const state: TurnWorldState = {
|
||||
id: 1,
|
||||
currentYear: 190,
|
||||
currentMonth: 1,
|
||||
tickSeconds: 600,
|
||||
lastTurnTime: new Date('2026-07-31T09:00:00.000Z'),
|
||||
meta: {},
|
||||
};
|
||||
const snapshot: TurnWorldSnapshot = {
|
||||
generals,
|
||||
cities: [],
|
||||
nations: [],
|
||||
troops: [],
|
||||
diplomacy: [],
|
||||
events: [],
|
||||
initialEvents: [],
|
||||
scenarioConfig: {
|
||||
stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 50, npcMin: 10, chiefMin: 65 },
|
||||
iconPath: '',
|
||||
map: {},
|
||||
const: {},
|
||||
environment: { mapName: 'test', unitSet: 'test' },
|
||||
},
|
||||
scenarioMeta: {
|
||||
title: 'test',
|
||||
startYear: 190,
|
||||
life: null,
|
||||
fiction: null,
|
||||
history: [],
|
||||
ignoreDefaultEvents: false,
|
||||
},
|
||||
map: {
|
||||
id: 'test',
|
||||
name: 'test',
|
||||
cities: [],
|
||||
defaults: { trust: 50, trade: 100, supplyState: 1, frontState: 0 },
|
||||
},
|
||||
};
|
||||
return new InMemoryTurnWorld(state, snapshot, { schedule });
|
||||
};
|
||||
|
||||
const buildCommandDb = (actorUserId = 'user-1') =>
|
||||
({
|
||||
inputEvent: {
|
||||
findUnique: vi.fn(async () => ({
|
||||
createdAt: new Date('2026-07-31T09:00:00.000Z'),
|
||||
actorUserId,
|
||||
target: 'ENGINE',
|
||||
eventType: 'adjustGeneralIcon',
|
||||
})),
|
||||
},
|
||||
}) as unknown as GamePrisma.TransactionClient;
|
||||
|
||||
const command = (
|
||||
overrides: Partial<{
|
||||
requestId: string;
|
||||
userId: string;
|
||||
picture: string;
|
||||
imageServer: number;
|
||||
iconRevision: string;
|
||||
}> = {}
|
||||
) => ({
|
||||
type: 'adjustGeneralIcon' as const,
|
||||
requestId,
|
||||
userId: 'user-1',
|
||||
picture: 'new.png',
|
||||
imageServer: 1,
|
||||
iconRevision: revision,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe('adjustGeneralIcon ENGINE command', () => {
|
||||
it('updates only the authenticated human general and preserves existing meta', async () => {
|
||||
const human = buildGeneral();
|
||||
const possessedNpc = buildGeneral({ id: 2, npcState: 1 });
|
||||
const foreign = buildGeneral({ id: 3, userId: 'user-2' });
|
||||
const world = buildWorld([human, possessedNpc, foreign]);
|
||||
const handler = createTurnDaemonCommandHandler({ world });
|
||||
|
||||
await expect(handler.handle(command(), { db: buildCommandDb() })).resolves.toEqual({
|
||||
type: 'adjustGeneralIcon',
|
||||
ok: true,
|
||||
generalId: human.id,
|
||||
updated: true,
|
||||
});
|
||||
expect(world.getGeneralById(human.id)).toMatchObject({
|
||||
picture: 'new.png',
|
||||
imageServer: 1,
|
||||
meta: {
|
||||
killturn: 24,
|
||||
accountIconUpdatedAt: revision,
|
||||
},
|
||||
});
|
||||
expect(world.getGeneralById(possessedNpc.id)).toMatchObject({ picture: 'old.jpg', imageServer: 0 });
|
||||
expect(world.getGeneralById(foreign.id)).toMatchObject({ picture: 'old.jpg', imageServer: 0 });
|
||||
});
|
||||
|
||||
it('treats no human general as a successful no-op', async () => {
|
||||
const world = buildWorld([buildGeneral({ id: 2, npcState: 1 }), buildGeneral({ id: 3, userId: 'user-2' })]);
|
||||
const handler = createTurnDaemonCommandHandler({ world });
|
||||
|
||||
await expect(handler.handle(command(), { db: buildCommandDb() })).resolves.toEqual({
|
||||
type: 'adjustGeneralIcon',
|
||||
ok: true,
|
||||
generalId: null,
|
||||
updated: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps an identical revision idempotent and rejects rollback or tuple conflicts', async () => {
|
||||
const world = buildWorld([
|
||||
buildGeneral({
|
||||
picture: 'new.png',
|
||||
imageServer: 1,
|
||||
meta: { killturn: 24, accountIconUpdatedAt: revision },
|
||||
}),
|
||||
]);
|
||||
const handler = createTurnDaemonCommandHandler({ world });
|
||||
|
||||
await expect(handler.handle(command(), { db: buildCommandDb() })).resolves.toMatchObject({
|
||||
ok: true,
|
||||
updated: false,
|
||||
});
|
||||
await expect(
|
||||
handler.handle(command({ iconRevision: '2026-07-31T08:59:59.000Z' }), {
|
||||
db: buildCommandDb(),
|
||||
})
|
||||
).resolves.toMatchObject({ ok: false, code: 'CONFLICT' });
|
||||
await expect(
|
||||
handler.handle(command({ picture: 'conflict.png' }), { db: buildCommandDb() })
|
||||
).resolves.toMatchObject({ ok: false, code: 'CONFLICT' });
|
||||
expect(world.getGeneralById(1)).toMatchObject({ picture: 'new.png', imageServer: 1 });
|
||||
});
|
||||
|
||||
it('fails closed for a corrupt watermark or mismatched input-event actor', async () => {
|
||||
const corruptWorld = buildWorld([buildGeneral({ meta: { killturn: 24, accountIconUpdatedAt: 'not-a-date' } })]);
|
||||
const corruptHandler = createTurnDaemonCommandHandler({ world: corruptWorld });
|
||||
await expect(corruptHandler.handle(command(), { db: buildCommandDb() })).resolves.toMatchObject({
|
||||
ok: false,
|
||||
code: 'PRECONDITION_FAILED',
|
||||
});
|
||||
|
||||
const actorWorld = buildWorld([buildGeneral()]);
|
||||
const actorHandler = createTurnDaemonCommandHandler({ world: actorWorld });
|
||||
await expect(actorHandler.handle(command(), { db: buildCommandDb('user-2') })).rejects.toThrow(
|
||||
'actor does not match'
|
||||
);
|
||||
expect(actorWorld.getGeneralById(1)).toMatchObject({ picture: 'old.jpg', imageServer: 0 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import type { TurnDaemonCommand } from '@sammo-ts/common';
|
||||
|
||||
import { normalizeTurnDaemonCommand } from '../src/turn/commandRegistry.js';
|
||||
|
||||
const normalize = (command: Record<string, unknown>) =>
|
||||
normalizeTurnDaemonCommand({
|
||||
requestId: 'icon-request',
|
||||
sentAt: '2026-07-31T09:00:00.000Z',
|
||||
command: command as unknown as TurnDaemonCommand,
|
||||
});
|
||||
|
||||
describe('adjustGeneralIcon command registry', () => {
|
||||
it('accepts only a canonical authenticated icon projection', () => {
|
||||
expect(
|
||||
normalize({
|
||||
type: 'adjustGeneralIcon',
|
||||
userId: 'user-1',
|
||||
picture: 'owner.png',
|
||||
imageServer: 1,
|
||||
iconRevision: '2026-07-31T09:00:00.000Z',
|
||||
})
|
||||
).toEqual({
|
||||
type: 'adjustGeneralIcon',
|
||||
requestId: 'icon-request',
|
||||
userId: 'user-1',
|
||||
picture: 'owner.png',
|
||||
imageServer: 1,
|
||||
iconRevision: '2026-07-31T09:00:00.000Z',
|
||||
});
|
||||
expect(
|
||||
normalize({
|
||||
type: 'adjustGeneralIcon',
|
||||
requestId: 'icon-request',
|
||||
userId: 'user-1',
|
||||
picture: 'owner.png',
|
||||
imageServer: 1,
|
||||
iconRevision: '2026-07-31T09:00:00.000Z',
|
||||
})
|
||||
).toEqual({
|
||||
type: 'adjustGeneralIcon',
|
||||
requestId: 'icon-request',
|
||||
userId: 'user-1',
|
||||
picture: 'owner.png',
|
||||
imageServer: 1,
|
||||
iconRevision: '2026-07-31T09:00:00.000Z',
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
['empty owner', { userId: '' }],
|
||||
['empty picture', { picture: '' }],
|
||||
['negative image server', { imageServer: -1 }],
|
||||
['fractional image server', { imageServer: 1.5 }],
|
||||
['noncanonical revision', { iconRevision: '2026-07-31T09:00:00Z' }],
|
||||
['mismatched durable request ID', { requestId: 'different-request' }],
|
||||
['unknown field', { generalId: 1 }],
|
||||
])('rejects %s', (_label, override) => {
|
||||
expect(
|
||||
normalize({
|
||||
type: 'adjustGeneralIcon',
|
||||
userId: 'user-1',
|
||||
picture: 'owner.png',
|
||||
imageServer: 1,
|
||||
iconRevision: '2026-07-31T09:00:00.000Z',
|
||||
...override,
|
||||
})
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,460 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
import type { TurnDaemonCommand, TurnDaemonCommandResult } from '@sammo-ts/common';
|
||||
import { createGamePostgresConnector, type GamePrisma, type GamePrismaClient } from '@sammo-ts/infra';
|
||||
import type { MapDefinition, ScenarioConfig, ScenarioMeta, TurnSchedule } from '@sammo-ts/logic';
|
||||
|
||||
import { createDatabaseTurnHooks, type DatabaseTurnHooks } from '../src/turn/databaseHooks.js';
|
||||
import { EngineStateManager } from '../src/turn/engineStateManager.js';
|
||||
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
|
||||
import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js';
|
||||
import { createTurnDaemonCommandHandler } from '../src/turn/worldCommandHandler.js';
|
||||
import { loadTurnWorldFromDatabase } from '../src/turn/worldLoader.js';
|
||||
|
||||
const databaseUrl = process.env.IMMEDIATE_ACTION_DATABASE_URL;
|
||||
const integration = describe.skipIf(!databaseUrl);
|
||||
const worldId = 991_741;
|
||||
const targetGeneralId = 991_741;
|
||||
const npcGeneralId = 991_742;
|
||||
const foreignGeneralId = 991_743;
|
||||
const rollbackRequestId = 'integration:engine:account-icon:rollback';
|
||||
const actorMismatchRequestId = 'integration:engine:account-icon:actor-mismatch';
|
||||
const successRequestId = 'integration:engine:account-icon:success';
|
||||
const noGeneralRequestId = 'integration:engine:account-icon:no-general';
|
||||
const rollbackConstraint = 'account_icon_rollback_test';
|
||||
const targetUserId = 'account-icon-target-user';
|
||||
const npcUserId = 'account-icon-npc-user';
|
||||
const foreignUserId = 'account-icon-foreign-user';
|
||||
const initialPicture = 'account-icon-old.jpg';
|
||||
const initialImageServer = 0;
|
||||
const nextPicture = 'account-icon-new.png';
|
||||
const nextImageServer = 1;
|
||||
const rollbackRevision = '2026-07-31T09:05:00.000Z';
|
||||
const successRevision = '2026-07-31T09:10:00.000Z';
|
||||
const schedule: TurnSchedule = { entries: [{ startMinute: 0, tickMinutes: 10 }] };
|
||||
|
||||
const assertDedicatedDatabase = (rawUrl: string): void => {
|
||||
const schema = new URL(rawUrl).searchParams.get('schema');
|
||||
if (!schema?.endsWith('immediate_action_integration')) {
|
||||
throw new Error(`Refusing to mutate non-dedicated schema: ${schema ?? '(missing)'}`);
|
||||
}
|
||||
};
|
||||
|
||||
const scenarioConfig: ScenarioConfig = {
|
||||
stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 50, npcMin: 10, chiefMin: 65 },
|
||||
iconPath: '',
|
||||
map: {},
|
||||
const: {},
|
||||
environment: {
|
||||
mapName: 'che',
|
||||
unitSet: 'che',
|
||||
},
|
||||
};
|
||||
|
||||
const scenarioMeta: ScenarioMeta = {
|
||||
title: '계정 아이콘 영속성 통합',
|
||||
startYear: 190,
|
||||
life: null,
|
||||
fiction: null,
|
||||
history: [],
|
||||
ignoreDefaultEvents: false,
|
||||
};
|
||||
|
||||
const map: MapDefinition = {
|
||||
id: 'account-icon-integration',
|
||||
name: '계정 아이콘 영속성 통합',
|
||||
cities: [],
|
||||
};
|
||||
|
||||
const state: TurnWorldState = {
|
||||
id: worldId,
|
||||
currentYear: 190,
|
||||
currentMonth: 1,
|
||||
tickSeconds: 600,
|
||||
lastTurnTime: new Date('2026-07-31T09:00:00.000Z'),
|
||||
meta: {
|
||||
killturn: 24,
|
||||
lastTurnTime: '2026-07-31T09:00:00.000Z',
|
||||
scenarioMeta,
|
||||
},
|
||||
};
|
||||
|
||||
const buildGeneral = (overrides: Partial<TurnGeneral>): TurnGeneral => ({
|
||||
id: targetGeneralId,
|
||||
userId: targetUserId,
|
||||
name: '아이콘통합장수',
|
||||
nationId: 0,
|
||||
cityId: 0,
|
||||
troopId: 0,
|
||||
stats: { leadership: 50, strength: 50, intelligence: 50 },
|
||||
turnTime: new Date('2026-07-31T09:10:00.000Z'),
|
||||
recentWarTime: null,
|
||||
role: {
|
||||
items: { horse: null, weapon: null, book: null, item: null },
|
||||
personality: null,
|
||||
specialDomestic: null,
|
||||
specialWar: null,
|
||||
},
|
||||
triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} },
|
||||
meta: { killturn: 24, preserved: 'yes' },
|
||||
penalty: {},
|
||||
officerLevel: 0,
|
||||
experience: 0,
|
||||
dedication: 0,
|
||||
injury: 0,
|
||||
gold: 1_000,
|
||||
rice: 1_000,
|
||||
crew: 0,
|
||||
crewTypeId: 0,
|
||||
train: 0,
|
||||
atmos: 0,
|
||||
age: 20,
|
||||
npcState: 0,
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const targetGeneral = buildGeneral({});
|
||||
const npcGeneral = buildGeneral({
|
||||
id: npcGeneralId,
|
||||
userId: npcUserId,
|
||||
name: '빙의NPC',
|
||||
npcState: 1,
|
||||
});
|
||||
const foreignGeneral = buildGeneral({
|
||||
id: foreignGeneralId,
|
||||
userId: foreignUserId,
|
||||
name: '타사용자장수',
|
||||
});
|
||||
const generals = [targetGeneral, npcGeneral, foreignGeneral];
|
||||
|
||||
const buildCommand = (
|
||||
requestId: string,
|
||||
userId: string,
|
||||
iconRevision: string,
|
||||
picture = nextPicture,
|
||||
imageServer = nextImageServer
|
||||
): Extract<TurnDaemonCommand, { type: 'adjustGeneralIcon' }> => ({
|
||||
type: 'adjustGeneralIcon',
|
||||
requestId,
|
||||
userId,
|
||||
picture,
|
||||
imageServer,
|
||||
iconRevision,
|
||||
});
|
||||
|
||||
const toGeneralCreate = (general: TurnGeneral): GamePrisma.GeneralCreateManyInput => ({
|
||||
id: general.id,
|
||||
userId: general.userId,
|
||||
name: general.name,
|
||||
nationId: general.nationId,
|
||||
cityId: general.cityId,
|
||||
troopId: general.troopId,
|
||||
npcState: general.npcState,
|
||||
leadership: general.stats.leadership,
|
||||
strength: general.stats.strength,
|
||||
intel: general.stats.intelligence,
|
||||
officerLevel: general.officerLevel,
|
||||
experience: general.experience,
|
||||
dedication: general.dedication,
|
||||
injury: general.injury,
|
||||
gold: general.gold,
|
||||
rice: general.rice,
|
||||
crew: general.crew,
|
||||
crewTypeId: general.crewTypeId,
|
||||
train: general.train,
|
||||
atmos: general.atmos,
|
||||
turnTime: general.turnTime,
|
||||
recentWarTime: general.recentWarTime,
|
||||
age: general.age,
|
||||
picture: general.picture,
|
||||
imageServer: general.imageServer,
|
||||
meta: general.meta as GamePrisma.InputJsonValue,
|
||||
penalty: general.penalty as GamePrisma.InputJsonValue,
|
||||
});
|
||||
|
||||
integration('adjustGeneralIcon PostgreSQL persistence', () => {
|
||||
let db: GamePrismaClient;
|
||||
let disconnect: (() => Promise<void>) | undefined;
|
||||
let hooks: DatabaseTurnHooks | undefined;
|
||||
|
||||
beforeAll(async () => {
|
||||
assertDedicatedDatabase(databaseUrl!);
|
||||
const connector = createGamePostgresConnector({ url: databaseUrl! });
|
||||
await connector.connect();
|
||||
db = connector.prisma;
|
||||
disconnect = () => connector.disconnect();
|
||||
|
||||
await db.$executeRawUnsafe(`ALTER TABLE input_event DROP CONSTRAINT IF EXISTS ${rollbackConstraint}`);
|
||||
await db.inputEvent.deleteMany({
|
||||
where: {
|
||||
requestId: {
|
||||
in: [rollbackRequestId, actorMismatchRequestId, successRequestId, noGeneralRequestId],
|
||||
},
|
||||
},
|
||||
});
|
||||
await db.rankData.deleteMany({
|
||||
where: { generalId: { in: generals.map((general) => general.id) } },
|
||||
});
|
||||
await db.general.deleteMany({
|
||||
where: { id: { in: generals.map((general) => general.id) } },
|
||||
});
|
||||
await db.worldState.deleteMany({ where: { id: worldId } });
|
||||
|
||||
await db.worldState.create({
|
||||
data: {
|
||||
id: worldId,
|
||||
scenarioCode: 'account-icon-integration',
|
||||
currentYear: state.currentYear,
|
||||
currentMonth: state.currentMonth,
|
||||
tickSeconds: state.tickSeconds,
|
||||
config: JSON.parse(JSON.stringify(scenarioConfig)) as GamePrisma.InputJsonValue,
|
||||
meta: state.meta as GamePrisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
await db.general.createMany({
|
||||
data: generals.map(toGeneralCreate),
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await hooks?.close();
|
||||
if (!db) {
|
||||
await disconnect?.();
|
||||
return;
|
||||
}
|
||||
await db.$executeRawUnsafe(`ALTER TABLE input_event DROP CONSTRAINT IF EXISTS ${rollbackConstraint}`);
|
||||
await db.inputEvent.deleteMany({
|
||||
where: {
|
||||
requestId: {
|
||||
in: [rollbackRequestId, actorMismatchRequestId, successRequestId, noGeneralRequestId],
|
||||
},
|
||||
},
|
||||
});
|
||||
await db.rankData.deleteMany({
|
||||
where: { generalId: { in: generals.map((general) => general.id) } },
|
||||
});
|
||||
await db.general.deleteMany({
|
||||
where: { id: { in: generals.map((general) => general.id) } },
|
||||
});
|
||||
await db.worldState.deleteMany({ where: { id: worldId } });
|
||||
await disconnect?.();
|
||||
});
|
||||
|
||||
it('commits the authenticated human icon with input_event and rolls both back on failure', async () => {
|
||||
const snapshot: TurnWorldSnapshot = {
|
||||
generals,
|
||||
cities: [],
|
||||
nations: [],
|
||||
troops: [],
|
||||
diplomacy: [],
|
||||
events: [],
|
||||
initialEvents: [],
|
||||
scenarioConfig,
|
||||
scenarioMeta,
|
||||
map,
|
||||
};
|
||||
const world = new InMemoryTurnWorld(state, snapshot, { schedule });
|
||||
const handler = createTurnDaemonCommandHandler({ world });
|
||||
hooks = await createDatabaseTurnHooks(databaseUrl!, world);
|
||||
const stateManager = new EngineStateManager();
|
||||
stateManager.register('world', {
|
||||
capture: () => world.captureState(),
|
||||
restore: (captured) => world.restoreState(captured),
|
||||
});
|
||||
const execute = async (
|
||||
command: Extract<TurnDaemonCommand, { type: 'adjustGeneralIcon' }>
|
||||
): Promise<TurnDaemonCommandResult> => {
|
||||
if (!hooks?.hooks.executeCommand || !command.requestId) {
|
||||
throw new Error('Database command execution hook is unavailable.');
|
||||
}
|
||||
return stateManager.transaction(() =>
|
||||
hooks!.hooks.executeCommand!(command.requestId!, async (context) => {
|
||||
const result = await handler.handle(command, context);
|
||||
if (!result) {
|
||||
throw new Error('adjustGeneralIcon command was not handled.');
|
||||
}
|
||||
return result;
|
||||
})
|
||||
);
|
||||
};
|
||||
const createInputEvent = async (
|
||||
command: Extract<TurnDaemonCommand, { type: 'adjustGeneralIcon' }>,
|
||||
actorUserId = command.userId
|
||||
): Promise<void> => {
|
||||
await db.inputEvent.create({
|
||||
data: {
|
||||
requestId: command.requestId!,
|
||||
target: 'ENGINE',
|
||||
eventType: command.type,
|
||||
actorUserId,
|
||||
status: 'PROCESSING',
|
||||
lockedBy: 'account-icon-integration-worker',
|
||||
leaseUntil: new Date('2026-07-31T09:30:00.000Z'),
|
||||
attempts: 1,
|
||||
payload: command as GamePrisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const rollbackCommand = buildCommand(rollbackRequestId, targetUserId, rollbackRevision, 'must-rollback.png', 7);
|
||||
await createInputEvent(rollbackCommand);
|
||||
await db.$executeRawUnsafe(`
|
||||
ALTER TABLE input_event
|
||||
ADD CONSTRAINT ${rollbackConstraint}
|
||||
CHECK (request_id <> '${rollbackRequestId}' OR status <> 'SUCCEEDED')
|
||||
`);
|
||||
await expect(execute(rollbackCommand)).rejects.toThrow(`violates check constraint "${rollbackConstraint}"`);
|
||||
expect(world.getGeneralById(targetGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
meta: { killturn: 24, preserved: 'yes' },
|
||||
});
|
||||
await expect(db.general.findUniqueOrThrow({ where: { id: targetGeneralId } })).resolves.toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
meta: { killturn: 24, preserved: 'yes' },
|
||||
});
|
||||
await expect(
|
||||
db.inputEvent.findUniqueOrThrow({ where: { requestId: rollbackRequestId } })
|
||||
).resolves.toMatchObject({
|
||||
status: 'PROCESSING',
|
||||
result: null,
|
||||
lockedBy: 'account-icon-integration-worker',
|
||||
});
|
||||
await db.$executeRawUnsafe(`ALTER TABLE input_event DROP CONSTRAINT ${rollbackConstraint}`);
|
||||
|
||||
const actorMismatchCommand = buildCommand(
|
||||
actorMismatchRequestId,
|
||||
targetUserId,
|
||||
rollbackRevision,
|
||||
'actor-mismatch.png',
|
||||
8
|
||||
);
|
||||
await createInputEvent(actorMismatchCommand, foreignUserId);
|
||||
await expect(execute(actorMismatchCommand)).rejects.toThrow('actor does not match');
|
||||
expect(world.getGeneralById(targetGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
await expect(db.general.findUniqueOrThrow({ where: { id: targetGeneralId } })).resolves.toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
await expect(
|
||||
db.inputEvent.findUniqueOrThrow({ where: { requestId: actorMismatchRequestId } })
|
||||
).resolves.toMatchObject({
|
||||
status: 'PROCESSING',
|
||||
result: null,
|
||||
});
|
||||
|
||||
const successCommand = buildCommand(successRequestId, targetUserId, successRevision);
|
||||
await createInputEvent(successCommand);
|
||||
await expect(execute(successCommand)).resolves.toEqual({
|
||||
type: 'adjustGeneralIcon',
|
||||
ok: true,
|
||||
generalId: targetGeneralId,
|
||||
updated: true,
|
||||
});
|
||||
expect(world.getGeneralById(targetGeneralId)).toMatchObject({
|
||||
picture: nextPicture,
|
||||
imageServer: nextImageServer,
|
||||
meta: {
|
||||
killturn: 24,
|
||||
preserved: 'yes',
|
||||
accountIconUpdatedAt: successRevision,
|
||||
},
|
||||
});
|
||||
expect(world.getGeneralById(npcGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
expect(world.getGeneralById(foreignGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
await expect(db.general.findUniqueOrThrow({ where: { id: targetGeneralId } })).resolves.toMatchObject({
|
||||
picture: nextPicture,
|
||||
imageServer: nextImageServer,
|
||||
meta: {
|
||||
killturn: 24,
|
||||
preserved: 'yes',
|
||||
accountIconUpdatedAt: successRevision,
|
||||
},
|
||||
});
|
||||
await expect(db.general.findUniqueOrThrow({ where: { id: npcGeneralId } })).resolves.toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
await expect(db.general.findUniqueOrThrow({ where: { id: foreignGeneralId } })).resolves.toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
await expect(
|
||||
db.inputEvent.findUniqueOrThrow({ where: { requestId: successRequestId } })
|
||||
).resolves.toMatchObject({
|
||||
status: 'SUCCEEDED',
|
||||
result: {
|
||||
type: 'adjustGeneralIcon',
|
||||
ok: true,
|
||||
generalId: targetGeneralId,
|
||||
updated: true,
|
||||
},
|
||||
error: null,
|
||||
lockedBy: null,
|
||||
leaseUntil: null,
|
||||
completedAt: expect.any(Date),
|
||||
});
|
||||
|
||||
const noGeneralCommand = buildCommand(noGeneralRequestId, npcUserId, successRevision, 'npc-must-stay.png', 9);
|
||||
await createInputEvent(noGeneralCommand);
|
||||
await expect(execute(noGeneralCommand)).resolves.toEqual({
|
||||
type: 'adjustGeneralIcon',
|
||||
ok: true,
|
||||
generalId: null,
|
||||
updated: false,
|
||||
});
|
||||
expect(world.getGeneralById(npcGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
});
|
||||
await expect(db.general.findUniqueOrThrow({ where: { id: npcGeneralId } })).resolves.toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
meta: { killturn: 24, preserved: 'yes' },
|
||||
});
|
||||
await expect(
|
||||
db.inputEvent.findUniqueOrThrow({ where: { requestId: noGeneralRequestId } })
|
||||
).resolves.toMatchObject({
|
||||
status: 'SUCCEEDED',
|
||||
result: {
|
||||
type: 'adjustGeneralIcon',
|
||||
ok: true,
|
||||
generalId: null,
|
||||
updated: false,
|
||||
},
|
||||
});
|
||||
|
||||
const reloaded = await loadTurnWorldFromDatabase({ databaseUrl: databaseUrl! });
|
||||
expect(reloaded.snapshot.generals.find((entry) => entry.id === targetGeneralId)).toMatchObject({
|
||||
picture: nextPicture,
|
||||
imageServer: nextImageServer,
|
||||
meta: {
|
||||
killturn: 24,
|
||||
preserved: 'yes',
|
||||
accountIconUpdatedAt: successRevision,
|
||||
},
|
||||
});
|
||||
expect(reloaded.snapshot.generals.find((entry) => entry.id === npcGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
npcState: 1,
|
||||
});
|
||||
expect(reloaded.snapshot.generals.find((entry) => entry.id === foreignGeneralId)).toMatchObject({
|
||||
picture: initialPicture,
|
||||
imageServer: initialImageServer,
|
||||
userId: foreignUserId,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -9,6 +9,14 @@ const integration = describe.skipIf(!databaseUrl);
|
||||
const profileName = 'runtime:consumer-integration';
|
||||
const actionId = '924f40ec-e9d2-432f-9867-e9fb3199f14a';
|
||||
|
||||
const assertDedicatedSchema = (): void => {
|
||||
const expected = process.env.GATEWAY_RUNTIME_INTEGRATION_SCHEMA;
|
||||
const actual = databaseUrl ? new URL(databaseUrl).searchParams.get('schema') : null;
|
||||
if (!expected || !expected.endsWith('_gateway_runtime_integration') || actual !== expected) {
|
||||
throw new Error('Refusing to mutate a Gateway database outside the runner-owned integration schema.');
|
||||
}
|
||||
};
|
||||
|
||||
const waitForApplied = async (db: GatewayPrismaClient): Promise<void> => {
|
||||
const deadline = Date.now() + 4_000;
|
||||
while (Date.now() < deadline) {
|
||||
@@ -27,11 +35,14 @@ const waitForApplied = async (db: GatewayPrismaClient): Promise<void> => {
|
||||
integration('gateway runtime action consumer', () => {
|
||||
let db: GatewayPrismaClient;
|
||||
let closeDb: (() => Promise<void>) | undefined;
|
||||
let initialized = false;
|
||||
|
||||
beforeAll(async () => {
|
||||
assertDedicatedSchema();
|
||||
const connector = createGatewayPostgresConnector({ url: databaseUrl! });
|
||||
await connector.connect();
|
||||
db = connector.prisma;
|
||||
initialized = true;
|
||||
closeDb = () => connector.disconnect();
|
||||
await db.gatewayProfile.upsert({
|
||||
where: { profileName },
|
||||
@@ -48,8 +59,10 @@ integration('gateway runtime action consumer', () => {
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await db.gatewayRuntimeAction.deleteMany({ where: { profileName } });
|
||||
await db.gatewayProfile.deleteMany({ where: { profileName } });
|
||||
if (initialized) {
|
||||
await db.gatewayRuntimeAction.deleteMany({ where: { profileName } });
|
||||
await db.gatewayProfile.deleteMany({ where: { profileName } });
|
||||
}
|
||||
await closeDb?.();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user