@animastor/orchestration 0.3.5 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +70 -0
- package/README.md +31 -0
- package/package.json +11 -8
- package/src/gen/accept.js +370 -0
- package/src/gen/claim.js +155 -0
- package/src/gen/commands.js +338 -0
- package/src/gen/index.js +107 -0
- package/src/gen/local-runner.js +80 -0
- package/src/gen/ports.js +163 -0
- package/src/gen/progress.js +99 -0
- package/src/gen/sql/001-gen-core.js +130 -0
- package/src/gen/sql/002-command-jobs.js +58 -0
- package/src/gen/sql/003-scope-scenes.js +38 -0
- package/src/gen/sql/index.js +35 -0
- package/src/gen/sweeper.js +141 -0
- package/src/gen/util.js +136 -0
- package/src/gen/validate-media.js +181 -0
- package/src/index.js +15 -91
- package/src/legacy/index.js +100 -0
- package/src/{runtime → legacy/runtime}/command-gate.js +11 -2
- package/src/{runtime → legacy/runtime}/persistence-port.js +21 -0
- package/src/{runtime → legacy/runtime}/reconciliation-engine.js +15 -0
- package/src/{runtime → legacy/runtime}/runtime-scheduler.js +9 -0
- package/src/{runtime → legacy/runtime}/scene-window.js +4 -0
- package/test/artifact-root-binding.test.js +0 -174
- package/test/capacity-deferral.test.js +0 -265
- package/test/counter-reconciliation.test.js +0 -164
- package/test/mocks/redis-mock.js +0 -231
- package/test/reconcile-quota-leak.test.js +0 -366
- package/test/stage146-d1-artifact-naming.test.js +0 -103
- package/test/stage147-a1-null-build-gate.test.js +0 -211
- package/test/test-bindings.js +0 -60
- /package/src/{host → legacy/host}/host-bindings.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/index.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/orchestrator.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/runtime-result-consumer.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/scene-callbacks.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/scene-orchestrator.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/scene-restoration.js +0 -0
- /package/src/{orchestration → legacy/orchestration}/scene-utils.js +0 -0
- /package/src/{runtime → legacy/runtime}/active-scenes-index.js +0 -0
- /package/src/{runtime → legacy/runtime}/audio-fsm-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/circuit-breaker.js +0 -0
- /package/src/{runtime → legacy/runtime}/counter-reconciliation.js +0 -0
- /package/src/{runtime → legacy/runtime}/dispatch-engine.js +0 -0
- /package/src/{runtime → legacy/runtime}/dispatch-outcomes.js +0 -0
- /package/src/{runtime → legacy/runtime}/failure-taxonomy.js +0 -0
- /package/src/{runtime → legacy/runtime}/hub-cancel-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/layer-config-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/lease-manager.js +0 -0
- /package/src/{runtime → legacy/runtime}/placeholder-audio-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/progress-events-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/retry-budget-manager.js +0 -0
- /package/src/{runtime → legacy/runtime}/runtime-metrics.js +0 -0
- /package/src/{runtime → legacy/runtime}/runtime-result-emitter.js +0 -0
- /package/src/{runtime → legacy/runtime}/scene-data-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/video-fsm-port.js +0 -0
- /package/src/{runtime → legacy/runtime}/worker-health.js +0 -0
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
// ======================================================
|
|
2
|
-
// Counter Reconciliation Tests (B10 verification)
|
|
3
|
-
// ======================================================
|
|
4
|
-
|
|
5
|
-
const { expect } = require('chai');
|
|
6
|
-
const { createMockRedis } = require('./mocks/redis-mock');
|
|
7
|
-
const counterReconciliation = require('../src/runtime/counter-reconciliation');
|
|
8
|
-
|
|
9
|
-
describe('Counter Reconciliation', () => {
|
|
10
|
-
let redis;
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
redis = createMockRedis();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
// ── correctCounterWithLua ─────────────────────────
|
|
17
|
-
describe('correctCounterWithLua (B10 regression)', () => {
|
|
18
|
-
it('corrects a drifted counter value', async () => {
|
|
19
|
-
// Simulate drift: counter says 5, but only 3 leases exist
|
|
20
|
-
await redis.set('animastor:runtime:active-audio', '5');
|
|
21
|
-
|
|
22
|
-
const result = await counterReconciliation.correctCounterWithLua(redis, 'audio', 3);
|
|
23
|
-
expect(result.success).to.equal(true);
|
|
24
|
-
expect(result.action).to.equal('corrected');
|
|
25
|
-
expect(result.old).to.equal('5');
|
|
26
|
-
expect(result.new).to.equal('3');
|
|
27
|
-
|
|
28
|
-
// Verify the counter was actually corrected
|
|
29
|
-
const val = await redis.get('animastor:runtime:active-audio');
|
|
30
|
-
expect(val).to.equal('3');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('corrects when counter is zero but leases exist', async () => {
|
|
34
|
-
// Drift: counter is 0, but lease scan finds 2
|
|
35
|
-
// Actually the correct behavior is: counter is too LOW
|
|
36
|
-
await redis.set('animastor:runtime:active-image', '0');
|
|
37
|
-
|
|
38
|
-
const result = await counterReconciliation.correctCounterWithLua(redis, 'image', 2);
|
|
39
|
-
expect(result.success).to.equal(true);
|
|
40
|
-
expect(result.old).to.equal('0');
|
|
41
|
-
expect(result.new).to.equal('2');
|
|
42
|
-
|
|
43
|
-
const val = await redis.get('animastor:runtime:active-image');
|
|
44
|
-
expect(val).to.equal('2');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('corrects when counter key does not exist', async () => {
|
|
48
|
-
const result = await counterReconciliation.correctCounterWithLua(redis, 'video', 1);
|
|
49
|
-
expect(result.success).to.equal(true);
|
|
50
|
-
expect(result.old).to.equal(null);
|
|
51
|
-
|
|
52
|
-
const val = await redis.get('animastor:runtime:active-video');
|
|
53
|
-
expect(val).to.equal('1');
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('B10 bug: ioredis null → "" no longer causes silent no-op', async () => {
|
|
57
|
-
// This test verifies that the fix for B10 works:
|
|
58
|
-
// Previously, correctCounterWithLua passed `null` as an "expected" guard arg,
|
|
59
|
-
// which ioredis serialized to "" (truthy in Lua), causing the guard to
|
|
60
|
-
// always fire and skip the SET. The guard was removed.
|
|
61
|
-
await redis.set('animastor:runtime:active-audio', '10');
|
|
62
|
-
|
|
63
|
-
const result = await counterReconciliation.correctCounterWithLua(redis, 'audio', 7);
|
|
64
|
-
expect(result.success).to.equal(true);
|
|
65
|
-
expect(result.old).to.equal('10');
|
|
66
|
-
expect(result.new).to.equal('7');
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// ── getCounterWithDriftCheck ──────────────────────
|
|
71
|
-
describe('getCounterWithDriftCheck', () => {
|
|
72
|
-
it('reports no drift when counter matches lease count', async () => {
|
|
73
|
-
const result = await counterReconciliation.getCounterWithDriftCheck(redis, 'audio');
|
|
74
|
-
expect(result.stage).to.equal('audio');
|
|
75
|
-
expect(result.leaseCount).to.equal(0);
|
|
76
|
-
expect(result.counterValue).to.equal(0);
|
|
77
|
-
expect(result.drift).to.equal(0);
|
|
78
|
-
expect(result.correct).to.equal(true);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('detects positive drift (counter > actual leases)', async () => {
|
|
82
|
-
// Set counter higher with no leases
|
|
83
|
-
await redis.set('animastor:runtime:active-audio', '3');
|
|
84
|
-
|
|
85
|
-
const result = await counterReconciliation.getCounterWithDriftCheck(redis, 'audio');
|
|
86
|
-
expect(result.drift).to.equal(3);
|
|
87
|
-
expect(result.correct).to.equal(false);
|
|
88
|
-
expect(result.counterValue).to.equal(3);
|
|
89
|
-
expect(result.leaseCount).to.equal(0);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// ── reconcileCounters ─────────────────────────────
|
|
94
|
-
describe('reconcileCounters', () => {
|
|
95
|
-
it('reports no corrections when counters are balanced', async () => {
|
|
96
|
-
const report = await counterReconciliation.reconcileCounters(redis);
|
|
97
|
-
|
|
98
|
-
expect(report.summary.correctedCount).to.equal(0);
|
|
99
|
-
expect(report.summary.totalDrift).to.equal(0);
|
|
100
|
-
for (const stage of ['audio', 'image', 'video']) {
|
|
101
|
-
expect(report.stages[stage].corrected).to.equal(false);
|
|
102
|
-
expect(report.stages[stage].drift).to.equal(0);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('corrects drifted counters across all stages', async () => {
|
|
107
|
-
// Create drift: audio=2, image=3, video=1
|
|
108
|
-
await redis.set('animastor:runtime:active-audio', '2');
|
|
109
|
-
await redis.set('animastor:runtime:active-image', '3');
|
|
110
|
-
await redis.set('animastor:runtime:active-video', '1');
|
|
111
|
-
|
|
112
|
-
const report = await counterReconciliation.reconcileCounters(redis);
|
|
113
|
-
|
|
114
|
-
// All 3 should be corrected to 0 (no leases in mock)
|
|
115
|
-
expect(report.summary.correctedCount).to.equal(3);
|
|
116
|
-
|
|
117
|
-
// Verify counters are corrected
|
|
118
|
-
expect(await redis.get('animastor:runtime:active-audio')).to.equal('0');
|
|
119
|
-
expect(await redis.get('animastor:runtime:active-image')).to.equal('0');
|
|
120
|
-
expect(await redis.get('animastor:runtime:active-video')).to.equal('0');
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('corrects counter even with non-numeric initial value', async () => {
|
|
124
|
-
await redis.set('animastor:runtime:active-audio', 'not-a-number');
|
|
125
|
-
|
|
126
|
-
const report = await counterReconciliation.reconcileCounters(redis);
|
|
127
|
-
// Counter should be corrected to 0 (no leases in mock)
|
|
128
|
-
expect(report.stages.audio.corrected).to.equal(true);
|
|
129
|
-
expect(await redis.get('animastor:runtime:active-audio')).to.equal('0');
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// ── countActiveLeasesByStage ──────────────────────
|
|
134
|
-
describe('countActiveLeasesByStage', () => {
|
|
135
|
-
it('returns 0 when no leases exist', async () => {
|
|
136
|
-
const count = await counterReconciliation.countActiveLeasesByStage(redis, 'audio');
|
|
137
|
-
expect(count).to.equal(0);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
// ── getCurrentCounter ────────────────────────────
|
|
142
|
-
describe('getCurrentCounter', () => {
|
|
143
|
-
it('returns 0 for missing key', async () => {
|
|
144
|
-
const val = await counterReconciliation.getCurrentCounter(redis, 'audio');
|
|
145
|
-
expect(val).to.equal(0);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('returns stored integer value', async () => {
|
|
149
|
-
await redis.set('animastor:runtime:active-audio', '42');
|
|
150
|
-
const val = await counterReconciliation.getCurrentCounter(redis, 'audio');
|
|
151
|
-
expect(val).to.equal(42);
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// ── manualCounterCorrection ───────────────────────
|
|
156
|
-
describe('manualCounterCorrection', () => {
|
|
157
|
-
it('sets counter to exact value', async () => {
|
|
158
|
-
await redis.set('animastor:runtime:active-audio', '99');
|
|
159
|
-
const result = await counterReconciliation.manualCounterCorrection(redis, 'audio', 5);
|
|
160
|
-
expect(result.success).to.equal(true);
|
|
161
|
-
expect(await redis.get('animastor:runtime:active-audio')).to.equal('5');
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
package/test/mocks/redis-mock.js
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
// In-memory Redis mock for tests.
|
|
2
|
-
// Operations are internally consistent: hset/hget/hgetall use flat `key:field` storage,
|
|
3
|
-
// scan filters the store by glob pattern, lrange handles stop=-1.
|
|
4
|
-
|
|
5
|
-
function globToRegex(pattern) {
|
|
6
|
-
let regexStr = '^';
|
|
7
|
-
for (const ch of pattern) {
|
|
8
|
-
if (ch === '*') regexStr += '.*';
|
|
9
|
-
else if (ch === '?') regexStr += '.';
|
|
10
|
-
else if ('[](){}+.^$|\\'.includes(ch)) regexStr += '\\' + ch;
|
|
11
|
-
else regexStr += ch;
|
|
12
|
-
}
|
|
13
|
-
regexStr += '$';
|
|
14
|
-
return new RegExp(regexStr);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function createMockRedis() {
|
|
18
|
-
const store = new Map();
|
|
19
|
-
// Recorded TTLs (seconds) so ttl() can report them. Keys are NOT
|
|
20
|
-
// auto-expired — existing tests rely on keys persisting. Tests simulate
|
|
21
|
-
// renewal / decay by calling set(...,'EX',n) or expire(key, n).
|
|
22
|
-
const expiries = new Map();
|
|
23
|
-
const recordExpiry = (key, args) => {
|
|
24
|
-
const i = args.indexOf('EX');
|
|
25
|
-
if (i !== -1 && args[i + 1] !== undefined) expiries.set(key, Number(args[i + 1]));
|
|
26
|
-
};
|
|
27
|
-
return {
|
|
28
|
-
hset: async (key, field, value) => {
|
|
29
|
-
if (field && typeof field === 'object' && !Array.isArray(field)) {
|
|
30
|
-
for (const [name, fieldValue] of Object.entries(field)) {
|
|
31
|
-
store.set(`${key}:${name}`, fieldValue);
|
|
32
|
-
}
|
|
33
|
-
return Object.keys(field).length;
|
|
34
|
-
}
|
|
35
|
-
store.set(`${key}:${field}`, value);
|
|
36
|
-
return 1;
|
|
37
|
-
},
|
|
38
|
-
hget: async (key, field) => store.get(`${key}:${field}`) || null,
|
|
39
|
-
hgetall: async (key) => {
|
|
40
|
-
const result = {};
|
|
41
|
-
const prefix = key + ':';
|
|
42
|
-
for (const [k, v] of store) {
|
|
43
|
-
if (k.startsWith(prefix)) {
|
|
44
|
-
result[k.slice(prefix.length)] = v;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return Object.keys(result).length > 0 ? result : null;
|
|
48
|
-
},
|
|
49
|
-
hdel: async (key, ...fields) => {
|
|
50
|
-
let n = 0;
|
|
51
|
-
for (const field of fields) {
|
|
52
|
-
if (store.delete(`${key}:${field}`)) n++;
|
|
53
|
-
}
|
|
54
|
-
return n;
|
|
55
|
-
},
|
|
56
|
-
hlen: async (key) => {
|
|
57
|
-
const prefix = key + ':';
|
|
58
|
-
let n = 0;
|
|
59
|
-
for (const k of store.keys()) if (k.startsWith(prefix)) n++;
|
|
60
|
-
return n;
|
|
61
|
-
},
|
|
62
|
-
hexists: async (key, field) => store.has(`${key}:${field}`) ? 1 : 0,
|
|
63
|
-
get: async (key) => store.get(key) || null,
|
|
64
|
-
set: async (key, value, ...args) => {
|
|
65
|
-
const nx = args.includes('NX');
|
|
66
|
-
if (nx && store.has(key)) return null;
|
|
67
|
-
store.set(key, value);
|
|
68
|
-
recordExpiry(key, args);
|
|
69
|
-
return 'OK';
|
|
70
|
-
},
|
|
71
|
-
del: async (...keys) => { let n = 0; for (const k of keys) { expiries.delete(k); if (store.delete(k)) n++; } return n; },
|
|
72
|
-
expire: async (key, ttl) => {
|
|
73
|
-
if (!store.has(key)) return 0;
|
|
74
|
-
expiries.set(key, Number(ttl));
|
|
75
|
-
return 1;
|
|
76
|
-
},
|
|
77
|
-
// Mirrors ioredis: -2 if key missing, -1 if no expiry, else seconds.
|
|
78
|
-
ttl: async (key) => {
|
|
79
|
-
if (!store.has(key)) return -2;
|
|
80
|
-
return expiries.has(key) ? expiries.get(key) : -1;
|
|
81
|
-
},
|
|
82
|
-
incr: async (key) => { const v = (parseInt(store.get(key) || '0', 10)) + 1; store.set(key, String(v)); return v; },
|
|
83
|
-
incrby: async (key, increment) => { const v = (parseInt(store.get(key) || '0', 10)) + Number(increment || 0); store.set(key, String(v)); return v; },
|
|
84
|
-
decr: async (key) => { const v = Math.max(0, (parseInt(store.get(key) || '0', 10)) - 1); store.set(key, String(v)); return v; },
|
|
85
|
-
exists: async (key) => store.has(key) ? 1 : 0,
|
|
86
|
-
scan: async (cursor, ...args) => {
|
|
87
|
-
let pattern = null;
|
|
88
|
-
for (let i = 0; i < args.length; i++) {
|
|
89
|
-
if (args[i] === 'MATCH') pattern = args[i + 1];
|
|
90
|
-
}
|
|
91
|
-
if (!pattern) return ['0', []];
|
|
92
|
-
const regex = globToRegex(pattern);
|
|
93
|
-
const matched = [...store.keys()].filter(k => regex.test(k));
|
|
94
|
-
return ['0', matched];
|
|
95
|
-
},
|
|
96
|
-
hscan: async (key, cursor, ...args) => {
|
|
97
|
-
const prefix = key + ':';
|
|
98
|
-
const entries = [];
|
|
99
|
-
for (const [k, v] of store) {
|
|
100
|
-
if (k.startsWith(prefix)) {
|
|
101
|
-
entries.push(k.slice(prefix.length), v);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return ['0', entries];
|
|
105
|
-
},
|
|
106
|
-
eval: async (script, keysCount, ...args) => {
|
|
107
|
-
// reconcile-quota-leak: dispatch-engine.markDispatchJobsEnqueued
|
|
108
|
-
// compare-and-set (KEYS=[metaKey], ARGV=[expectedRaw, newRaw]).
|
|
109
|
-
if (script.includes('KEEPTTL') && script.includes('raw ~= ARGV[1]')) {
|
|
110
|
-
const key = args[0];
|
|
111
|
-
const [expected, next] = args.slice(keysCount);
|
|
112
|
-
const raw = store.get(key);
|
|
113
|
-
if (raw === undefined || raw !== expected) return 0;
|
|
114
|
-
store.set(key, next);
|
|
115
|
-
return 1;
|
|
116
|
-
}
|
|
117
|
-
if (script.includes('local metadata') && script.includes("ARGV[5] == '1'")) {
|
|
118
|
-
const [metadataKey, completedKey, leaseKey, quotaKey] = args.slice(0, keysCount);
|
|
119
|
-
const [
|
|
120
|
-
expectedMetadata,
|
|
121
|
-
markerValue,
|
|
122
|
-
markerTtl,
|
|
123
|
-
expectedLease,
|
|
124
|
-
quotaOwned,
|
|
125
|
-
] = args.slice(keysCount);
|
|
126
|
-
const metadata = store.get(metadataKey);
|
|
127
|
-
if (metadata === undefined) return store.has(completedKey) ? 2 : 0;
|
|
128
|
-
if (metadata !== expectedMetadata) return -1;
|
|
129
|
-
const lease = store.get(leaseKey);
|
|
130
|
-
if (lease !== undefined && (!expectedLease || lease !== expectedLease)) return -2;
|
|
131
|
-
if (store.has(completedKey)) return 2;
|
|
132
|
-
|
|
133
|
-
store.set(completedKey, markerValue);
|
|
134
|
-
store.delete(metadataKey);
|
|
135
|
-
expiries.delete(metadataKey);
|
|
136
|
-
if (lease !== undefined && expectedLease) {
|
|
137
|
-
store.delete(leaseKey);
|
|
138
|
-
expiries.delete(leaseKey);
|
|
139
|
-
}
|
|
140
|
-
if (quotaOwned === '1') {
|
|
141
|
-
const current = Number(store.get(quotaKey) || 0);
|
|
142
|
-
if (current > 0) store.set(quotaKey, String(current - 1));
|
|
143
|
-
}
|
|
144
|
-
void markerTtl;
|
|
145
|
-
return 1;
|
|
146
|
-
}
|
|
147
|
-
// Lease renewal (lease-manager.renewLeaseIfOwner): compare-and-EXPIRE.
|
|
148
|
-
// KEYS=[leaseKey, metadataKey], ARGV=[expectedToken, ttl, now].
|
|
149
|
-
if (script.includes('ttl_add') && script.includes('EXPIRE')) {
|
|
150
|
-
const leaseKey = args[0];
|
|
151
|
-
const metadataKey = args[1];
|
|
152
|
-
const expected = args[keysCount];
|
|
153
|
-
const ttlAdd = Number(args[keysCount + 1]);
|
|
154
|
-
const now = args[keysCount + 2];
|
|
155
|
-
const current = store.get(leaseKey);
|
|
156
|
-
if (current === undefined) return ['false', 'no_lease'];
|
|
157
|
-
if (current !== expected) return ['false', 'token_mismatch', current];
|
|
158
|
-
expiries.set(leaseKey, ttlAdd);
|
|
159
|
-
if (store.has(metadataKey)) expiries.set(metadataKey, ttlAdd);
|
|
160
|
-
return ['true', 'renewed', String(now)];
|
|
161
|
-
}
|
|
162
|
-
if (script.includes("current ~= ARGV[1]") && script.includes("redis.call('DEL', KEYS[1])")) {
|
|
163
|
-
const key = args[0];
|
|
164
|
-
const expected = args[keysCount];
|
|
165
|
-
const current = store.get(key);
|
|
166
|
-
if (current === undefined) return 0;
|
|
167
|
-
if (current !== expected) return -1;
|
|
168
|
-
store.delete(key);
|
|
169
|
-
expiries.delete(key);
|
|
170
|
-
return 1;
|
|
171
|
-
}
|
|
172
|
-
if (script.includes("return redis.call('INCR', key)")) {
|
|
173
|
-
const key = args[0];
|
|
174
|
-
const max = Number(args[keysCount]);
|
|
175
|
-
const current = Number(store.get(key) || 0);
|
|
176
|
-
if (current >= max) return 0;
|
|
177
|
-
const next = current + 1;
|
|
178
|
-
store.set(key, String(next));
|
|
179
|
-
return next;
|
|
180
|
-
}
|
|
181
|
-
// Return array mimicking Lua return: {true, 'corrected', old, new}
|
|
182
|
-
// Callers (e.g. correctCounterWithLua) destructure result[0]..result[3]
|
|
183
|
-
const key = args[0];
|
|
184
|
-
const target = args[keysCount] || args[1];
|
|
185
|
-
let current = store.get(key);
|
|
186
|
-
if (current === undefined) current = null;
|
|
187
|
-
store.set(key, target);
|
|
188
|
-
return ['true', 'corrected', current, target];
|
|
189
|
-
},
|
|
190
|
-
rpush: async (key, ...values) => { const arr = store.get(key) || []; arr.push(...values); store.set(key, arr); return arr.length; },
|
|
191
|
-
lpush: async (key, ...values) => { const arr = store.get(key) || []; arr.unshift(...values); store.set(key, arr); return arr.length; },
|
|
192
|
-
rpoplpush: async (source, destination) => {
|
|
193
|
-
const arr = store.get(source);
|
|
194
|
-
if (!arr || arr.length === 0) return null;
|
|
195
|
-
const value = arr.pop();
|
|
196
|
-
if (arr.length === 0) store.delete(source);
|
|
197
|
-
const dest = store.get(destination) || [];
|
|
198
|
-
dest.unshift(value);
|
|
199
|
-
store.set(destination, dest);
|
|
200
|
-
return value;
|
|
201
|
-
},
|
|
202
|
-
llen: async (key) => { const arr = store.get(key); return arr ? arr.length : 0; },
|
|
203
|
-
lrange: async (key, start, stop) => {
|
|
204
|
-
const arr = store.get(key) || [];
|
|
205
|
-
if (stop === -1 || stop === undefined) return arr.slice(start);
|
|
206
|
-
return arr.slice(start, stop + 1);
|
|
207
|
-
},
|
|
208
|
-
lrem: async (key, count, value) => {
|
|
209
|
-
const arr = store.get(key) || [];
|
|
210
|
-
const index = arr.indexOf(value);
|
|
211
|
-
if (index === -1) return 0;
|
|
212
|
-
arr.splice(index, 1);
|
|
213
|
-
return 1;
|
|
214
|
-
},
|
|
215
|
-
sadd: async (key, member) => { const s = store.get(key) || new Set(); s.add(member); store.set(key, s); return 1; },
|
|
216
|
-
srem: async (key, member) => { const s = store.get(key); if (s) { s.delete(member); return 1; } return 0; },
|
|
217
|
-
smembers: async (key) => { const s = store.get(key); return s ? [...s] : []; },
|
|
218
|
-
scard: async (key) => { const s = store.get(key); return s ? s.size : 0; },
|
|
219
|
-
zrem: async (key, member) => {
|
|
220
|
-
const s = store.get(key);
|
|
221
|
-
if (s && s instanceof Set) { return s.delete(member) ? 1 : 0; }
|
|
222
|
-
return 0;
|
|
223
|
-
},
|
|
224
|
-
multi: () => ({ exec: async () => [] }),
|
|
225
|
-
quit: async () => 'OK',
|
|
226
|
-
disconnect: () => {},
|
|
227
|
-
duplicate: () => createMockRedis(),
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
module.exports = { createMockRedis };
|