February 202509:235k views

Testing my TUI

About

I wrote an integration testing setup for Solo, my terminal UI: a PHPUnit test starts the real process, sends it key presses, and makes assertions about what's on the screen. It's a bit like Laravel Dusk for the command line.

Transcript

Everything I said

00:00

This is my TUI right here. We are testing my command line tool. I am not touching the keyboard at all, and yet the command line tool is responding as if I am, because what's happening is I'm sending a bunch of key presses to the underlying TUI, and then I'm making assertions about what I see. You can see a little bit of status in the top there, running test, wrapping and unwrapping, seven actions remaining.

00:22

Upcoming action is ... That is a keyboard arrow up. And so once we get to where I am looking for this stuff, then it runs through a bunch more actions, makes some assertions, and boom. Now I know that that particular part of my TUI is working. Let me show you how I am doing this. Here is that PHP unit test case. You can see wrapping and unwrapping, which is the name we saw in the terminal when we were over there.

00:47

So wrapping and unwrapping, and this is, um, one of the assertions that I'm doing a bunch of times. So what I'm trying to assert is, um, let me, let me flip over and I can actually just run it not as a test. And so what I'm trying to assert here is that when, when I scroll all the way up here, so this is, uh, this is where the test takes us. It scrolls all the way up, and then when I hit the W key, which is allow wrapping.

01:11

So some of these lines have been truncated. You can see this one's been truncated here. What I'm trying to assert is that, uh, when I hit that W key, the top part of the screen doesn't jump around. So if you're scrolling around and you're like, "Ooh, I need to see, you know, I need to see the rest of these lines," I want the scroll bar to stay in the right position. And so if you're at number sixty-three and you hit W, you'll stay at number sixty-three even though it's a completely different line than it was before.

01:38

So it was line twenty-seven, now it's line fifty-four. I did a lot of math to figure that out, but I'm trying to make sure that this user experience is good for you. So that is what we're asserting. I wanna make sure that we're gonna explode, uh, we're gonna explode the lines on the screen, and we're gonna look at the fourth line, which is at index number three, and we're gonna assert that it contains string trace, which would mean we are staying constant right here.

02:02

And you can see it does work, and so that is what the assertion is. Now, the way that we do that is we get this, um, set of actions built up. So the first thing I'm gonna do is I'm gonna send the W hotkey, which is this hotkey right here, to allow wrapping or disallow wrapping, and that's gonna just help us scroll a little bit faster. And then I'm going to make an assertion that says allow wrapping, which means we are in the prevent wrapping mode because the hotkey says press W to allow.

02:31

Then, this part is really cool. Um, I have made it so that you can pass a generator through here. So in this case, this is a generator, so it's going to yield the, uh, up arrow key, meaning until it gets to number zero zero, you, you know, all of this stuff, until it sees that on the screen, it's just going to continue to yield up arrows.

02:54

And we can see that if we come back here and we run it again. You'll see it counts down, and then it says, "Seven actions remaining." But it, it continues to do stuff, and that is because the action that we're currently in is that generator. And so it's just yielding up arrows until we get to the right spot, and it takes a while because that zero zero is actually not the right spot.

03:14

There's one more stack trace above us that it's going to arrow up until it finds zero zero. We don't have to watch it. But once it does find zero zero, then it moves on. So after that, it moves on to arrow up one more time, and then it checks that the first line is trace, and so we're almost there. So it's gonna get zero zero, then check, then turn wrapping on and off, and check and check and check until we assert that it's all good.

03:39

So we go up arrow one more time, check that it's trace, turn wrapping back on, check that the, uh, top line didn't jump around, turn it back off just for giggles, and check that the top line did not r- jump around. And then after all of that runs, we know that, um, we've implemented this feature correctly because we have tested it visually, and we know that the, the lines aren't jumping up and down.

04:01

Okay, all of this code is open source, but I'm gonna give you a super quick crash course on how this is working. I think this is very cool, like integration testing for co- a command line interface. It's kind of cool. Um, so I'll give you a crash course on how it's working, but if you wanna dive in, the repo's down below. Now, the first thing we do, we've defined all of our actions here, all of the things we want to do, hotkeys, assertions, more hotkeys, all of that stuff.

