diff --git a/static/exporter.js b/static/exporter.js index 23535d8..06dd2e6 100644 --- a/static/exporter.js +++ b/static/exporter.js @@ -112,11 +112,24 @@ function _instrCharacterLines(instr) { }); } +// RFC §4.4.1: DATE = YYYY-MM-DD HH:MM:SS +// notChangedSince from AST log is a float epoch; score YAML must have ISO date. + +function _epochToISO(val) { + if (!val) return null; + if (typeof val === 'string') return val; + return new Date(val * 1000).toISOString().slice(0, 19).replace('T', ' '); +} + +function _nowISO() { + return new Date().toISOString().slice(0, 19).replace('T', ' '); +} + // RFC §4.4: embedded instrument key is "instrument NAME:" not "instrument: 'NAME'" export function exportInstrument(instr) { const lines = [`instrument ${instr.name}:`]; - if (instr.notChangedSince) lines.push(` NOT_CHANGED_SINCE: ${instr.notChangedSince}`); + lines.push(` NOT_CHANGED_SINCE: ${_nowISO()}`); lines.push(` character:`); lines.push(..._instrCharacterLines(instr)); return lines.join('\n'); @@ -210,6 +223,13 @@ function _patchInstrumentHeader(text, instruments) { } else if (instr.isLinked && !instr._modified) { result.push(line); i++; + while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) { + if (/^\s+NOT_CHANGED_SINCE\s*:/.test(lines[i])) + result.push(` NOT_CHANGED_SINCE: ${_epochToISO(instr.notChangedSince)}`); + else + result.push(lines[i]); + i++; + } } else { i++; while (i < lines.length && (lines[i].startsWith(' ') || lines[i] === '')) i++; diff --git a/test-parser.mjs b/test-parser.mjs index 406beff..5b5b119 100644 --- a/test-parser.mjs +++ b/test-parser.mjs @@ -542,6 +542,23 @@ ok('new bar has _meta with BPM', newBarPatched.includes('beats_per_minute: 120') ok('new bar appended as extra document', (newBarPatched.match(/\n---\n/g) ?? []).length === 3); ok('new bar logged as changed', newBarLog.some(e => e.level === 'changed' && e.path === 'bar / 001P1L1M3')); +// ── NOT_CHANGED_SINCE epoch → ISO conversion ────────────────────────────── +section('NOT_CHANGED_SINCE: epoch converted on passthrough, now on re-serialize'); +const ncsScore = `instrument linked/pi:\n NOT_CHANGED_SINCE: 1710000000.0\n character:\n A: "1:0,10;1,0"\n\ninstrument embedded:\n NOT_CHANGED_SINCE: 1710000000.0\n character:\n A: "1:0,5;1,0"\n`; +const ncsList = [ + { name: 'linked/pi', isLinked: true, _modified: false, notChangedSince: 1710000000.0, + variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] }, + { name: 'embedded', isLinked: false, _modified: true, notChangedSince: 1710000000.0, + variations: [], basicProperties: null, volumes: null, timbre: null, fmModulations: [], amModulations: [] }, +]; +const { text: ncsPatched } = patchScore(ncsScore, ncsList); +const ISO_RX = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/; +const ncsLines = ncsPatched.split('\n'); +const linkedNCS = ncsLines.find(l => /NOT_CHANGED_SINCE/.test(l) && ncsLines.indexOf(l) < ncsLines.findIndex(l2 => /instrument embedded/.test(l2))); +const embeddedNCS = ncsLines.find((l, i) => /NOT_CHANGED_SINCE/.test(l) && i > ncsLines.findIndex(l2 => /instrument embedded/.test(l2))); +ok('linked passthrough: epoch converted to ISO date', linkedNCS && ISO_RX.test(linkedNCS) && !linkedNCS.includes('1710000000')); +ok('modified instrument: NOT_CHANGED_SINCE set to now', embeddedNCS && ISO_RX.test(embeddedNCS) && !embeddedNCS.includes('1710000000')); + // ── patchScore deleted instrument ───────────────────────────────────────── section('patchScore deleted instrument'); const delInstrScore = `instrument alpha:\n character:\n A: "1:0,10;1,0"\n\ninstrument ki:\n character:\n A: "1:0,5;1,0"\n`;