Compressive Sensing - Sparse in frequency example
I think I understand your confusion. In your first example, it would have been more realistic to take

y = x(q)


for example.

A possible explanation for the approach you see here is that authors of such examples would like to ensure they obtain the exact sparse solution they generate the example from to compare against. Therefore they generate the measurements directly from it. It is not "cheating" per se in a mathematical sense, but you can say that it does not reflect the practical reality of taking measurements in the time domain.

What could happen when taking the measurements in the time domain is that if you generate sinusoids with arbitrary frequencies, the signal will appear less sparse in the frequency domain when the frequencies do not match the frequencies of the dictionary. If for example you generate a sinusoid at 1.5 Hz and your DFT has atoms corresponding to the frequencies 1 Hz, 2 Hz, 3 Hz, etc., your signal will not be 1-sparse in this dictionary but will have to use multiple atoms at adjacent frequencies to form the sinusoid at 1.5 Hz. A simple way to avoid this is to generate a signal of the prescribed sparsity directly in the frequency domain and then convert it to the time domain as part of taking the measurements.

Technically, it does not involve already knowing the frequency spectrum of the signal, because X is not known in the reconstruction and the sufficient frequency information in the classic sampling-theoretical sense is lost in the multiplication

y = A*X


because A is a wide matrix.