04:25

We pass it through to a runSolo method, and the runSolo method receives the array of actions as well as a closure that defines how you want to set up Solo. So in this case, we just wanna run one command called logs, and the path is over here. It is in a fixtures folder because when you're testing, you need to test against something reliable, and so I have this file here that we test against all the time.

04:49

Now, the runSolo command is where it gets a little bit wacky. So the first thing we're gonna do is turn off output buffering from PHP unit, because if we don't, we won't be able to see anything, which would be very frustrating. Then we're gonna execute. Now, execute actually starts a process, and so this is, um, this is, uh, actually running the underlying command line interface or the TUI.

05:14

It's not mocking it. It's not doing anything in memory. It's, it actually spawns a new process, and it sets it up such that we can write input to it and receive the output back, and that is what we're gonna make assertions against, is the output that we receive back. So if you come down here, you'll see we're running PHP test bench Solo test, and we're passing that closure through to that artisan command.

05:39

And then in that artisan command, it unserializes it, and it sets up Solo, and then it runs Solo. And down here, you'll notice, um, that we, when we start it, we capture the output. So Solo is running under the hood. It's spewing out all of this output. We are capturing it, and we're assigning it to this frame variable.

06:00

So every time we get a new full frame, we capture that as a variable, and then that way we can make assertions against it, um, in our actions. So coming back up here, once we have started the process, we just enter a loop, and while the process is running, we're gonna do some stuff. The first thing we're gonna do is write some status, what the name of the test is, how many actions remain, what the next action is, and then, um, if we have a new buffer, we're gonna write that buffer out to the screen so we can watch it, so we can

06:30

maybe debug it, see what's going on. Okay. Then we just wait to make sure that we're not running the actions too quickly back to back. And once we're good, then, um, we call the new frame callbacks, which I'll show you here in a second. Um, but down here, this is where we parse the actions. So if the action is a string, we just write it to the underlying process.

06:52

And so that's how we get this right here, where, um, when we hit the W key, this test says, "Well, that's a string. I'm just gonna forward that to the underlying process and let it react however it reacts." So we're doing some honest-to-goodness testing here. So it forwards it to the underlying process. If it's an integer, we just kind of wait around, um, and if it's a callable, then we add it to this new frame callbacks.

07:15

So when, um, when we pass a callable as an action, so that would be this guy right here, we're saying, "Hey, the next time you get a new frame from the underlying process, I want you to hang on a second because I've got something I wanna do. And, um, that thing that I wanna do is I wanna make a PHP unit assertion. So give me the full ANSI-ified output, but also give me the plain output because sometimes I just wanna assert against the plain output." So we add that to, um, this class level property, and

07:45

then up here you notice call new frame callbacks. So, um, after we get a new frame, the very first thing we do is see if there are any callbacks that are left over from, uh, any previous actions. And if there are, we call the new frame callbacks, we pass through the frame, and we pass through a plain version of the frame just in case, you know, just in case it's easier to assert that way.

08:12

And here is one of the cool things. If the result we get from the callback is a generator, PHP has generators, they're very awesome. If it's a generator and it's valid, then you can add an additional action, which is the current, uh, result of that generator, and we'll put the callback back in the stack so that the next frame, it gets called again, which means, coming over here, down here you'll see we're yielding an up arrow.

08:39

That means until you see this, continue to put the up arrow on the action stack, and then once you do see this, well, you're done. You can move on, which is one more up arrow, and then we start doing our assertions. This gives me great confidence that my TUI is working correctly. So not only did I write a full-on TUI, I also wrote basically an integration testing library for it.

09:01

Um, this is somewhat like Laravel Dusk in that, um, instead of launching a browser, it launches the actual underlying process, and it gives you an interface to write input to it and get the output back from it and make assertions against it. So go check out the repo. Let me know if you have any questions. I hope you found this at least a little bit interesting. See ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues