fix reserved turn bulk and repeat parity
This commit is contained in:
@@ -4,8 +4,13 @@ import type { DatabaseClient, GeneralTurnRow, NationTurnRow } from '../src/conte
|
||||
import {
|
||||
MAX_GENERAL_TURNS,
|
||||
MAX_NATION_TURNS,
|
||||
expandGeneralTurnIndices,
|
||||
repeatGeneralTurns,
|
||||
repeatNationTurns,
|
||||
setGeneralTurn,
|
||||
setGeneralTurns,
|
||||
setNationTurn,
|
||||
setNationTurns,
|
||||
shiftGeneralTurns,
|
||||
shiftNationTurns,
|
||||
ReservedTurnRevisionConflictError,
|
||||
@@ -238,4 +243,99 @@ describe('reservedTurns', () => {
|
||||
expect(pushed.turns[0]?.action).toBe('휴식');
|
||||
expect(pushed.turns[1]?.action).toBe('che_포상');
|
||||
});
|
||||
|
||||
it('expands legacy general sentinel turn lists and applies bulk entries in order', async () => {
|
||||
const { db } = buildDb();
|
||||
|
||||
expect(expandGeneralTurnIndices([-1])).toEqual(
|
||||
Array.from({ length: MAX_GENERAL_TURNS / 2 }, (_, index) => index * 2)
|
||||
);
|
||||
expect(expandGeneralTurnIndices([-2])).toEqual(
|
||||
Array.from({ length: MAX_GENERAL_TURNS / 2 }, (_, index) => index * 2 + 1)
|
||||
);
|
||||
expect(expandGeneralTurnIndices([-3])).toEqual(Array.from({ length: MAX_GENERAL_TURNS }, (_, index) => index));
|
||||
|
||||
const result = await setGeneralTurns(
|
||||
db,
|
||||
3,
|
||||
[
|
||||
{
|
||||
turnIndices: expandGeneralTurnIndices([-1]),
|
||||
action: 'che_훈련',
|
||||
args: {},
|
||||
},
|
||||
{
|
||||
turnIndices: expandGeneralTurnIndices([-2]),
|
||||
action: 'che_사기진작',
|
||||
args: {},
|
||||
},
|
||||
{
|
||||
turnIndices: [29],
|
||||
action: '휴식',
|
||||
args: {},
|
||||
},
|
||||
],
|
||||
0
|
||||
);
|
||||
|
||||
expect(result.turns[0]?.action).toBe('che_훈련');
|
||||
expect(result.turns[1]?.action).toBe('che_사기진작');
|
||||
expect(result.turns[28]?.action).toBe('che_훈련');
|
||||
expect(result.turns[29]?.action).toBe('휴식');
|
||||
});
|
||||
|
||||
it('repeats the leading general pattern at the legacy interval', async () => {
|
||||
const { db } = buildDb();
|
||||
const seeded = await setGeneralTurns(
|
||||
db,
|
||||
4,
|
||||
[
|
||||
{ turnIndices: [0], action: 'che_훈련', args: {} },
|
||||
{ turnIndices: [1], action: 'che_사기진작', args: {} },
|
||||
{ turnIndices: [2], action: 'che_징병', args: { crewTypeId: 1100, amount: 100 } },
|
||||
],
|
||||
0
|
||||
);
|
||||
const repeated = await repeatGeneralTurns(db, 4, 3, seeded.revision);
|
||||
|
||||
expect(repeated.turns.slice(0, 9).map((turn) => turn.action)).toEqual([
|
||||
'che_훈련',
|
||||
'che_사기진작',
|
||||
'che_징병',
|
||||
'che_훈련',
|
||||
'che_사기진작',
|
||||
'che_징병',
|
||||
'che_훈련',
|
||||
'che_사기진작',
|
||||
'che_징병',
|
||||
]);
|
||||
});
|
||||
|
||||
it('supports nation bulk/repeat and preserves the legacy amount-12 no-op', async () => {
|
||||
const { db } = buildDb();
|
||||
const seeded = await setNationTurns(
|
||||
db,
|
||||
5,
|
||||
12,
|
||||
[
|
||||
{ turnIndices: [0, 2], action: 'che_포상', args: { amount: 1 } },
|
||||
{ turnIndices: [1], action: 'che_몰수', args: { amount: 1 } },
|
||||
],
|
||||
0
|
||||
);
|
||||
const repeated = await repeatNationTurns(db, 5, 12, 3, seeded.revision);
|
||||
expect(repeated.turns.slice(0, 6).map((turn) => turn.action)).toEqual([
|
||||
'che_포상',
|
||||
'che_몰수',
|
||||
'che_포상',
|
||||
'che_포상',
|
||||
'che_몰수',
|
||||
'che_포상',
|
||||
]);
|
||||
|
||||
const noOpRepeat = await repeatNationTurns(db, 5, 12, 12, repeated.revision);
|
||||
expect(noOpRepeat).toEqual(repeated);
|
||||
const noOpPush = await shiftNationTurns(db, 5, 12, 12, repeated.revision);
|
||||
expect(noOpPush).toEqual(repeated);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -476,6 +476,113 @@ describe('appRouter', () => {
|
||||
expect(writes).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('applies legacy general sentinel bulk entries and repeats the leading pattern', async () => {
|
||||
const general = buildGeneralRow({ id: 16 });
|
||||
const bulkWrites: unknown[] = [];
|
||||
const bulkCaller = appRouter.createCaller(buildContext({ general, generalTurnWrites: bulkWrites }));
|
||||
|
||||
const bulk = await bulkCaller.turns.reserved.setGeneralBulk({
|
||||
generalId: general.id,
|
||||
entries: [
|
||||
{ turnList: [-1], action: 'che_훈련' },
|
||||
{ turnList: [-2], action: 'che_사기진작' },
|
||||
],
|
||||
expectedRevision: 0,
|
||||
});
|
||||
expect(bulk.turns[0]?.action).toBe('che_훈련');
|
||||
expect(bulk.turns[1]?.action).toBe('che_사기진작');
|
||||
expect(bulk.turns[29]?.action).toBe('che_사기진작');
|
||||
expect(bulkWrites).toHaveLength(1);
|
||||
|
||||
const repeatWrites: unknown[] = [];
|
||||
const repeatCaller = appRouter.createCaller(
|
||||
buildContext({
|
||||
general,
|
||||
generalTurns: [
|
||||
{
|
||||
id: 1,
|
||||
generalId: general.id,
|
||||
turnIdx: 0,
|
||||
actionCode: 'che_훈련',
|
||||
arg: {},
|
||||
createdAt: new Date(),
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
generalId: general.id,
|
||||
turnIdx: 1,
|
||||
actionCode: 'che_사기진작',
|
||||
arg: {},
|
||||
createdAt: new Date(),
|
||||
},
|
||||
],
|
||||
generalTurnWrites: repeatWrites,
|
||||
})
|
||||
);
|
||||
const repeated = await repeatCaller.turns.reserved.repeatGeneral({
|
||||
generalId: general.id,
|
||||
amount: 2,
|
||||
expectedRevision: 0,
|
||||
});
|
||||
expect(repeated.turns.slice(0, 6).map((turn) => turn.action)).toEqual([
|
||||
'che_훈련',
|
||||
'che_사기진작',
|
||||
'che_훈련',
|
||||
'che_사기진작',
|
||||
'che_훈련',
|
||||
'che_사기진작',
|
||||
]);
|
||||
expect(repeatWrites).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('accepts the legacy nation amount-12 no-op without incrementing revision', async () => {
|
||||
const general = buildGeneralRow({ id: 17, nationId: 3, officerLevel: 12 });
|
||||
const nationWrites: unknown[] = [];
|
||||
const caller = appRouter.createCaller(buildContext({ general, nationTurnWrites: nationWrites }));
|
||||
|
||||
const shifted = await caller.turns.reserved.shiftNation({
|
||||
generalId: general.id,
|
||||
amount: 12,
|
||||
expectedRevision: 0,
|
||||
});
|
||||
const repeated = await caller.turns.reserved.repeatNation({
|
||||
generalId: general.id,
|
||||
amount: 12,
|
||||
expectedRevision: 0,
|
||||
});
|
||||
|
||||
expect(shifted.revision).toBe(0);
|
||||
expect(repeated.revision).toBe(0);
|
||||
expect(nationWrites).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('applies nation bulk entries sequentially so later entries overwrite overlaps', async () => {
|
||||
const general = buildGeneralRow({ id: 18, nationId: 3, officerLevel: 12 });
|
||||
const nationWrites: unknown[] = [];
|
||||
const caller = appRouter.createCaller(buildContext({ general, nationTurnWrites: nationWrites }));
|
||||
|
||||
const response = await caller.turns.reserved.setNationBulk({
|
||||
generalId: general.id,
|
||||
entries: [
|
||||
{
|
||||
turnList: [0, 2],
|
||||
action: 'che_포상',
|
||||
args: { isGold: true, amount: 1, destGeneralId: 7 },
|
||||
},
|
||||
{
|
||||
turnList: [2],
|
||||
action: 'che_포상',
|
||||
args: { isGold: false, amount: 2, destGeneralId: 8 },
|
||||
},
|
||||
],
|
||||
expectedRevision: 0,
|
||||
});
|
||||
|
||||
expect(response.turns[0]?.args).toEqual({ isGold: true, amount: 1, destGeneralId: 7 });
|
||||
expect(response.turns[2]?.args).toEqual({ isGold: false, amount: 2, destGeneralId: 8 });
|
||||
expect(nationWrites).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('rejects malformed and cross-scope arguments without writing turns', async () => {
|
||||
const general = buildGeneralRow({ id: 14, nationId: 3, officerLevel: 5 });
|
||||
const generalWrites: unknown[] = [];
|
||||
|
||||
Reference in New Issue
Block a user