Take serial numbers from a pool the whole entry shares

Each station worked out its next number as the highest in its log plus one,
so two operators typing at the same moment both sent the same number and
one of the two contacts was wrong in the sponsor's log.

SerialNumberPool holds the highest number taken per band, which is N1MM's
QSONumbers array. A number is taken at the first character of the callsign,
as N1MM takes it, and ReserveNr tells the other stations before the contact
is worked. A number at or below one this station has used is answered with
the whole pool, N1MM's QSONRS, and the station that is behind counts on from
there. The pool also goes out when a contest is opened and when the numbers
are started again.

Contest.NumbersByBand says whether the numbers run per band or as one
sequence for the station. CQ WPX runs one per band for a multi-two and a
multi-multi; every other contest here runs one sequence. The band index in
the message is N1MM's, so the array lines up with theirs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015NZkFxHde1iifAQemFTHxf
This commit is contained in:
2026-09-10 11:56:49 +00:00
co-authored by Claude Opus 5
parent 33c7631e17
commit 7ba0b44fc1
21 changed files with 677 additions and 40 deletions
@@ -0,0 +1,137 @@
using Nonemm.Core;
namespace Nonemm.Session.Tests;
public class SerialNumberPoolTests
{
private static Qso Worked(Band band, int number) => new()
{
Id = Qso.NewId(),
TimestampUtc = new DateTime(2026, 5, 30, 12, 0, 0, DateTimeKind.Utc),
ContestName = "CQWPX",
Call = Callsign.Parse("JA1XYZ"),
Frequency = band.Low,
Mode = Modes.Cw,
SentNumber = number,
};
[Fact]
public void TheFirstNumberIsOne() =>
Assert.Equal(1, new SerialNumberPool().Next(Bands.Band20M));
[Fact]
public void TakingANumberMovesTheNextOneOn()
{
SerialNumberPool pool = new();
Assert.Equal(1, pool.Take(Bands.Band20M));
Assert.Equal(2, pool.Next(Bands.Band20M));
}
/// One sequence for the whole station: a number taken on one band is not
/// sent again on another.
[Fact]
public void OneSequenceCountsAcrossTheBands()
{
SerialNumberPool pool = new();
pool.Take(Bands.Band20M);
pool.Take(Bands.Band20M);
Assert.Equal(3, pool.Next(Bands.Band40M));
}
/// CQ WPX multi-two and multi-multi count per band, so each band starts
/// again at one.
[Fact]
public void ByBandEachBandHasItsOwnSequence()
{
SerialNumberPool pool = new() { ByBand = true };
pool.Take(Bands.Band20M);
pool.Take(Bands.Band20M);
Assert.Equal(1, pool.Next(Bands.Band40M));
Assert.Equal(3, pool.Next(Bands.Band20M));
}
/// The point of the pool: the other station says which number it has taken
/// before it works the contact, so this one counts past it.
[Fact]
public void ANumberAnotherStationTookIsNotSentAgain()
{
SerialNumberPool pool = new();
pool.Take(Bands.Band20M);
Assert.True(pool.Note(2, Bands.Band40M));
Assert.Equal(3, pool.Next(Bands.Band20M));
}
/// A station that has fallen behind sends a number this one has used. The
/// clash is reported so the caller answers with the whole pool.
[Fact]
public void ANumberAlreadyUsedIsRefused()
{
SerialNumberPool pool = new();
pool.Take(Bands.Band20M);
pool.Take(Bands.Band20M);
Assert.False(pool.Note(1, Bands.Band40M));
}
/// The same number back from the station it went to is the message going
/// round the entry, not two stations on one number.
[Fact]
public void TheNumberThisStationTookComingBackIsNoClash()
{
SerialNumberPool pool = new();
int mine = pool.Take(Bands.Band20M);
Assert.True(pool.Note(mine, Bands.Band20M));
}
[Fact]
public void ByBandAClashIsPerBand()
{
SerialNumberPool pool = new() { ByBand = true };
pool.Take(Bands.Band20M);
Assert.True(pool.Note(1, Bands.Band40M));
Assert.False(pool.Note(1, Bands.Band20M));
}
/// The whole pool from another station: the higher of the two is kept for
/// each band, so a station that was away cannot pull the numbers down.
[Fact]
public void MergingKeepsTheHigherNumberOfEachBand()
{
SerialNumberPool pool = new() { ByBand = true };
pool.Take(Bands.Band20M);
IReadOnlyList<int> theirs = new SerialNumberPool { ByBand = true }.Snapshot();
pool.Merge([.. theirs.Select((_, at) => at == N1mmBandIndex.Of(Bands.Band40M) ? 7 : 0)]);
Assert.Equal(2, pool.Next(Bands.Band20M));
Assert.Equal(8, pool.Next(Bands.Band40M));
}
[Fact]
public void TheLogSaysWhereTheNumbersHaveGot()
{
SerialNumberPool pool = new();
pool.SeedFrom([Worked(Bands.Band20M, 4), Worked(Bands.Band40M, 9)]);
Assert.Equal(10, pool.Next(Bands.Band20M));
}
[Fact]
public void TakingANumberTellsTheOtherStations()
{
SerialNumberPool pool = new();
List<ReservedNumber> taken = [];
pool.Reserved += (_, number) => taken.Add(number);
pool.Take(Bands.Band20M);
Assert.Equal([new ReservedNumber(1, Bands.Band20M)], taken);
}
}